简体   繁体   中英

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. ("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."

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?).

Here's my code:

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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