recv() typecasting error
I have a server and a client program written and I have a struct named JCPPM that is the struct that holds my packet of data I send between the program. I have this code in my server program:
Obviously the declared instance of my JCPPM struct is JCPPpacket. This particular case I'm trying to receive a packet of the exact same setup(even same naming system since client and server have been written by me). The ConsoleMessage function is my custom function and no errors arise from that so don't worry about that. Up further I have this code:
recv((SOCKET)wParam,(struct JCPPM *)&JCPPpacket,sizeof(struct
And I probably have given more info than needed BUT I get this error on my recv function:
unsigned int type;
struct JCPPM JCPPpacket;
A typecasting error that I didn't expect. I thought the recv function took any kind of type to receive to. Anybody know what's my problem? Thanks.
132 C:\WINDOWS\Desktop\JCPPChat\main.cpp cannot convert `JCPPM*' to `char*' for argument `2' to `int recv(SOCKET, char*, int, int)'
The recv() function takes a buffer of type char*, so that is what you need to cast to.
Also keep in mind that the structure you are sending has two pointer values in it, and those pointer values will be invalid once you send them out a socket.
Aso keep in mind that recv might not get the entire structure in a single call. The return value will tell you how many bytes were recieved.
So you're saying it won't send the entire structure? As in, after I receive the structure, it won't have the JCPPpacket.type and JCPPpacket.sender? So it's basically just sending the data segment? I probably have misunderstood you and I apologize.
Thank you both for your quick replies.
What bithub is saying is that the structure will be sent but not the data pointed to by sender and data. Only the pointers themselves will actually be sent and who knows what these will actually point to on another machine.
To send these things you would need to pass the sender and data pointers to send.
Hmm, alrighty then. It looks as if the purpose I created that struct is defeated because it won't send all the data associated with it. I might as well just send segment by segment(for example send the sender name first, then the type and then the message) instead of a struct, UNLESS I'm missing something. What is the positive side of sending structs against sending the data in segments? Thanks for clearing that up Quantum.
Unless you're using some kind of deliminater at the end of each messages to show it's end you should send the length before so that the recieving end knows how much to recieve.
Use structs when it is a fixed amount of fixed sized items, use a buffer when it is a variable amount.
If the data you're sending is mostly text then I would sugest using a deliminater like \r\n and using another deliminater to seperate message sender and message data. For example
and then reading in the data on the recieving end and dealing with it once you have a complete line.
Type::Message Sender::Message Data\r\n