recv() typecasting error

This is a discussion on recv() typecasting error within the Networking/Device Communication forums, part of the General Programming Boards category; I have a server and a client program written and I have a struct named JCPPM that is the struct ...

  1. #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

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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.

  4. #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.

  5. #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

  6. #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.

  7. #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

  8. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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