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

Author Topic: Why aren't my computers connecting?  (Read 1724 times)

0 Members and 1 Guest are viewing this topic.

Merle

  • Newbie
  • *
  • Posts: 17
    • View Profile
Why aren't my computers connecting?
« 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10829
    • View Profile
    • development blog
    • Email
Why aren't my computers connecting?
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/