Thread: Alternate send()s and recv()s

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    Alternate send()s and recv()s

    Hi guys,

    I am new to this forum and also socket programming, and heres my question.

    I am using send() and recv() in my socket codes. How do you make them flexible in the sense you can have multiple send() before you recv(). at the moment my code looks like

    send()
    recv()
    send()
    recv()
    .
    .
    .
    send()
    recv()

    which means the order of send() and recv() is fixed. I need it to be like a chatting program, where you can have one end to have many send()s and a few recv() after that... the other end to have reverse order obviously.... I am sure you guys could understand what I need here. Tried to look through the forum, but not having much luck. perhaps a link to previous thread will help too...

    Thanks heaps!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Google for:
    1. non-blocking sockets
    2. The select() function.
    3. The poll() function.

    Any of those should be fine for you.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Hi

    Thanks for the reply. Did a quick look up on select()....

    I can understand how it works, and managed to compile an example.... the problem with it was that.. the moment i clicked on the keyboard, the character was immediately sent. ie the code didnt send out a string, but sent out characters. (when i want to type "hello", the h-e-l-l-o get sent separately.... the moment i typed h, the server received it and had it displayed)

    my question then is.. how do i send string instead of characters... they are the same, yes, but i need to send.. say a complete word...("hello", then hit ENTER, and server captures that as a whole word)...

    my bet at the moment, is to include #typedef STDIN 0 in the code, and FD_SET(STDIN,&readfds)? and the moment the code only included FD_SET(sock_id,&readfds)....

    sorry, for my might-be-silly question...

    Thanks.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    One thing to do then is to make stdin a blocking stream (which it is by default). The select() call should only return an indication for stdin when you have pressed 'return' (or the stream buffer is full).

    Or you could leave it as it is now, and buffer each key press internally to your program, and only send the string when you see a newline character. This also allows you a bit more flexibility, say sending a partial line after some timeout.
    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.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Hi,

    Thanks to all, my part of the work is done.. the broadcast server is working perfectly.

    Next, I shall hand over the code to the next developer.. however, i am just curious as to whether our proposal is possible...

    The next person shall interface the C program that Ive created with his software. Therefore, I wouldnt need any standard input from users anymore as the software will actually provide a string. The string is actually a command, which will then be broadcast to all connected clients, to send the command to all other similar softwares.. so actually i am just creating a medium for the command to be broadcast to all of the connected computers running the software...

    my question is, im not sure whether will STDIN now work.

    If not, how can i program such that:

    if FD_ISSET( counter = listener)
    {
    then I will call a recv() to receive the command from server
    }
    else
    {
    how can i detect if a full string, say of 25 char, is full, and transmit once i have all 25 characters ... eg call send() to send out char outmsg[25] when all 25 characters are filled
    }

    just curious... please advise... thanks

Popular pages Recent additions subscribe to a feed