简体   繁体   English

我不知道为什么与套接字相关的非常基本的代码会导致问题

[英]i don't know why a very basic code related to socket is causing problem

It's a socket communication code that I've been using for a long time, but a few days ago, I tried to wrap it up but it didn't work at all.是我用了很久的socket通讯代码,但是前几天想把它包起来,但是根本没用。 I checked setting of the port number of inbound rules but it doesn't work.我检查了入站规则的端口号设置,但它不起作用。 so I'm asking you this question所以我问你这个问题

If setting ip to 127.0.0.1 and run each two files on the same pc, there will be no communication problem.如果将 ip 设置为 127.0.0.1 并在同一台电脑上运行这两个文件,则不会出现通信问题。 But the problem is that if I run the server and the client file on the two different networks, I get 10060 error但问题是,如果我在两个不同的网络上运行服务器和客户端文件,我会得到 10060 错误

I checked the public ip of each pc several times and checked the port setting several times in the inbound rule.我检查了几次每台pc的公共ip,并在入站规则中检查了几次端口设置。 And I thought it was an individual problem with the PC, so I tried it with 6 different PCs, but the results were the same.而且我认为这是个人电脑的问题,所以我尝试了 6 台不同的电脑,但结果是一样的。 It's a very basic code, but I don't know which part has a problem because there is an error.这是一个很基础的代码,但是我不知道哪个部分有问题,因为有错误。

this is my server code and client code这是我的服务器代码和客户端代码

Server服务器

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <WinSock2.h>
#pragma comment(lib,"ws2_32")

#define PORT 10168
#define PACKET_SIZE 1024

using namespace std;

void ConnectSocket();

char* ClientIndex;
SOCKET hc = NULL;
SOCKET hListen;
SOCKADDR_IN tListenAddr = {};

int main()
{
    ClientIndex = (char*)malloc(sizeof(char) * 128);
    ClientIndex = (char*)"123.456.789.123";

    WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);

    
    hListen = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    
    tListenAddr.sin_family = AF_INET;
    tListenAddr.sin_port = htons(PORT);
    tListenAddr.sin_addr.s_addr = htonl(INADDR_ANY);

    
    bind(hListen, (SOCKADDR*)&tListenAddr, sizeof(tListenAddr));
    
    listen(hListen, 20);
    
    for (;;)
    {
        ConnectSocket();

        if (hc != NULL)
        {   
            cout << "success" << endl;
            break;
        }
        Sleep(200);
    }
}

void ConnectSocket()
{
    
    SOCKADDR_IN tCIntAddr = {};
    int iCIntSize = sizeof(tCIntAddr);
    SOCKET hclient = accept(hListen, (SOCKADDR*)&tCIntAddr, &iCIntSize);

    if (!strcmp(inet_ntoa(tCIntAddr.sin_addr), ClientIndex))
    {       
        hc = hclient;
    }
    hclient = NULL;
    return;
}

Client客户

#include "stdio.h"
#include <iostream>
#include <WinSock2.h>
#include "windows.h"
#include <ws2tcpip.h>

#pragma comment(lib,"ws2_32")

#define PORT 10168
#define PACKET_SIZE 1024
#define SERVER_IP "123.456.123.456"

using namespace std;

int main()
{
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);

    SOCKET hSocket;
    hSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
 

    SOCKADDR_IN tAddr = {};
    tAddr.sin_family = AF_INET;
    tAddr.sin_port = htons(PORT);
    tAddr.sin_addr.s_addr = inet_addr(SERVER_IP);

    if (connect(hSocket, (sockaddr*)&tAddr, sizeof(tAddr)) == -1)
        cout << "connect fail : " << GetLastError()  << endl;

}

I solve it.我解决它。 problem is port forwarding setting of router问题是路由器的端口转发设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM