473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Send receive using sockets

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
Jan 31 '06 #1
4 2762
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

Jan 31 '06 #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.

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

--
Ian Collins.
Jan 31 '06 #3
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
Jan 31 '06 #4
Berk Birand <gr******@yahoo.com> 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) ks***@mib.org <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.
Jan 31 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kitchen Bin | last post by:
Hi. I am trying to use Sockets to do multiple Send and Receives via HTTP (not simultaneously). A first pair of Send/Receives works fine and sure enough I receive HTML back, but the next...
1
by: EppO | last post by:
I wrote a small server/client app. I created a class for my customized socket object using (synchronous) Sockets functions for both server and client use. When client connects to server, if It...
1
by: Amadej | last post by:
Hello everyone, I'm having some odd problems with a little program I wrote for sending/receiving bytes across the network. I am using synchronous sockets, and it seems that when I send byte...
1
by: Steve | last post by:
Please take a look at the simple code segment below and advise me what is wrong. According to the help and examples I've seen it should work unless I misunderstand something. The problem is...
2
by: Matt | last post by:
Hi, I've got a C# client/service application using a Socket object to communicate. I have a socket bug that I can't solve without kludged-code. Does anyone know a more elegant solutions? Is this...
1
by: Laura T. | last post by:
Hi, I've this kind a program, using sockets to communicate with the clients. One of the clients can be a web browser (like IE). When using IE as a client, the transmission blocks completely, and...
11
by: hazz | last post by:
smtpClient.Send(message) is causing me problems as per specifics in the trace below. Email is sent but not without this error typically upon sending the second email, but sometimes when running...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
14
by: eliss.carmine | last post by:
I'm using TCP/IP to send a Bitmap object over Sockets. This is my first time using C# at all so I don't know if this is the "right" way to do it. I've already found out several times the way I was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.