简体   繁体   English

Winsock客户端和服务器将无法连接

[英]Winsock client and server wont connect

I have written a client and server using winsock2. 我已经使用winsock2编写了客户端和服务器。 The server seems to run fine but when I run the client it returns the 10038 error code for "Socket operation on nonsocket" immediately. 服务器似乎运行良好,但是当我运行客户端时,它立即返回10038错误代码,表示“套接字在非套接字上的操作”。 Not sure why its doing that at is will create the socket with returning an error. 不知道为什么这样做会创建套接字并返回错误。

Here is the code for the client, It fails on the connect function call: 这是客户端的代码,它在连接函数调用时失败:

#include <stdio.h>
#include <winsock2.h>
#include <time.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int *argc, char **argv){
    struct sockaddr_in server, client;
    char server_reply[2000];
    WSADATA wsa;
    int result = WSAStartup(MAKEWORD(2,2),&wsa);
    SOCKET sock, newSock;
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)  == INVALID_SOCKET)){
        printf("Failed to create socketwith error code : %d" , WSAGetLastError());
    }
    else{
        printf("Socket created\n");
    }
    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons(8080);
    if((newSock = connect(sock , (struct sockaddr *)&server , sizeof(server)) == INVALID_SOCKET )){
        printf("connect failed with error code : %d" , WSAGetLastError());
    }
    else{
        printf("connected");
    }
    closesocket(socket);
    WSACleanup();
    return 0;

}

The code for the server which I believe works: 我认为可以正常工作的服务器代码:

#include<io.h>
#include<stdio.h>
#include<winsock2.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc , char *argv[])
{
    WSADATA wsa;
    SOCKET s , new_socket;
    struct sockaddr_in server , client;
    int c;
    char message[54];


    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }

    printf("Initialised.\n");

    //Create a socket
    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }

    printf("Socket created.\n");

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 8080 );

    //Bind
    if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
    {
        printf("Bind failed with error code : %d" , WSAGetLastError());
        return 1;
    }

    puts("Bind done");

    //Listen to incoming connections
    listen(s , 3);

    //Accept and incoming connection
    puts("Waiting for incoming connections...");

    c = sizeof(struct sockaddr_in);
    while( (new_socket = accept(s , (struct sockaddr *)&client, &c)) != INVALID_SOCKET )
    {   
        recv(s, message, 2000, 0);
        printf(message);

    }

    if (new_socket == INVALID_SOCKET)
    {
        printf("accept failed with error code : %d" , WSAGetLastError());
        return 1;
    }


    closesocket(s);
    WSACleanup();

    return 0;
}

You are setting the parentheses wrong: 您将括号设置错误:

if ((sock = socket(AF_INET, SOCK_STREAM, 0)  == INVALID_SOCKET))

assigns 0 to "sock", you want the following: 将0分配给“袜子”,您需要以下内容:

if ((sock = socket(AF_INET, SOCK_STREAM, 0))  == INVALID_SOCKET)

to have the comparison after the assignment, instead of assigning the result of the comparison. 在分配进行比较,而不是分配比较结果。 This is a matter of operator precedence, == "winning" over = . 这是运算符优先级的问题, == “赢” =

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

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