Thread: comand read, write. use in socket

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    comand read, write. use in socket

    Hello everyone ,
    i'm making a client-server system with comunication through socket.
    More clients write on the same socket and, for the specifications, i have to use the command write and read.

    When the server run the read on the socket, this take all messages present.
    But i want take only one message, the first.
    I don't know the size of message and so i read max 1024 characters. For this reason i read on the socket all messages contained in 1024 characters.
    These message are separated by "\n".


    Please help me with a example that resolve my problem or with good explanation

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So read the first message, and store everything that isn't part of the first message as the next pending message. Otherwise, you are going to have to make single byte read calls until you happen to run into a \n, at which point you stop reading for messages. Pick one. (This concept is suspiciously similar to checking for a single word or sentence in a file!)


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Hmm....where have I seen this before? Oh yeah, I remember:

    How do I read/write from a socket - Cboard-FAQ

    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    2
    Ok perfect, in my specifications it's solution is recommended.
    Solution 2 - Use a data length indicator
    Every message that is sent can be prefixed with a value that represents the data's length. The receiver starts by recv()ing a fixed number of bytes to get this length indicator and, once it has it, it recv()s that specific number of bytes. If all goes well, two recv()s are all that are needed to read one message. Of course, the data may be split, meaning that you need two or more calls to get it... and don't forget this includes the getting of the length indicator in the first place!
    But i don't understand how implement this.
    I read, for example, "0017This is a message0010So is this".
    So i have to elaborate the first message and save the second in a list of pending message?
    Not exists a method for change the pointer to file socket to next string.
    That is, I read "0017This is a message0010So is this" then i get "0017This is a message" and change the pointer of socket to position 21 -> so next time i read "0010So is this".

    I hope that I explained well.
    Thanks

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read an int. See what it is. Read that many bytes. Read another int. Repeat until bored.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well think about the message for a little bit.

    "0017This is a message
    0010So is this"

    There are two very obvious parts of this. The length sent over is how many bytes you should read next, so you do that. And then you keep listening to get the next number, then read N bytes again.

    Rinse. Repeat.

    Edit - foiled again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket write synchronization
    By kapil1089thekin in forum C Programming
    Replies: 2
    Last Post: 02-01-2011, 11:28 PM
  2. Write to a file from a socket?
    By Anddos in forum Windows Programming
    Replies: 6
    Last Post: 03-29-2009, 06:07 PM
  3. Closed socket but we can still write
    By myle in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-23-2007, 09:48 AM
  4. Internet Socket - Read/Write sync with Select()
    By INFERNO2K in forum Networking/Device Communication
    Replies: 8
    Last Post: 07-19-2007, 04:28 PM
  5. read/write through udp socket
    By xErath in forum Networking/Device Communication
    Replies: 3
    Last Post: 05-22-2005, 05:43 PM