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

Author Topic: SOLVED - After sending a packet (seems) receiving wrong header...  (Read 5886 times)

0 Members and 1 Guest are viewing this topic.

gnerpo

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Hi all,
i've modified the chat example adding to the packet sended an header (1 client, 2 sender) to identify if the message was sended from the client or the server. Doing this i think is possible to show on the client side only the messages sended from the server ad reverse.

But for strange reason when i send the packet from the client the received header is 2 and not 1...so the message is not filtered and showing on both client and server! Hmmmmmm...

Here is the simple code:

#include <iostream>
#include <windows.h>
#include <SFML/Network.hpp>
#include <SFML/Window.hpp>

const unsigned short PORT = 5000;
const std::string IPADDRESS("192.168.1.71");//change to suit your needs

sf::TcpSocket client_server;

bool quit = false;

sf::Uint16 header_client = 1;
sf::Uint16 header_server = 2;
sf::Uint16 header = 0;

void DoStuff(void){
    while(!quit){
        std::string msg_receive;
        sf::Uint16 header_receive;
        std::string who = "";

        sf::Packet packetReceive;

        client_server.receive(packetReceive);
        if(packetReceive >> header_receive && packetReceive >> msg_receive){
            if( header_receive == header_client ){
                who = "client: ";
            }else{
                who = "server: ";
            }

            std::cout << "header: " << header << std::endl;
            std::cout << "header_receive: " << header_receive << std::endl;

            if( header_receive != header ){
                std::cout << header_receive << " - " << msg_receive << std::endl;
            }
        }
    }
}

void Server(void){
    header = header_server;

    sf::TcpListener listener;
    listener.listen(PORT);
    listener.accept(client_server);
    std::cout << "New client connected: " << client_server.getRemoteAddress() << std::endl;
}

bool Client(void){
    header = header_client;

    if(client_server.connect(IPADDRESS, PORT) == sf::Socket::Done){
        std::cout << "Connected\n";
        return true;
    }
    return false;
}

void GetInput(void){
    std::string msg = "";

    if( sf::Keyboard::isKeyPressed(sf::Keyboard::Left) ){
        msg = "LEFT";
    }
    if( sf::Keyboard::isKeyPressed(sf::Keyboard::Right) ){
        msg = "RIGHT";
    }
    if( sf::Keyboard::isKeyPressed(sf::Keyboard::Up) ){
        msg = "UP";
    }
    if( sf::Keyboard::isKeyPressed(sf::Keyboard::Down) ){
        msg = "DOWN";
    }
    if( sf::Keyboard::isKeyPressed(sf::Keyboard::Escape) ){
        msg = "EXIT";
        quit = true;
    }

    if(msg != ""){
        sf::Packet packet;
        std::cout << "header: " << header << std::endl;
        packet << header << msg;
        client_server.send(packet);
    }

    Sleep(60);
}

int main(int argc, char* argv[]){
    sf::Thread* thread = 0;

    char who;
    std::cout << "Do you want to be a server (s) or a client (c) ? ";
    std::cin  >> who;

    if(who == 's')
        Server();
    else
        Client();

    thread = new sf::Thread(&DoStuff);
    thread->launch();

    while(!quit){
        GetInput();
    }

    if(thread){
        thread->wait();
        delete thread;
    }

    return 0;
}
 
« Last Edit: November 30, 2014, 12:37:08 pm by gnerpo »

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: After sending a packet (seems) receiving wrong header...
« Reply #1 on: November 29, 2014, 11:54:08 am »
Maybe you dont understand how sockets actually work. The "server" is really just an application with an open socket which can accept connections. The "client" is basically the same thing thing except its distributed. Each client has their own socket in which they connect to the server's socket, so the server always knows who is sending data to it. The same thing applies to clients. If you have a p2p thing setup with SFML, the client will know which socket sent them data, be it the server or another client.

As for your problem, it probably has something to do with your threading. You arent synchronizing the data in anyway so theres probably a race condition there. Also you should avoid using global variables.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: After sending a packet (seems) receiving wrong header...
« Reply #2 on: November 29, 2014, 11:54:40 am »
Can you provide the output of the client and server?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

gnerpo

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: After sending a packet (seems) receiving wrong header...
« Reply #3 on: November 29, 2014, 12:28:39 pm »
Maybe you dont understand how sockets actually work. The "server" is really just an application with an open socket which can accept connections. The "client" is basically the same thing thing except its distributed. Each client has their own socket in which they connect to the server's socket, so the server always knows who is sending data to it. The same thing applies to clients. If you have a p2p thing setup with SFML, the client will know which socket sent them data, be it the server or another client.

As for your problem, it probably has something to do with your threading. You arent synchronizing the data in anyway so theres probably a race condition there. Also you should avoid using global variables.

Ok, gambit i understand the problem. The clients arent communicating together so they all communicate with the server and the server will communicate back to all the clients...correct?

But for the problem of the "race" condition, there is no shared piece of code i must protect so what i have to synchronyze? The server shows the correct header=1 sended from the client, but the client recieves a message with the header=2 as if it was sended from the server, but the data were sended only from the client with header=1...

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: After sending a packet (seems) receiving wrong header...
« Reply #4 on: November 29, 2014, 12:51:36 pm »
The server will communicate to clients that it is told to. If you iterate over a list of connections and send a packet to all of them, then the server will communicate to all of them provided that it can reach the socket.

