Thread: Socket Question

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    47

    Socket Question

    I am writing a c tcp client socket program. If I want to send the server a string and 2 ints can I send that in one write function or do I have to use the write function 3 times, once for each paramter? Example below
    Code:
    write(sockfd,inputString,int1,int2,strlen(inputString)+2*sizeof(int));

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    or use write 3 times, or convert all you want to send into 1 string - and send it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    And remember to respect the endianess of the host machine, when transmitting numbers in binary format.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Quote Originally Posted by vart View Post
    or use write 3 times, or convert all you want to send into 1 string - and send it
    So you are saying I could use both methods?

    Quote Originally Posted by xuftugulus View Post
    And remember to respect the endianess of the host machine, when transmitting numbers in binary format.
    Thanks, I'll keep that in mind.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    You might want to take a look at fdopen(). This allows you to create a stream from your FILE*. Then you can use fread() and fwrite() which is sometimes easier.

    -Dustin
    http://www.theCprogrammer.com

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    I guess I'm confused because my sample code I posted above does not work. I need to send 2 ints and a string in 1 packet. How would I do this? i thought about using a struct but that did not compile either.

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by BENCHMARKMAN View Post
    I guess I'm confused because my sample code I posted above does not work. I need to send 2 ints and a string in 1 packet. How would I do this? i thought about using a struct but that did not compile either.
    Does the prototype for write include parameters that are two ints and a string?

    No. Why do you think it doesn't work?

    Convert the ints to a string, add the other string to that string, send it.

    A struct would work if you cast it appropriately - what is the default type for the variable to be written in the write() prototype?

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    A struct would work if you cast it appropriately
    While you do not encounter endian and alignment issues on the host and client
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Ok so I was able to cast it as a char but how do I get the server to accept the struct. Server code is
    Code:
     = read(sockfd,(const char*)&p,sizeof(p));
    but I get an error on the server after starting up the client "ERROR reading from socket: Transport endpoint is not connected". I don't get this error if I send a simple string. Sorry for the dumb questions and thanks for the patients. I've never done any socket programming before.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Seems like you are ttrying to read from socket before someone is actually connected
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Quote Originally Posted by vart View Post
    Seems like you are ttrying to read from socket before someone is actually connected
    Nope. I just commented out my line and right below the comment tried reading a string and got no error messages. Soon as I change the line back to the struct it errors out. Do I need to read it in first as a string and then cast it?

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    No - you do not need that, but obviously - you cannot read into const char*
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Quote Originally Posted by vart View Post
    No - you do not need that, but obviously - you cannot read into const char*

    If sorry I didn't fully understand your comment. I understand I don't need to read as a cosnt char* but are you also saying I don't need to cast the struct bacK? How exactly would I go about accessing the members of the struct that the server receives?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    read() expects a void * for the second argument, does it not? I suppose plain char * would work too.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Quote Originally Posted by tabstop View Post
    read() expects a void * for the second argument, does it not? I suppose plain char * would work too.
    Ok but If the client is sending a struct that has been cast to a const char how should the server read the message and be able to access the struct members? This is what I'm confused on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket program
    By mhetfield in forum C Programming
    Replies: 5
    Last Post: 04-03-2007, 03:46 PM
  2. Slight problem with socket reading!!!
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 02-15-2006, 09:55 AM
  3. problem closing socket
    By Wisefool in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-29-2003, 12:19 PM
  4. problem closing a socket
    By Wisefool in forum C Programming
    Replies: 1
    Last Post: 10-28-2003, 01:38 PM
  5. Simple Socket Question
    By SourceCode in forum Linux Programming
    Replies: 2
    Last Post: 06-22-2003, 09:20 PM