Winsock只接受来自同一台计算机的连接,但不接受来自同一网络的连接

时间:2017-07-29 10:55:38

标签: c tcp winsock winsock2

所以我一直关注这个指南 http://www.binarytides.com/winsock-socket-programming-tutorial/
要在c中创建一个简单的tcp服务器,这是我提出的代码:
https://pastebin.com/CDxiLv3b

int c;
char client_message[2000];
int recv_size;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
    printf("Failed. Error Code : %d", WSAGetLastError());
    return 1;
}

printf("Initialised.\n");

//Create a socket
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
    printf("Could not create socket : %d", WSAGetLastError());
}

printf("Server Socket created.\n");

memset(&server_addr, 0, sizeof(server_addr));
//Prepare the sockaddr_in structure
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(port);

//Bind
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == SOCKET_ERROR)
{
    printf("Bind failed with error code : %d", WSAGetLastError());
}

printf("Bind to port %d done.\n", port);

//Listen to incoming connections
listen(server_socket, 3);

//Accept and incoming connection
puts("Waiting for incoming reverse shell...");

c = sizeof(struct sockaddr_in);
while ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &c)) != INVALID_SOCKET)
{
    puts("Connection accepted");
    if ((recv_size = recv(client_socket, client_message, 2000, 0)) == SOCKET_ERROR)
    {
        puts("recv failed");
        break;
    }
    client_message[recv_size] = '\0';
    strcpy_s(path, MAX_PATH_LEN, client_message);
    break;
}

if (client_socket == INVALID_SOCKET)
{
    printf("accept failed with error code : %d", WSAGetLastError());
    return 1;
}

return 0;


(是的,我基本上复制和更改了东西)
现在问题是我只能使用相同的连接连接到这个服务器我无法使用网络中的不同计算机连接到它,即使我打开端口(实际上并不重要)它也不会接受来自我的网络上的不同计算机(仅来自同一台计算机)

1 个答案:

答案 0 :(得分:1)

我只需要在防火墙上打开端口