SFML不同子网中两台计算机之间的网络通信

时间:2017-11-21 15:15:48

标签: c++ sockets networking tcp sfml

好的,所以我想创建一个非常简单的UDP& amp;测试TCP客户端和服务器通信。

void runTcpServer(unsigned short port)
{
    sf::TcpListener listener;

    if (listener.listen(port) != sf::Socket::Done)
        return;
    std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl;

    sf::TcpSocket socket;
    if (listener.accept(socket) != sf::Socket::Done)
        return;
    std::cout << "Client connected: " << socket.getRemoteAddress() << std::endl;

    const char out[] = "JAZDA JAZDAAAAAAAAAAAAAA";
    if (socket.send(out, sizeof(out)) != sf::Socket::Done)
        return;
    std::cout << "Message sent to the client: \"" << out << "\"" << std::endl;

    char in[128];
    std::size_t received;
    if (socket.receive(in, sizeof(in), received) != sf::Socket::Done)
        return;
    std::cout << "Answer received from the client: \"" << in << "\"" << std::endl;
}

void runTcpClient(unsigned short port)
{
    sf::IpAddress server;
    do
    {
        std::cout << "Type the address or name of the server to connect to: ";
        std::cin >> server;
    } while (server == sf::IpAddress::None);

    sf::TcpSocket socket;

    if (socket.connect(server, port) != sf::Socket::Done)
        return;
    std::cout << "Connected to server " << server << std::endl;

    char in[128];
    std::size_t received;
    if (socket.receive(in, sizeof(in), received) != sf::Socket::Done)
        return;
    std::cout << "Message received from the server: \"" << in << "\"" << std::endl;

    const char out[] = "JAZDAAAA JAZDAAA JECHANKA KANKA";
    if (socket.send(out, sizeof(out)) != sf::Socket::Done)
        return;
    std::cout << "Message sent to the server: \"" << out << "\"" << std::endl;
}

void runUdpServer(unsigned short port)
{   // Create a socket to receive a message from anyone
    sf::UdpSocket socket;

    if (socket.bind(port) != sf::Socket::Done)
        return;
    std::cout << socket.getLocalPort() << " connected with this port\n";
    std::cout << "Server is listening to port " << port << ", waiting for a message... " << std::endl;

    char in[128];
    sf::Packet packet_in, packet_out;
    sf::IpAddress sender;

    unsigned short senderPort;
    if (socket.receive(packet_in, sender, senderPort) != sf::Socket::Done)
        return;
    packet_in >> in;
    std::cout << "Message received from client " << sender << ": \"" << in << "\"" << std::endl;

    const char out[] = "JAZDA JAZDAAAAAA";
    packet_out << out;
    if (socket.send(packet_out, sender, senderPort) != sf::Socket::Done)
        return;
    std::cout << "Message sent to the client: \"" << out << "\"" << std::endl;
}

void runUdpClient(unsigned short port)
{
    sf::IpAddress server;
    do
    {
        std::cout << "Type the address or name of the server to connect to: ";
        std::cin >> server;
    } while (server == sf::IpAddress::None);
    sf::Packet packet_out, packet_in;
    sf::UdpSocket socket;
    std::cout << socket.getLocalPort() << "connected with this port \n";

    const char out[] = "JECHANKA KANKA";
    packet_out << out;

    if (socket.send(packet_out, server, port) != sf::Socket::Done)
        return;

    std::cout << "Message sent to the server: \"" << out << "\"" << std::endl;
    char in[128];
    std::size_t received;
    sf::IpAddress sender;
    unsigned short senderPort;

    if (socket.receive(packet_in, sender, senderPort) != sf::Socket::Done)
        return;

    packet_in >> in;
    std::cout << "Message received from " << sender << ": \"" << in << "\"" << std::endl;
}

int main()
{
    const unsigned short port = 50001;
    char protocol;
    std::cout << "Do you want to use TCP (t) or UDP (u)? ";
    std::cin >> protocol;
    char who;
    std::cout << "Do you want to be a server (s) or a client (c)? ";
    std::cin >> who;
    if (protocol == 't')
    {
        // Test the TCP protocol
        if (who == 's')
            runTcpServer(port);
        else
            runTcpClient(port);
    }
    else
    {
        // Test the unconnected UDP protocol
        if (who == 's')
            runUdpServer(port);
        else
            runUdpClient(port);
    }
}

我决定不从代码中删除任何因为我真的不知道究竟问题在哪里。

好处是它在localhost上以各种方式工作(当我输入localhost作为服务器的ip我想连接到的时候),但是当我试图在我的朋友在其他计算机上测试它时它无论如何都不起作用。刚输入服务器客户端的地址后,无法连接到TCP中的服务器,并在UDP客户端发送消息,但服务器没有得到它。我们在Windows防火墙中解锁了端口,并试图完全关闭防火墙。我们还在任何情况下使用外部ip地址(80 ..),在https://www.whatismyip.com上查看。我尝试在数据包和原始数据上进行(如示例所示)。无论如何,它在本地网络中正常工作,但在外部网络中无法正常工作。

0 个答案:

没有答案
相关问题