Send receive using sockets

B

Berk Birand

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
 
V

Vladimir S. Oka

Berk said:
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

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
 
I

Ian Collins

Berk said:
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!
 
T

those who know me have no need of my name

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.
 
K

Keith Thompson

Berk Birand said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top