Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Send receive using sockets

Reply
Thread Tools

Send receive using sockets

 
 
Berk Birand
Guest
Posts: n/a
 
      01-31-2006
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
 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-31-2006
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-31-2006
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.
 
Reply With Quote
 
those who know me have no need of my name
Guest
Posts: n/a
 
      01-31-2006
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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-31-2006
Berk Birand <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use Request to receive xml data that send by using HttpWebRequest, Ying-CO ASP .Net 0 06-05-2008 09:52 PM
Re: Can we send and receive data to and from Port Using Python ? Steve Holden Python 0 04-11-2008 12:52 PM
Trying to Send Repeated Messages to a Server Using sockets danfolkes Python 1 10-16-2007 06:50 PM
Send/Receive using MPI_Type_struct lancer6238@yahoo.com C Programming 5 09-09-2007 08:17 PM
using winpcap to send receive packets kiny Computer Support 4 07-09-2006 05:54 PM



Advertisments