C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 05-18-2006, 08:01 PM   #1
C++ Enthusiast
 
jmd15's Avatar
 
Join Date: Mar 2005
Location: MI
Posts: 532
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:
Code:
case FD_READ:
         recv((SOCKET)wParam,(struct JCPPM *)&JCPPpacket,sizeof(struct 
         JCPPM),0);
         if(strcmp(JCPPpacket.data,"")!=0)
         ConsoleMessage("",JCPPpacket.data,false);
         break;
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:
Code:
struct JCPPM
{
       unsigned int type;
       char *sender;
       char *data;
};
struct JCPPM JCPPpacket;
And I probably have given more info than needed BUT I get this error on my recv function:
Code:
132 C:\WINDOWS\Desktop\JCPPChat\main.cpp cannot convert `JCPPM*' to `char*' for argument `2' to `int recv(SOCKET, char*, int, int)'
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.
__________________
Trinity: "Neo... nobody has ever done this before."
Neo: "That's why it's going to work."
c9915ec6c1f3b876ddf38514adbb94f0
jmd15 is offline   Reply With Quote
Old 05-18-2006, 08:08 PM   #2
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 3,020
The recv() function takes a buffer of type char*, so that is what you need to cast to.
Code:
recv((SOCKET)wParam,(char*)&JCPPpacket,sizeof(struct
bithub is offline   Reply With Quote
Old 05-18-2006, 08:11 PM   #3
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 3,020
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.
bithub is offline   Reply With Quote
Old 05-18-2006, 08:35 PM   #4
Registered User
 
Join Date: Jan 2005
Posts: 847
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.
Quantum1024 is offline   Reply With Quote
Old 05-19-2006, 05:58 AM   #5
C++ Enthusiast
 
jmd15's Avatar
 
Join Date: Mar 2005
Location: MI
Posts: 532
@ bithub
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.
__________________
Trinity: "Neo... nobody has ever done this before."
Neo: "That's why it's going to work."
c9915ec6c1f3b876ddf38514adbb94f0
jmd15 is offline   Reply With Quote
Old 05-19-2006, 02:00 PM   #6
Registered User
 
Join Date: Jan 2005
Posts: 847
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.
Quantum1024 is offline   Reply With Quote
Old 05-19-2006, 03:05 PM   #7
C++ Enthusiast
 
jmd15's Avatar
 
Join Date: Mar 2005
Location: MI
Posts: 532
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.
__________________
Trinity: "Neo... nobody has ever done this before."
Neo: "That's why it's going to work."
c9915ec6c1f3b876ddf38514adbb94f0
jmd15 is offline   Reply With Quote
Old 05-19-2006, 03:32 PM   #8
Registered User
 
Join Date: Jan 2005
Posts: 847
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
Code:
Type::Message Sender::Message Data\r\n
and then reading in the data on the recieving end and dealing with it once you have a complete line.
Quantum1024 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting an error with OpenGL: collect2: ld returned 1 exit status Lorgon Jortle C++ Programming 6 05-08-2009 08:18 PM
An error is driving me nuts! ulillillia C Programming 5 04-04-2009 09:15 PM
Making C DLL using MSVC++ 2005 chico1st C Programming 26 05-28-2008 01:17 PM
Learning OpenGL HQSneaker C++ Programming 7 08-06-2004 08:57 AM
Couple C questions :) Divx C Programming 5 01-28-2003 01:10 AM


All times are GMT -6. The time now is 04:53 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22