c++ - TCP socket program to send a Buffer. -
// The code below works fine, but is used to send the string. What changes do I make to send a buffer (address of buffer), which includes the headers with different data type fields? (I'm new to programming. So, excuse me at noob)
ClientSocket.cpp ClientSocket :: ClientSocket (std :: string host, int port) {if (! ()) {SocketException ("Could not create client socket."); } If (! Socket :: connect (host, port)) (socket expansion ("could not connect.");}} Client clients & client's socket :: operator (Std :: string & amp; s) const {if (! Socket :: recv (s)) {SocketException ("Could not read from socket");} Return * This;} Simple_client_main Cpp #include "ClientSocket.h" #include "SocketException.h" #Include & lt; iostream & gt; #include & lt; String {int main (int argc, int argv []) {try {client sock client certificate ("169.254.103.63", 30000); std :: string reply; {client_socket << test message . "; Client_socket>> Answer:; Hold (socket expansion & amp;) {} std :: cout & lt;" We received this response from the server: \ n \ "" & Lt; answer << \ n ";;} hold (socket expansion and a) {std :: cout & lt; & Lt; "The exception was captured:" & lt; & Lt; E.description () & lt; & Lt; "\ N"; } Return 0; } ServerSocket.cpp // ServerSocket class implementation #include "ServerSocket.h" #include "SocketException.h" ServerSocket :: ServerSocket (int port) {If (! Socket :: create ()) {SocketException ("Server socket Create. "); } If (! Socket :: bund (port)) (socket exception ("could not be compelled by port.");} If (! Socket :: listen ()) {socket expansion ("Could not hear socket." );}} ServerSocket :: ~ ServerSockets () {} Constant Server Server & ServerSocket :: Operator & lt; (const std :: string & amp; s) const {if (socket :: send (send! }) {Socket exception ("could not be written in socket");} Return * This;} Cons ServerSocket & ServerSocket :: Operator & gt; & gt; (std :: string & amp; s) const {If (! Socket :: recv (s)) {SocketException ("Could not be read from socket");} return * this;} zero serversecure :: accept (server socket and sock) {if (! Socket :: acceptable) (socket expiry ("socket not accepted" Could go ");}} simple_server_main.cpp #include" ServerSocket.h "#include" SocketException.h "#include & lt; string & gt; #include & lt; iostream & gt; int main (int argc, int Argv []) {std :: cout & lt; "Running .... \ n"; Try {// Socket Server Server (30000); while (true) {ServerSocket new_sock; Server.accept (new_sock); {While {true} {std :: string data; New_sock & gt; & Gt; Information; New_sock & lt; & Lt; Information; }} Hold (socket expansion & amp;) {}}} hold (socket expansion and a) {std :: cout & lt; & Lt; "The exception was captured:" & lt; & Lt; E.description () & lt; & Lt; "\ NExiting \ N."; } Return 0; } You have to send new () and recv () functions, which you did not show A socket can send binary data but you have to give it a shape in the bytes. There will be a problem on the recv () side, to find out how many bytes you need before the entire string. It usually requires adding some protocols, such as the length prefix in each message.
Comments
Post a Comment