You are sharing your "header*" variables between 2 threads and you arent using a mutex which means you are more than likely getting a race condition because 1 thread is a writer.

Dienes

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: After sending a packet (seems) receiving wrong header...
« Reply #5 on: November 29, 2014, 02:49:58 pm »
You are sharing your "header*" variables between 2 threads and you arent using a mutex which means you are more than likely getting a race condition because 1 thread is a writer.

No synchronization is needed here.

The "header_*" globals do not change and the "header" global is only written to in the main thread before the second thread launches. And even the "quit" global, which is used in both threads (one reading, one writing) does not need a mutex.
« Last Edit: November 29, 2014, 02:51:52 pm by Dienes »

gnerpo

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: After sending a packet (seems) receiving wrong header...
« Reply #6 on: November 29, 2014, 05:49:53 pm »
I really dont know what can i do! I've tried many solutions included the thread elimination...but nothing! I send header=1 from the server and i receive header=2 on the server and header=1 on the client! Ah, network programming...

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Re: AW: After sending a packet (seems) receiving wrong header...
« Reply #7 on: November 29, 2014, 07:24:44 pm »
You could probably start by doing what I've already asked...

Can you provide the output of the client and server?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

gnerpo

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: AW: Re: AW: After sending a packet (seems) receiving wrong header...
« Reply #8 on: November 29, 2014, 08:02:48 pm »
You could probably start by doing what I've already asked...

Can you provide the output of the client and server?

After i press the UP key on the client. I've attached the screenshot: UP image Server, Bottom image Client.

gnerpo

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: After sending a packet (seems) receiving wrong header...
« Reply #9 on: November 29, 2014, 09:07:24 pm »
Is possible that the problem is because i launch the serve and the client on the same machine? Hmmm, now i try to communicate between two different computers...

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: After sending a packet (seems) receiving wrong header...
« Reply #10 on: November 29, 2014, 09:09:23 pm »
Thats not the problem and you communicate the same way.

gnerpo

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: After sending a packet (seems) receiving wrong header...
« Reply #11 on: November 29, 2014, 09:20:16 pm »
Ok, i no more have a header and shared variables but the result is the same...

I've attached output. Top server, bottom client. The command was sended from the client but it's showed on both windows. Also, when i exit the client the server closes too! Why?

Here the code:

#include <iostream>
#include <windows.h>
#include <SFML/Network.hpp>
#include <SFML/Window.hpp>

const unsigned short PORT = 5000;
const std::string IPADDRESS("192.168.1.71");//change to suit your needs

sf::TcpSocket client_server;

bool quit = false;

std::string message_from = "";

void DoStuff(void){
    while(!quit){
        std::string msg_receive;
        sf::Packet packetReceive;

        client_server.receive(packetReceive);
        if(packetReceive >> msg_receive){
            std::cout << message_from << msg_receive << std::endl;
        }
    }
}

void Server(void){
    message_from = "client: ";

    sf::TcpListener listener;
    listener.listen(PORT);
    listener.accept(client_server);
    std::cout << "New client connected: " << client_server.getRemoteAddress() << std::endl;
}

bool Client(void){
    message_from = "server: ";

    if(client_server.connect(IPADDRESS, PORT) == sf::Socket::Done){
        std::cout << "Connected\n";
        return true;
    }
    return false;
}

void GetInput(void){
    std::string msg = "";

    if( sf::Keyboard::isKeyPressed(sf::Keyboard::Left) ){
        msg = "LEFT";
    }
    if( sf::Keyboard::isKeyPressed(sf::Keyboard::Right) ){
        msg = "RIGHT";
    }
    if( sf::Keyboard::isKeyPressed(sf::Keyboard::Up) ){
        msg = "UP";
    }
    if( sf::Keyboard::isKeyPressed(sf::Keyboard::Down) ){
        msg = "DOWN";
    }
    if( sf::Keyboard::isKeyPressed(sf::Keyboard::Escape) ){
        msg = "EXIT";
        quit = true;
    }

    if(msg != ""){
        sf::Packet packet;
        packet << msg;
        client_server.send(packet);
    }

    Sleep(60);
}

int main(int argc, char* argv[]){
    sf::Thread* thread = 0;

    char who;
    std::cout << "Do you want to be a server (s) or a client (c) ? ";
    std::cin  >> who;

    if(who == 's'){
        Server();
    }else{
        Client();
    }

    thread = new sf::Thread(&DoStuff);
    thread->launch();

    while(!quit){
        GetInput();
    }

    if(thread){
        thread->wait();
        delete thread;
    }

    return 0;
}

 


Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: After sending a packet (seems) receiving wrong header...
« Reply #12 on: November 29, 2014, 10:02:10 pm »
SFML tracks keyboard presses even when the program is out of focus.

gnerpo

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: After sending a packet (seems) receiving wrong header...
« Reply #13 on: November 30, 2014, 02:03:00 am »
Ok, executing the client and the server on two different computers everything works fine! So the problem is that sfml tracks keyboard events even out of focus! Thanks Gambit!

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: After sending a packet (seems) receiving wrong header...
« Reply #14 on: November 30, 2014, 02:16:02 am »
Not events, presses. There is a difference.

 

anything