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

Author Topic: Try to use UDP.  (Read 4332 times)

0 Members and 1 Guest are viewing this topic.

djmentos

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • http://www.krolnet.pl/
Try to use UDP.
« on: September 18, 2010, 08:39:08 pm »
Hi.

I try to make client and server on UDP.

Client:
Code: [Select]

    int ServPort = 1337; //Port Servera
    IPAddress ServIP("127.0.0.1"); //IP Servera, moze byc HOST

    if (!ServIP.IsValid()){
        cout << "Niepoprawny adres serwera" << endl;
    }

    SocketUDP Client;
    char Message[] = "Hi, I'm a client !";

    if (Client.Send(Message, sizeof(Message), ServIP, ServPort) != Socket::Done)
        cout << "Pierdolony error!" << endl;
    else
        cout << "Message sent to server : \"" << Message << "\"" << endl;

    Client.Close();


and Server:
Code: [Select]

IPAddress SenderIP;
unsigned short SenderPort;

    while (true)
    {
        unsigned int NbSockets = Selector.Wait();

        for (unsigned int i = 0; i < NbSockets; ++i)
        {
            SocketUDP Socket = Selector.GetSocketReady(i);

            sf::Packet Packet;
            if (Socket.Receive(Packet, SenderIP, SenderPort) == sf::Socket::Done)
            {
                // Extract the message and display it
                float    x;
                float    y;
                Packet >> x >> y;
                std::cout << SenderIP << ":"<< SenderPort << " " << x << " " << y << std::endl;
            }
            else
            {
                // Error : we'd better remove the socket from the selector
                Selector.Remove(Socket);
            }
        }

    }


And server dont gets a message from client. Whats wrong?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Try to use UDP.
« Reply #1 on: September 18, 2010, 11:18:43 pm »
You don't need a selector with UDP sockets. A UDP socket is not connected, it can receive from any client which means that it will always receive the first message available (which is what a selector does with TCP sockets).
Laurent Gomila - SFML developer

djmentos

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • http://www.krolnet.pl/
Try to use UDP.
« Reply #2 on: September 19, 2010, 11:42:24 am »
So now it looks:
Code: [Select]

 SocketUDP Server;

    if (!Server.Bind(1337))
    {
        cout << "Pierdolony error!" << endl;
    }

    cout << "Init Zayebista Server" << endl;

    IPAddress SenderIP;
    unsigned short SenderPort;

    while (true)
    {
        sf::Packet Packet;
        if (Server.Receive(Packet, SenderIP, SenderPort) == sf::Socket::Done)
        {
            // Extract the message and display it
            float    x;
            float    y;
            Packet >> x >> y;
            std::cout << SenderIP << ":"<< SenderPort << " " << x << " " << y << std::endl;
        }

    }

    Server.Close();

And server dont get any message from client.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Try to use UDP.
« Reply #3 on: September 19, 2010, 11:54:39 am »
It would be easier with a complete code. Is your server socket bound to the port 1337 (nice port choice, by the way)?

I see that your client code is inspired from the sockets example, now that you don't use a selector anymore you should do the same with the server code.
Laurent Gomila - SFML developer

djmentos

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • http://www.krolnet.pl/
Try to use UDP.
« Reply #4 on: September 19, 2010, 12:06:39 pm »
Ok, updated code in upper post. ITs whole main()

andbal

  • Newbie
  • *
  • Posts: 3
    • View Profile
Try to use UDP.
« Reply #5 on: September 19, 2010, 04:10:16 pm »
Check topic named
Quote
[SOLVED] Server and client

User purkskis have posted an example

djmentos

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • http://www.krolnet.pl/
Try to use UDP.
« Reply #6 on: September 19, 2010, 07:54:23 pm »
Thanks, it's work.

But now i have another question to UDP. In case of UDP dont have 'connections', client need to have public IP or is it any way to resend to him a packet from server?

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Try to use UDP.
« Reply #7 on: September 20, 2010, 09:27:50 am »
UDP has no connection. This is not meaning clients have no IP Adress. It's just that the protocol is not making a connection and can loose packet.

When you want to resend a packet, the UDP protocol is giving you the adresse of the client, it's easy to resend the packet to it.
But you can't keep the IPs and resend to each client, because you are not sure that the client is "on" (till there is no connection).

Use TCP if you want connections, or modify your own protocol to make a Connected UDP ;)

By the way : open an other thread if you want to discuss about other subjects. It will be easier for readers having the same problem ;)
Mindiell
----

 

anything