Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

this very simple UDP client/server.. doesn't work?

121 views
Skip to first unread message

Adam M.

unread,
Aug 4, 2003, 12:37:26 AM8/4/03
to
Hi, I'm trying to get a simple UDP client/server working.

I can get it working when the client and server are running on the same
machine,
but when they're running on separate machines, the clients supposedly
succeeds,
but the server doesn't appear to receive anything (even though I can see
the
lights on the hub flash for both machines when the client executes sendto())
.

Both computers are on a very small LAN, both sitting here under my desk
connected
by a hub. So, I don't think it's caused by the UDP packet being lost
somewhere
along the way. It's never once succeeded when 'client' and 'server' are run
on
different machines.

Anyway, below is the code for my attempt.

/*** client.c ***/
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>

int main()
{ int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in sa;
int e;

sa.sin_family = AF_INET;
sa.sin_port = 3000;
sa.sin_addr.s_addr = inet_addr("24.165.20.125"); /* IP of machine running
'server' */

/* i've tried binding the socket here, but it makes no difference.
anyway,
it shouldn't be necessary, right?
*/

e = sendto(s, "hello, world", 13, 0, &sa, sizeof(sa));
printf("sendto: %d\n", e);
return 0;
}

/*** server.c ***/
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>

int main()
{ int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in sa;
int e;
char buf[100];

sa.sin_family = AF_INET;
sa.sin_port = 3000;
sa.sin_addr.s_addr = INADDR_ANY;
e = bind(s, &sa, sizeof(sa)); /* this succeeds, returning 0 */
printf("bind: %d\n", e);

/* when the client executes on the same machine, this works as expected.
when it executes on another machine, this never returns.
*/
e = recvfrom(s, buf, sizeof(buf), 0, NULL, NULL);
printf("recvfrom: %d\n", e);
if(e>0) printf("message: %s\n", buf);

return 0;
}

Jack Klein

unread,
Aug 4, 2003, 1:32:42 AM8/4/03
to
On Mon, 04 Aug 2003 04:37:26 GMT, "Adam M." <ad...@san.rr.com> wrote
in comp.lang.c:

> Hi, I'm trying to get a simple UDP client/server working.

In the future please leave comp.lang.c out of crossposts like this.
The standard C language has no support for sockets or any sort of
networking.

> #include <sys/socket.h>
> #include <netinet/in.h>

The two headers above are not part of the C language and are off-topic
here.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

Adam M.

unread,
Aug 4, 2003, 1:34:32 AM8/4/03
to
On Mon, 04 Aug 2003 04:37:26 GMT, Adam M. <ad...@san.rr.com> wrote:

> Hi, I'm trying to get a simple UDP client/server working.

> [snip]
Sorry, I'm an idiot.

It wasn't working due to 'iptables' blocking the packets. I can't believe
I forgot about that.

Wellp, nothing to see here, move along. Just another useless usenet post.
:-)

Monisha Barooah

unread,
Aug 4, 2003, 7:09:38 AM8/4/03
to
"Adam M." <ad...@san.rr.com> wrote in message news:<oprtc8i7...@news-server.san.rr.com>...


Hi Adam,

e = recvfrom(s, buf, sizeof(buf), 0, NULL, NULL);

You are using recvfrom and not even specified the IP address from
which to receive the packet?

I think that is the problem.

Monisha

0 new messages