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

Author Topic: SOLVED - Simple tcp connection with packet send not working..  (Read 2385 times)

0 Members and 1 Guest are viewing this topic.

gnerpo

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
SOLVED - Simple tcp connection with packet send not working..
« on: November 28, 2014, 04:03:35 pm »
Hi people,
i've read the networking and packet tutorial and tried to put everything in a working example.

So, the connection is accepted from the server showing the client ip...but is not showing the packet client have sended.

Here is the client code:

#include <iostream>
#include <SFML/Network.hpp>

int main(int argc, char* argv[])
{
    // ----- The client -----
    sf::TcpSocket socket;

    sf::Socket::Status status = socket.connect("192.168.1.71", 5001);
    if (status != sf::Socket::Done){
        // error...
    }

    sf::Uint16 x = 10;
    std::string s = "hello";
    double d = 0.6;

    sf::Packet packet;
    packet << x << s << d;

    socket.send(packet);

    return 0;
}


An then, the server code:

#include <iostream>
#include <SFML/Network.hpp>

int main(int argc, char* argv[])
{
    // ----- The server -----
    // Create a listener to wait for incoming connections on port 55001
    sf::TcpListener listener;
    listener.listen(5001);
    if (listener.listen(5001) != sf::Socket::Done){
        // error...
    }

    // Wait for a connection
    sf::TcpSocket client;
    if (listener.accept(client) != sf::Socket::Done)
    {
        // error...
}
    std::cout << "New client connected: " << client.getRemoteAddress() << std::endl;
    // Receive a message from the client

    while(true){
        sf::Packet packet;
        client.receive(packet);

        sf::Uint16 x;
        std::string s;
        double d;

        packet >> x >> s >> d;

        if (packet >> x) {
            std::cout << x << std::endl;
        }
    }
    return 0;
}
« Last Edit: November 30, 2014, 12:37:28 pm by gnerpo »

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Simple tcp connection with packet send not working..
« Reply #1 on: November 28, 2014, 04:52:23 pm »
Quote
        packet >> x >> s >> d;

        if (packet >> x) {
            std::cout << x << std::endl;
        }
You're already retrieving x s and d, so when you call "packet >> x" there is nothing to retrieve anymore and the condition is evaluating to false.

Please, use the [code=cpp] tag when posting code here to make it more readable. :)

gnerpo

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Simple tcp connection with packet send not working..
« Reply #2 on: November 28, 2014, 07:00:06 pm »
Ok, thank you! Now it's working. ...but if i try to launch the client program a second time nothig is sended to the server...why?

Networking is a pain!

Here i post the code again with the tag cpp as you suggested:

Server side:
#include <iostream>
#include <SFML/Network.hpp>

int main(int argc, char* argv[])
{
    // ----- The server -----
    // Create a listener to wait for incoming connections on port 55001
    sf::TcpListener listener;
    listener.listen(5001);
    if (listener.listen(5001) != sf::Socket::Done){
        // error...
    }

    // Wait for a connection
    sf::TcpSocket client;
    if (listener.accept(client) != sf::Socket::Done)
    {
        // error...
}
    std::cout << "New client connected: " << client.getRemoteAddress() << std::endl;
    // Receive a message from the client

    while(true){
        sf::Packet packet;
        client.receive(packet);

        sf::Uint16 x;
        std::string s;
        double d;

        if (packet >> x && packet >> s && packet >> d) {
            std::cout << "x: " << x << ", s: " << s << ", d: " << d << std::endl;
        }
    }
    return 0;
}
 

Client side:

#include <iostream>
#include <SFML/Network.hpp>

int main(int argc, char* argv[])
{
    // ----- The client -----
    sf::TcpSocket socket;

    sf::Socket::Status status = socket.connect("192.168.1.71", 5001);
    if (status != sf::Socket::Done){
        // error...
    }

    sf::Uint16 x = 12;
    std::string s = "hello2";
    double d = 0.7;

    sf::Packet packet;
    packet << x << s << d;

    socket.send(packet);

    return 0;
}

 

gnerpo

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Simple tcp connection with packet send not working..
« Reply #3 on: November 28, 2014, 07:14:44 pm »
Ok, i've got it! I must check for new connections before get new data from a new client...