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

Author Topic: Why isn't connecting to my server working?  (Read 1720 times)

0 Members and 1 Guest are viewing this topic.

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
Why isn't connecting to my server working?
« on: November 14, 2013, 06:26:25 pm »
It's giving me the error message I made "Could not connect"

Here's the code:

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

using namespace std;

const unsigned short port = 33981;

void Client(sf::TcpSocket& socket, sf::IpAddress& targetIP)
{
        if(socket.connect(targetIP, port) == sf::Socket::Done)
                cout << "Connected to ip adress " << targetIP << " ." << endl;
        else
                cout << "Could not connect";
}

void Server()
{
        sf::TcpListener listener;
        if(listener.listen(port) != sf::Socket::Done)
                cout << "Could not listen to port " << port << " ." << endl;
        else
                cout << "Listening to port " << port << " ." << endl;
}

int main(int argc, char* argv[])
{
        sf::TcpSocket socket;
        sf::TcpListener listener;
        sf::IpAddress targetIP("71.3.101.232");
        sf::Packet packetSend;
        sf::Packet packetGet;
        int input;

        string messageSend;
        string messageGet;

        cout << "Client or Server" << endl
                 << "1): Client" << endl
                 << "2): Server" << endl
                 << ">: ";
        cin >> input;

        if(input == 1)
                Client(socket, targetIP);
        else if(input == 2)
                Server();
        else
                return 0;
       
        while(true);
       
        cin.get();

        return 0;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Why isn't connecting to my server working?
« Reply #1 on: November 14, 2013, 07:11:32 pm »
Your Server() function creates a listener, starts listening, then immediately destroys it (because it goes out of scope), leaves and falls into the endless empty while loop.
Laurent Gomila - SFML developer