# UDP echo- back client+server in C++



## burnthepc (Aug 12, 2007)

Hi guys, I'm pulling my hair out here trying to understand Winsock headers.

My lecturer must have a sick sense of humour as he's set us the task of writing a UDP client/server after only studying C++ for 2 months.

I've managed to get a server up and listening to a socket. I've got the client to send the message and the server to pick it up and print it to screen.

The remaining horrors are 

1) Read the IP address of sender and print to screen... just reading to a vairiable is the bit I'm stuck at.

2) Echoing the message back to sender and reading it...I'm a bit lost on this - can this be done with a simple swap from a send to receive socket? And how does the server get the info to send to client - may be related to 1) above.

Any help you guys can give would be great.


----------



## JohnWill (Oct 19, 2002)

I'll move you over to development, probably a better chance of a response here.


----------



## burnthepc (Aug 12, 2007)

I've solved many problems with it so far.

Only one that remains is that the server is having trouble closing it's recv and starting a send.

Here's the critical info:

Programmed:
Server listen then send to sender's IP and port
Client send and listen on port server was listening on.

Behaviour:
Server gets message, seems to send and exits like planned.
client sends message and hangs waiting for reply.

In testing I've found that another client sending a message (to 127.0.0.1) will be picked up by the hanging client. So the client is listening to the right port.

If I hardwire a different port for the server and client to send the ack/echo on then the program works fine. Problem is this is supposed to be UDP and the client and server can't agree on a port for return send.

Any ideas as to what's causing it? I think it may be that the server isn't freeing up it's recieve port to send the datagram on.

I've tried shutdown(udp_socket) but to no avail.


----------

