SFML community forums

Help => Network => Topic started by: AniCator on February 05, 2013, 04:09:58 pm

Title: UDP | Make client receive response from server
Post by: AniCator on February 05, 2013, 04:09:58 pm
Hello everyone,

I've been trying to setup a simple UDP server and client but I've got this one issue.
I would like to make it so that the server sends a message back to the client after the server has received a message from that client.

It would go like this.

My idea is that the client will send a command to the server for example. Like a request.
The server would then send a packet back to the client containing the requested information.
For example position info.

I might be doing it all wrong and if so. Please feel free to take out the hammer and bash my brains in.

:)

Client (area that is most likely responsible for the issue)
                //Listen for connection on port ...
                ClientSocket.Bind(0);

                char Buffer2[128];
                size_t Received;
                sf::IPAddress Sender;
                unsigned short Port;

                //Receive data from server
                if(ClientSocket.Receive(Buffer2,sizeof(Buffer2),Received,Sender,Port) == sf::Socket::Done)
                {
                        std::cout<<Buffer2<<std::endl;
                }

Client console output
Failed to receive data ; the UDP socket first needs to be bound to a port
Title: Re: UDP | Make client receive response from server
Post by: Laurent on February 05, 2013, 04:20:21 pm
And are we supposed to guess what the problem is with this code?

Have you looked at the SFML examples? The "Socket" one does exactly what you want.
Title: Re: UDP | Make client receive response from server
Post by: AniCator on February 05, 2013, 04:28:00 pm
The 'Socket' sample code only does both sending and receiving for the TCP portion.
I'm using UDP.

Main problem with my code is that I don't know how to make get the client to receive the message from the server.

Which is most likely caused by the port I'm trying to bind it to.
I set it to port 0 because I don't know how to get the port the server will be sending the message to.
It seems that only the server knows which port the client used to communicate with it.
                //Listen for connection on port ...
                ClientSocket.Bind(0);

                char Buffer2[128];
                size_t Received;
                sf::IPAddress Sender;
                unsigned short Port;

                //Receive data from server
                if(ClientSocket.Receive(Buffer2,sizeof(Buffer2),Received,Sender,Port) == sf::Socket::Done)
                {
                        std::cout<<Buffer2<<std::endl;
                }
Title: Re: UDP | Make client receive response from server
Post by: Laurent on February 05, 2013, 04:51:00 pm
Quote
The 'Socket' sample code only does both sending and receiving for the TCP portion.
I'm using UDP.
It does both.
Title: Re: UDP | Make client receive response from server
Post by: AniCator on February 05, 2013, 04:58:27 pm
I got it to work sort of but currently both the server and client require their ports to be open for it to work.  :-\
Title: Re: UDP | Make client receive response from server
Post by: Ruckamongus on February 06, 2013, 04:37:48 am
Look up UDP hole-punching. Essentially, the client must send the server a UDP packet temporarily opening the port. However, the port can (and most likely will) change by the time the server receives it. So make note of the port when the server receives the data, then send the data back through that port. If the client doesn't receive a reply in a short period of time, the "punched" port will close, and the server wil never get its UDP messages through.