无法建立服务器客户端连接- c winsock中错误10061

时间:2020-11-27 18:54:10

I'm trying to run server client on my 2 computers in my local network at home. The first computer is server and the second is client.

我试图在我的本地网络的两台计算机上运行服务器客户端。第一个计算机是服务器,第二个是客户端。

I have error 10061 when I'm trying to connect the server. ("error - connect failed. sockfd is 164, errno is 34, WSA is 10061"). error 10061 means -"connection refused. No connection could be made because the target machine actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host — that is, one with no server application running."

当我试图连接服务器时,我有10061错误。(“错误,连接失败。sockfd是164,errno是34,WSA是10061")。错误10061表示“拒绝连接”。由于目标机主动拒绝连接,所以无法连接。这通常是由于试图连接到国外主机上不活动的服务——即没有服务器应用程序运行的服务。

I thought it might be firewall problem so I approved in my firewall the port Im using, but stil it doesnt work. Also, both computer have the same IP(why is that?).

我认为这可能是防火墙问题,所以我在我的防火墙中批准了我使用的端口,但它没有起作用。而且,两台计算机都有相同的IP(为什么?)

Here's my code:

这是我的代码:

server.c:

server.c:

#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <tchar.h>


int socketBind(int sockfd, int port){
    struct sockaddr_in serv_addr;
    ZeroMemory((char*) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(port);
    if ( bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0 ){
        // we can check errno for exact ERROR
        printf("bind failed with errno %d\n",errno);fflush(NULL);
        return ERROR;
    }
    if ( listen(sockfd, 100) == -1 ){
        return ERROR;
    }
    return 1;
}

int main()
{
    WSADATA wsaData;

        if (WSAStartup(MAKEWORD(2,2), &wsaData) == SOCKET_ERROR) {
            printf ("Error initialising WSA.\n");
            return -1;
        }
    int sockfd; // server's listening socket's descriptor id
    int port = 4997;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    while ( sockfd < 0 ){ // ERROR
        printf("Listener socket creation failed with:%d, errno is %d\n",sockfd,errno);fflush(NULL);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
    }
    if ( socketBind(sockfd, port) == ERROR ){
        printf("Socket bind failed with errno=%d\n",errno);fflush(NULL);
        close(sockfd);
        return ERROR;
    }
    printf("Starting to listen to other USERS!\n");fflush(NULL);
    struct sockaddr_in cli_addr;
    int clilen = sizeof(cli_addr); // length of address
    // accept() returns the socket that will be used for Control Connection with the accepted client
    printf("*************Waiting for other USERS************\n");fflush(NULL);
    int newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
    int readLength;
    char command[(128+1)];
    while(1)
    {
        ZeroMemory(command, sizeof(command));
        readLength = read(newsockfd, command, 128+1);
        if(readLength <= 0)
        {
            continue;
        }
        if(readLength > 0)
        {
            printf(" here should be API's func to command %s", command);fflush(NULL);
        }
        else
        {
            close(sockfd);
            close(newsockfd);
            WSACleanup();
            printf("Read failed with errno:%d\n",errno);fflush(NULL);
            return ERROR;
        }
    }
    close(sockfd);
    close(newsockfd);
    WSACleanup();
    return 1;
}

client.c:

client.c:

#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <tchar.h>

int verifyWrite(int sockfd, char* command){
    int size = strlen(command);
    int i=0, x=0;
    for(i=0;i<size;){
        x = write(sockfd, command, size);
        if(x < 0){
            return ERROR;
        }
        if(x >= 0) {
            i += x;
        }
    }
    return 0;
}

int sendToAll(char* message, int sockfd)
{
    if ( verifyWrite(sockfd, message) < 0 )
    {
        printf("error while sending message\n");fflush(NULL);
    }
    return 0;
}

int main()
{
    WSADATA wsaData;

        if (WSAStartup(MAKEWORD(2,2), &wsaData) == SOCKET_ERROR) {
            printf ("Error initialising WSA.\n");
            return -1;
        }
    int port,sockfd;
    struct sockaddr_in serv_addr;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);       //creating control connection
    while(sockfd < 0){
        printf("error - sockfd = %d\n",sockfd);fflush(NULL);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
    }
    port = 4997;
    ZeroMemory((char*)&serv_addr, sizeof(serv_addr));
    serv_addr.sin_addr.s_addr = inet_addr("192.168.1.20");
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(port);
    while(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0){
        printf("error - connect failed. sockfd is %d, errno is %d, WSA is %d\n",sockfd,errno,WSAGetLastError());fflush(NULL);
    }
    printf("\n opened connection to %s\n", "192.168.1.20");fflush(NULL);

    int i = 0;
    while(i< 6)
    {
        sendToAll("just a message", sockfd);
        i++;
    }
    WSACleanup();

    return 0;
}

1 个解决方案

#1


2  

Your server is listening on port 4997, but your client is connecting to port 80 instead. You would have caught that if you had included the listening and connecting IP:Port pairs in your debug output to the console.

您的服务器正在监听端口4997,但是您的客户机将连接到端口80。如果您将侦听和连接IP:端口对包含在调试输出到控制台的输出中,您就会发现这一点。

#1


2  

Your server is listening on port 4997, but your client is connecting to port 80 instead. You would have caught that if you had included the listening and connecting IP:Port pairs in your debug output to the console.

您的服务器正在监听端口4997,但是您的客户机将连接到端口80。如果您将侦听和连接IP:端口对包含在调试输出到控制台的输出中,您就会发现这一点。