Socket Question

This is a discussion on Socket Question within the C Programming forums, part of the General Programming Boards category; I am writing a c tcp client socket program. If I want to send the server a string and 2 ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    or use write 3 times, or convert all you want to send into 1 string - and send it
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    478
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    A struct would work if you cast it appropriately
    While you do not encounter endian and alignment issues on the host and client
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Seems like you are ttrying to read from socket before someone is actually connected
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    No - you do not need that, but obviously - you cannot read into const char*
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,185
    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.

Page 1 of 2 12 LastLast
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, 08:55 AM
  3. problem closing socket
    By Wisefool in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-29-2003, 11:19 AM
  4. problem closing a socket
    By Wisefool in forum C Programming
    Replies: 1
    Last Post: 10-28-2003, 12:38 PM
  5. Simple Socket Question
    By SourceCode in forum Linux Programming
    Replies: 2
    Last Post: 06-22-2003, 09:20 PM

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