无法连接winsock - c上的错误10049

时间:2014-05-09 13:30:03

标签: c eclipse windows client-server winsock

我试图在我家的本地网络中的两台计算机之间运行服务器客户端。

我在客户端失败了错误10049。我认为它是防火墙的东西,所以我试图运行它我的电脑(带线程),但仍然是同样的问题。 我在功能失败"连接"我的输出:

"错误 - 连接失败。 sockfd是188,errno是34,WSA是10049"

这是我的代码。

app.c:

#include "client.h"
#include "server.h"


int main()
{
    DWORD dwThreadID1, dwThreadID2;

    CreateThread(
                     NULL,         // default security attributes
                     0,            // default stack size
                     (LPTHREAD_START_ROUTINE) apserver,
                     NULL,         // no thread function arguments
                     0,            // default creation flags
                     &dwThreadID1); // receive thread identifier
    CreateThread(
                         NULL,         // default security attributes
                         0,            // default stack size
                         (LPTHREAD_START_ROUTINE) apClient,
                         NULL,         // no thread function arguments
                         0,            // default creation flags
                         &dwThreadID2); // receive thread identifier
    while(1)
    {}

    return 0;
}

server.c:

#include "server.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;
}

DWORD WINAPI apserver(LPVOID lpParam)
{
    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 "client.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;
}

DWORD WINAPI apClient(LPVOID lpParam)
{
    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;
    serv_addr.sin_addr.s_addr = inet_addr("192.168.x.x");
    ZeroMemory((char*)&serv_addr, sizeof(serv_addr));
    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.x.x");fflush(NULL);

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

    WSACleanup();
    return 0;
}

1 个答案:

答案 0 :(得分:2)

Windows套接字错误10049WSAEADDRNOTAVAIL,如果您read about it,您会看到

  

connect,...当远程地址或端口对远程计算机无效时(例如,地址或端口0)。

原因很简单:

serv_addr.sin_addr.s_addr = inet_addr("192.168.x.x");
ZeroMemory((char*)&serv_addr, sizeof(serv_addr));

在设置要连接的地址后清除结构。