简体   繁体   English

无法建立服务器客户端连接 - 错误10061在C的winsock

[英]cannot establish server client connection - error 10061 in c winsock

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. 尝试连接服务器时出现错误10061。 ("error - connect failed. sockfd is 164, errno is 34, WSA is 10061"). (“错误-连接失败。sockfd为164,errno为34,WSA为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的意思是-“连接被拒绝。由于目标计算机主动拒绝了连接,所以无法建立连接。这通常是由于尝试连接到外部主机上处于非活动状态的服务(即没有运行服务器应用程序的服务)导致的。”

I thought it might be firewall problem so I approved in my firewall the port Im using, but stil it doesnt work. 我认为这可能是防火墙问题,所以我在防火墙中批准了Im正在使用的端口,但仍然无法正常工作。 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;
}

Your server is listening on port 4997, but your client is connecting to port 80 instead. 您的服务器正在侦听端口4997,但您的客户端连接到端口80来代替。 You would have caught that if you had included the listening and connecting IP:Port pairs in your debug output to the console. 你本来是可以赶上,如果你已经包括了听力和连接IP:在你的调试输出到控制台端口对。

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

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