SFML community forums

Help => Network => Topic started by: Merle on January 11, 2017, 11:48:37 pm

Title: Why aren't my computers connecting?
Post by: Merle on January 11, 2017, 11:48:37 pm
I've tried a lot of things, and yet I still can't figure this out. I saw this question was in the FAQ, I saw examples and I saw other peoples questions. The computers aren't even connecting, so I can't use wireshark. My goal was to create  a simple chatroom before I did something a bit more complex. Here's my code:

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

using namespace std;
using namespace sf;

int main() {
    TcpSocket socket;

    string txt = "Connected to ";

    char message[2000];

    cout << "Enter c for client and s for server: ";
    char type, mode;
    cin >> type;

    if (type == 's') {
        TcpListener listener;
        listener.listen(2000);
        listener.accept(socket);
        txt += "Client!";
        mode = 's';
    } else if (type == 'c') {
        socket.connect(IpAddress::LocalHost, 2000);
        txt += "Server!";
        mode = 'r';
    }

    socket.send(txt.c_str(), txt.size() + 1);
    size_t received;
    socket.receive(message, sizeof(message), received);
    cout << txt << endl;

    Packet packet;
    packet << 5;
    socket.send(packet);
    socket.receive(packet);
   
    bool done = false;
    while (!done) {
        if (mode == 's') {
            getline(cin, txt);
            socket.send(txt.c_str(), txt.length() + 1);
            mode = 'r';
        } else if (mode == 'r') {
            socket.receive(message, sizeof(message), received);
            if (received > 0) {
                cout << message << endl;
                mode = 's';
            }
        }
    }
    return 0;
}
 

What can I do to test this? Is my code just bad?
Title: Why aren't my computers connecting?
Post by: eXpl0it3r on January 12, 2017, 03:39:45 am
If you run client and server on two different PCs, then the client shouldn't try and connect to localhost, i.e. itself, but it use the IP address if the other PC.