read() write() problem

This is a discussion on read() write() problem within the Linux Programming forums, part of the Platform Specific Boards category; I get something very weird. I do on the server some write() in a loop.... and when i read() in ...

  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
    32,845
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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, 06: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, 02:40 PM
  3. Replies: 2
    Last Post: 09-13-2006, 02:14 PM
  4. check for read or write
    By SoFarAway in forum C Programming
    Replies: 6
    Last Post: 04-09-2005, 02:50 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM

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