sfml服务器将无法连接

时间:2013-08-02 16:31:18

标签: c++ sfml

我正在尝试学习sfml,下面的代码不是我自己的。稍后我会写自己的。现在我需要知道为什么连接没有连接到我真正的ip,但是当我使用我的本地ip时工作。

这是我的代码

#include<SFML/Graphics.hpp>
#include<SFML/Network.hpp>
#include<iostream>
#include<stdlib.h>
#include<ctime>

int main()
{
    sf::IpAddress ip = "142.187.215.01"; // works with 127.0.0.1 or sf::IpAddress::localhost
    std::cout << ip;
    sf::TcpSocket socket; 
    char connectionType;
    std::string text = "";
    char buffer[2000], mode = 'r';
    std::size_t received;

    std::cout << "Enter (s) for Server, Enter (c) for Client: "; 
    std::cin >> connectionType;

    if(connectionType == 's')
    {
        sf::TcpListener listener;
        listener.listen(2000);
        listener.accept(socket);
        text += "Server";
        mode = 's';
    }
    else if(connectionType == 'c')
    {
        socket.connect(ip, 2000);
        text += "Client";
    }

    socket.send(text.c_str(), text.length() + 1);
    socket.receive(buffer, sizeof(buffer), received);

    std::cout << buffer << std::endl;

    bool done = false; 

    while(!done)
    {
        if(mode == 's')
        {
            std::getline(std::cin, text);
            socket.send(text.c_str(), text.length() + 1);
            mode = 'r';
        }
        else if(mode == 'r')
        {
            socket.receive(buffer, sizeof(buffer), received); 
            if(received > 0)
            {
                std::cout << "Received: " << buffer << std::endl; 
                mode = 's';
            }
        }
    }
    return 0; 
}

0 个答案:

没有答案