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

时间:2014-05-10 22:25:14

标签: c eclipse windows client-server winsock

我正在尝试在家里的本地网络中的2台计算机上运行服务器客户端。 第一台计算机是服务器,第二台是客户端。

我正在尝试连接服务器时出现错误10061。 (“错误 - 连接失败.sockfd是164,errno是34,WSA是10061”)。 错误10061表示 - “连接被拒绝。无法建立连接,因为目标计算机主动拒绝它。这通常是因为尝试连接到外部主机上不活动的服务 - 即没有运行服务器应用程序的服务。”

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

这是我的代码:

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:

#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 个答案:

答案 0 :(得分:2)

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