Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: UDP | Make client receive response from server  (Read 5696 times)

0 Members and 1 Guest are viewing this topic.

AniCator

  • Newbie
  • *
  • Posts: 6
    • View Profile
UDP | Make client receive response from server
« 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.
  • Client sends message to server.
  • Server receives message. Sends another message back to client.
  • Client receives message.

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
« Last Edit: February 05, 2013, 04:40:48 pm by AniCator »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: UDP | Make client receive response from server
« Reply #1 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.
Laurent Gomila - SFML developer

AniCator

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: UDP | Make client receive response from server
« Reply #2 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;
                }

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: UDP | Make client receive response from server
« Reply #3 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.
Laurent Gomila - SFML developer

AniCator

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: UDP | Make client receive response from server
« Reply #4 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.  :-\

Ruckamongus

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: UDP | Make client receive response from server
« Reply #5 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.