Thread: Using recv() and send()

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    11

    Using recv() and send()

    I want to write client-server program. The server should send many strings (messeges) and get response from the user on the client-side, here is in example:

    on the server-side:

    ....
    char buffer[buff_size];
    ......
    recv(...buffer...);
    .....
    sprintf(buffer,"...");
    send(....buffer....);
    .......
    sprintf(buffer,"...");
    send(....buffer....);
    .......
    sprintf(buffer,"...");
    send(....buffer....);
    .......
    sprintf(buffer,"...");
    send(....buffer....);
    .......
    recv(buffer);



    on the client-side:
    ....
    char buffer[buff_size];
    ......
    do{
    fgets(buffer, stdin);
    send(...buffer...);
    ...
    recv(...buffer...);
    printf("%s", buffer); //I don't know if printf() allow me to get all the transmitted buffer from the server-side?!!
    }while(1)



    any help appritiated?!!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    TCP/IP is a stream based protocol, which means you need to define message boundaries/lengths. A common way to do this is to send a length indicator through the socket first, so that the app at the other end knows how much data to expect. The other way is to use a specific byte value as a terminator, eg a \0. I think there are some examples of this code around, see what you can find. If you need more info, ask away.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    Thanks mate.

    In case of using '\0', as a teminator of send. does this may cause the client hung and not recieving the other strings may be send after the first one?!!

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>does this may cause the client hung and not recieving the other strings may be send after the first one?!!
    I think the answer is yes, if I understand the question right.

    Using a \0 as a terminator makes life a little different, you need to handle the buffering correctly. Read my posts here:
    http://cboard.cprogramming.com/showt...333#post321228
    http://cboard.cprogramming.com/showt...333#post321434
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TCP/IP Sockets in C (problem with send() and recv(): how to loop them)
    By ferenczi in forum Networking/Device Communication
    Replies: 3
    Last Post: 11-18-2008, 07:38 AM
  2. send and recv are cpu intensive?
    By Rune Hunter in forum Networking/Device Communication
    Replies: 14
    Last Post: 09-04-2007, 09:24 AM
  3. send() and recv() functions
    By kris.c in forum Networking/Device Communication
    Replies: 9
    Last Post: 06-24-2006, 09:41 PM
  4. send and recv
    By DeadManWalking in forum C Programming
    Replies: 1
    Last Post: 12-04-2005, 09:17 AM
  5. Replies: 2
    Last Post: 03-05-2002, 05:52 AM