Thread: read() write() problem

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    read() write() problem

    I get something very weird. I do on the server some write() in a loop.... and when i read() in the client I get a single buffer with all the things I have send from the server. Is this posible?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes.

    TCP only guarantees that the order of bytes is preserved. During transmission, packets can be combined and fragmented. You need to be aware of this when sending and receiving.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    another problem is when I connect as a client to a server:
    Code:
    temp.sin_family=AF_INET;
    temp.sin_addr.s_addr=htonl("127.0.0.1");
    temp.sin_port=htons(111);
    if i do this it timeouts on the connect() call;
    Code:
            struct hostent *ip;
    
            ip=gethostbyname("127.0.0.1")
            temp.sin_family=AF_INET;
            memcpy((char *)&temp.sin_addr.s_addr,(char *)ip->h_addr,ip->h_length);
            temp.sin_port=htons(111);
    this works just fine.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by spank View Post
    temp.sin_addr.s_addr=htonl("127.0.0.1");
    Doesn't that give you a warning of some kind? It's not right. You want to use inet_addr(), not htonl().

    EDIT: Hitting the name server to convert a string to an IP because you don't know about inet_addr()... Hilarious.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    sorry... you are right... it doesn't compile without inet_addr()... in the actual program I used inet_addr() the post is a manual edit.

    very sorry about this... i'm new to socket programming

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by spank View Post
    very sorry about this... i'm new to socket programming
    I'm just being abrasive. It's morning, and my house is devoid of coffee.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read and write binary file?
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2008, 05:31 PM
  2. Talking to device over Serial: Write works, Read doesn't
    By theBishop in forum C Programming
    Replies: 2
    Last Post: 07-09-2008, 01:40 PM
  3. Replies: 2
    Last Post: 09-13-2006, 01:14 PM
  4. check for read or write
    By SoFarAway in forum C Programming
    Replies: 6
    Last Post: 04-09-2005, 01:50 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM