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

Author Topic: Packet transmit wrong data (?)  (Read 4695 times)

0 Members and 1 Guest are viewing this topic.

HaS3n

  • Newbie
  • *
  • Posts: 13
    • View Profile
Packet transmit wrong data (?)
« on: March 09, 2022, 05:54:57 pm »
Hi, I would like to do an online chess for only 2 players, however, packet with connectionstatus variable seems like not changing connectionstatus on client side. Where is the problem? I can't find it...
1st attachment is client side
2nd is server side
Take a note that the whole code will be reworked, this is only a test but i cannot get further without solving this now.
TIA

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11028
    • View Profile
    • development blog
    • Email
Re: Packet transmit wrong data (?)
« Reply #1 on: March 10, 2022, 07:11:35 pm »
You need to check the status returned by bind() to ensure that it was successful.
Then you also need to handle certain failure states instead of simply terminating the application.

Binding a port may take a bit of time, so when you immediately respond, the client might not be ready yet and it would seem like things fail.

Also don't forget all the fun topics like NAT and port forwarding, when it comes to internet connections: https://www.sfml-dev.org/faq.php#network-internet-network

I highly recommend to use Wireshark, to see what's going on on the wire, that way you'd at least know if/when stuff is or isn't sent.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

HaS3n

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Packet transmit wrong data (?)
« Reply #2 on: March 10, 2022, 08:57:14 pm »
You need to check the status returned by bind() to ensure that it was successful.
Then you also need to handle certain failure states instead of simply terminating the application.

Binding a port may take a bit of time, so when you immediately respond, the client might not be ready yet and it would seem like things fail.

Also don't forget all the fun topics like NAT and port forwarding, when it comes to internet connections: https://www.sfml-dev.org/faq.php#network-internet-network

I highly recommend to use Wireshark, to see what's going on on the wire, that way you'd at least know if/when stuff is or isn't sent.
For now, server and client are on the same computer.
Also, the code will be totally reworked to its final form, but thats my test, because this is my first time with SFML network.
I will check wireshark and this binding problem and I will update this topic.

HaS3n

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Packet transmit wrong data (?)
« Reply #3 on: March 10, 2022, 10:28:39 pm »
okay, i found one problem, server added data to the packet without cleaning it so the reply packet contained something like this (photo 1). I have fixed it, but it didn't fix the problem.
Client sends message (photo 2) and then server replies with correct data (photo 3), but then client just..ignores it? its not updating its variable (connectionstatus) so it remains 0 (I have added setting variable to 0 and then updating it with received packet, but it remains 0 for some reason... Server however is working perfectly fine for me).

Client code here:
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <windows.h>
#include "TextureHolder.h"

int main()
{      
       
        sf::UdpSocket socket;
        sf::IpAddress sendingip;
        unsigned short sendingport;
        std::cout << "IP: ";
        std::cin >> sendingip;
        std::cout << "Port: ";
        std::cin >> sendingport;
        unsigned int receivingport = socket.getLocalPort();
        if (socket.bind(receivingport) != sf::Socket::Done) {
                std::cout << "Failed to bind a socket!" << std::endl;
                exit(4);
        }
        sf::Packet initpacket;
        int connectionstatus = 1; //0-brak 1-proba 2-nawiazana

       
        initpacket << connectionstatus;
        if (socket.send(initpacket, sendingip, sendingport) != sf::Socket::Done) {
                std::cout << "Failed to send connection initialization packet!" << std::endl;
                exit(2);
        }
       
        sf::IpAddress senderip;
       
        unsigned short senderport;
        Sleep(1000);
        if (socket.receive(initpacket, senderip, senderport) != sf::Socket::Done) {
                std::cout << "Failed to recieve initialization packet!" << std::endl;
                exit(2);
        }
        connectionstatus = 0;
        if (!initpacket >> connectionstatus) {
                std::cout << "Failed to read initpacket!" << std::endl;
                exit(2);
        }
        std::cout << "CONNSTATUS: " << connectionstatus << std::endl;
        if (connectionstatus != 2) {
                std::cout << "Connection not stablilized!" << std::endl;
                exit(3);
        }
       
        //inicjalizacja wszystkiego
        TextureHolder TXHOLDER;
        sf::RenderWindow _window(sf::VideoMode(800, 800 ), "Chess", sf::Style::Titlebar | sf::Style::Close);
        _window.setFramerateLimit(75);
        sf::Sprite bG;
        bG.setTexture(TXHOLDER.bg);
        sf::Event event;

        while (_window.isOpen()) {

                while (_window.pollEvent(event)) {

                        if (event.type == sf::Event::Closed) {

                                _window.close();
                        }






                }


                _window.clear(sf::Color::White);
                _window.draw(bG);
                _window.display();
        }//isopen

        return 0;
}

Server code here:
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <windows.h>
int main()
{      
        int connectionstatus = 0;
        sf::Packet initpacket;
        sf::UdpSocket socket1;
        unsigned int receivingport = 57000;
        unsigned short senderport;
        sf::IpAddress senderip;
        if (socket1.bind(receivingport) != sf::Socket::Done) {
                std::cout << "Failed to bind a socket!" << std::endl;
                exit(4);
        }
        if (socket1.receive(initpacket, senderip, senderport) != sf::Socket::Done) {
                std::cout << "Failed to recieve initialization packet!" << std::endl;
                exit(2);
        }
        initpacket >> connectionstatus;
        std::cout << connectionstatus << std::endl;
        std::cout << senderip<<"||"<<senderport<<std::endl;
        if (connectionstatus == 1) {
                connectionstatus = 2;
                initpacket.clear();
                initpacket << connectionstatus;
                if (socket1.send(initpacket, senderip, senderport) != sf::Socket::Done) {
                        std::cout << "Failed to send connection initialization packet!" << std::endl;
                        exit(2);
                }
        }
        system("pause");
        return 0;
}

HaS3n

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Packet transmit wrong data (?)
« Reply #4 on: March 12, 2022, 12:38:56 pm »
Okay problem fixed, the topic can be closed
The mistake was here ->> if (!initpacket >> connectionstatus)
combined with not clearing the packet before giving him next data.