Thread: Socket Programmins

  1. #16
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  2. #17
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Have read that from cover to cover, though in the examples that he gives, he only uses char data types. I, on the other hand am trying to send a struct data type.

    The prototypes listed above do not list any of the variables that they accept as of type char.

    They are listed as const void * and void * respectively.

    Thanks for your input anyway.
    WebmasterMattD
    WebmasterMattD.NET

  3. #18
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>. I am trying to send a struct data type, though it does not want to be sent as it should.
    Can you be more specific in what's actually wrong?
    What debugging have you done?
    Does it send the correct number of bytes?
    What do you receive if you just use a normal buffer and hex dump it?
    What's the definition of the struct? Does it contain pointers (which might be a problem)?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #19
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Okay, what is actually wrong, I am not sure about.
    First of all, in all of the socket programming examples and tutes that I have look through and read they use chars. What they also do is that they do not pass the char array to send as
    send( socket, &msg, sizeof( msg ), 0 )
    they pass it as follows
    send( socket, msg, sizeof( msg ), 0 )

    This really confuses me. When I try to pass any other data type to the send function in this manner, the compiler will generate the error
    ' Can not convert from type const void * to MsgPacket '.

    This error dissapears when i pass the memmory address using the & operator with the structure and send as follows
    send( socket, &MsgPacket, sizeof( MsgPacket ), 0 )

    The structure definition does not contain any pointers what so ever and is as follows:
    struct MsgPacket
    {
    string username;
    string message;
    };

    The reason that it is so simple for the moment is so that I can get the passing of the structure right before I get too carried away.

    As far as the number of bytes, That is being sent correctly.

    Hex dumping? Well I am not sure on how to get that achieved.

    Thanks for your feedback, I hope that this futher information will help in the solving of this problem.
    WebmasterMattD
    WebmasterMattD.NET

  5. #20
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Your confusion over this:
    >send( socket, &msg, sizeof( msg ), 0 )
    >they pass it as follows
    >send( socket, msg, sizeof( msg ), 0 )
    is easily explained. A char array defined like this:
    >char mydata[100];
    will be passed like so:
    >>send( socket, mydata, sizeof( msg ), 0 )
    because mydata is a pointer to the first element of the array. That's how arrays work.

    Now if you have a single struct, then yes, you need to pass it's address, so you must use &mystruct.

    >As far as the number of bytes, That is being sent correctly.
    To clarify, does send() return the correct number of bytes?

    >Hex dumping? Well I am not sure on how to get that achieved.
    After recv()'ing data, write out a message that says %d bytes received (rc from recv), then loop through your received buffer 1 to nbytes times using printf("%02x ", buf[i]); or something similar. It's just a way of seeing the raw data your app received.

    >string username;
    I'm not sure of the underlying structure of a C++ string. This may be a problem, you might need to convert it somehow.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #21
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Thanks for your reply Hammer, it was of greate help.
    It appears that because String are created dynamicaly, then it is not as easy to determin their size ( in memory ) and requires a bit more fiddling around. Anyway when I created a different structure, this time with two integers contained inside, it was sent without a problem.

    If anyone with a bit more experience in sending C++ strings over a TCP ( stream ) socket connection, then please let me know how it is done.

    Later,
    WebmasterMattD
    WebmasterMattD.NET

  7. #22
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you want, you could send the strings indivually over the wire, by passing the c_str() to the send().

    stringA.c_str() for example will get you a char* to the string.

    I not sure how this will fit your needs though.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #23
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Thanks for your input about strings, and all.

    While, I have had success sending string using the c_str() command, it is a lot easier to send something such as a structure.

    I will rethink the way that I am going to solve the problem, and hopefully get it working better than before.

    Thanks,
    WebmasterMattD
    WebmasterMattD.NET

  9. #24
    TrollKingBlows
    Guest
    There's a reason everything you read sends text strings...
    different machines represent binary differently in memory, the size of data types and big-endian v. little-endian, etc...

    Sending binary structures across a socket could lead to a corruption of your data...

  10. #25
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Originally posted by TrollKingBlows
    There's a reason everything you read sends text strings...
    different machines represent binary differently in memory, the size of data types and big-endian v. little-endian, etc...

    Sending binary structures across a socket could lead to a corruption of your data...
    Why would he trust a cry baby?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  2. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  3. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 02:51 AM
  4. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 10:33 AM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM