Socket Programming question
-
Hi, Being relatively new to socket programming, I need some assistance in sending and receiving data over a TCP connected socket. I'm familiar with using send and recv commands to send and recv buffered character data, but what about if I want to send and recv data formatted within a structure? For example,
main () { struct { int data1; double data2; bool indicator; }msg; // initialize data msg.data1 = 0; msg.data2 = 5.8; msg.inidicator = false; // send data ..... // receive data ..... }
Are the send and recv commands the right ones to use for this type of information to send and receive data over a TCP connected socket or are there other commands that are better suited for this. When I use the send(socketfd, (char *)&msg, 0), it "appears" to work, but when receiving the data using recv(socketfd, (char *)&msg, 0), it returns with a -1. Thanks in advance for any help. -Martin NB: The commands must work on both the Windows and UNIX side, so Windows specific commands wouldn't work. -
Hi, Being relatively new to socket programming, I need some assistance in sending and receiving data over a TCP connected socket. I'm familiar with using send and recv commands to send and recv buffered character data, but what about if I want to send and recv data formatted within a structure? For example,
main () { struct { int data1; double data2; bool indicator; }msg; // initialize data msg.data1 = 0; msg.data2 = 5.8; msg.inidicator = false; // send data ..... // receive data ..... }
Are the send and recv commands the right ones to use for this type of information to send and receive data over a TCP connected socket or are there other commands that are better suited for this. When I use the send(socketfd, (char *)&msg, 0), it "appears" to work, but when receiving the data using recv(socketfd, (char *)&msg, 0), it returns with a -1. Thanks in advance for any help. -Martin NB: The commands must work on both the Windows and UNIX side, so Windows specific commands wouldn't work.you are sending 0 bytes. you need to give it a size that your are sending. You also need to give a size when you recv. It would work best if you just used sizeof(msg)