Send receive using sockets

Discussion in 'C Programming' started by Berk Birand, Jan 31, 2006.

  1. Berk Birand

    Berk Birand Guest

    Hi,

    I've been battling to get this for a long while now, but failed miserably
    after 6 hours. I'm trying to establish a client/server connection using
    low-level UNIX socket system calls. I am running into some troubles that I
    for some reason can't understand.

    I know that posting code and asking "What's wrong?" is not good
    netiquette, but as of now I have no clue as to what might be wrong. I've
    been following everything by the book.

    The code is supposed to get a directory listing, and send it to a client.

    Here's the server-side code


    struct dirent **namelist;
    int n;

    n = scandir("./public/", &namelist, 0, alphasort);
    if (n < 0)
    perror("scandir");
    else {
    while(n--) {
    printf("%s\n", namelist[n]->d_name);
    send(sock,namelist[n]->d_name, strlen(namelist[n]->d_name), 0);
    free(namelist[n]);
    }
    free(namelist);
    }

    sock is the socket, and it's been opened using the standard library calls.

    Here's the code for the client

    while ((n = recv(sock, buf, BUFFER_LEN, 0)) > 0) {
    printf("%s", buf);
    //write((int) stdout, &buf, strlen(buf));
    }

    This for some reason doesn't work. buf is declared as
    char buf[BUFFER_LEN];

    The client doesn't print anything! And it doesn't get out of that loop
    either.

    I would really appreciate if you could point me to some possible
    mistakes..

    Thanks,
    Berk Birand
    Berk Birand, Jan 31, 2006
    #1
    1. Advertising

  2. Berk Birand wrote:

    > Hi,
    >
    > I've been battling to get this for a long while now, but failed
    > miserably after 6 hours. I'm trying to establish a client/server
    > connection using low-level UNIX socket system calls. I am running into
    > some troubles that I for some reason can't understand.


    This is not topical in c.l.c. Standard C does not know of sockets. Also,
    the most knowledgeable experts here (not me!) may not know anything
    about them either. For better (any) advice try comp.unix.programmer,
    for example.

    >
    > I know that posting code and asking "What's wrong?" is not good
    > netiquette, but as of now I have no clue as to what might be wrong.
    > I've been following everything by the book.


    Actually, here it is quite the right way to go about things, provided
    it's Standard C, and it shows some effort on your part (and your
    questions are framed reasonably well -- yours are). If the problem does
    not turn out to be UNIX-related, try to strip your code of UNIXey bits
    and try again here. (But do see below, as well.)

    > Here's the server-side code


    < snipped some off-topic code>

    >
    > Here's the code for the client
    >
    > while ((n = recv(sock, buf, BUFFER_LEN, 0)) > 0) {
    > printf("%s", buf);
    > //write((int) stdout, &buf, strlen(buf));
    > }
    >
    > This for some reason doesn't work. buf is declared as
    > char buf[BUFFER_LEN];
    >
    > The client doesn't print anything! And it doesn't get out of that loop
    > either.


    This actually may be because you never terminate printf() with a '\n'.
    Not doing this releaves the implementation from actually printing
    anything.

    If you do emit an '\n' outside while {}, the problem colapses to: why
    the while {} never exits (which you already know is the case). For
    that, you'll have to ask somwhere where recv() is topical, I guess.

    Cheers

    Vladimir

    --
    "No, `Eureka' is Greek for `This bath is too hot.'"
    -- Dr. Who
    Vladimir S. Oka, Jan 31, 2006
    #2
    1. Advertising

  3. Berk Birand

    Ian Collins Guest

    Berk Birand wrote:
    > Hi,
    >
    > I've been battling to get this for a long while now, but failed miserably
    > after 6 hours. I'm trying to establish a client/server connection using
    > low-level UNIX socket system calls. I am running into some troubles that I
    > for some reason can't understand.
    >

    Move this to comp.unix.programmer and when you do, post the code that
    creates the sockets!

    --
    Ian Collins.
    Ian Collins, Jan 31, 2006
    #3
  4. in comp.lang.c i read:

    >I'm trying to establish a client/server connection using
    >low-level UNIX socket system calls.


    then you need comp.unix.programmer.

    > //write((int) stdout, &buf, strlen(buf));


    *boggle*

    stdout is a FILE *, not something you should ever consider casting to int.

    --
    a signature
    those who know me have no need of my name, Jan 31, 2006
    #4
  5. Berk Birand <> writes:
    > I've been battling to get this for a long while now, but failed miserably
    > after 6 hours. I'm trying to establish a client/server connection using
    > low-level UNIX socket system calls. I am running into some troubles that I
    > for some reason can't understand.

    [...]

    Standard C has no support for sockets. Try comp.unix.programmer.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jan 31, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    41,119
    Roedy Green
    Dec 7, 2005
  2. arti
    Replies:
    0
    Views:
    567
  3. Send/Receive using MPI_Type_struct

    , Sep 9, 2007, in forum: C Programming
    Replies:
    5
    Views:
    1,125
    Malcolm McLean
    Sep 9, 2007
  4. Steve Holden
    Replies:
    0
    Views:
    765
    Steve Holden
    Apr 11, 2008
  5. Ying-CO
    Replies:
    0
    Views:
    811
    Ying-CO
    Jun 5, 2008
Loading...

Share This Page