質問

the other "result too large"-question for c++ sockets didn't help that much. I have a server code, but every time i run it, i get a "result too large"-error for the listen()-function. Hope, you can help!

SOCKET sd;
SOCKET sd2;

char serve_1_clien_0_intern;
    struct sockaddr_in server;
    struct sockaddr_in client;

void udp_init(unsigned short port_number, int a1, int a2, int a3, int a4, char serve_1_clien_0 ){

    WSADATA w;                          
    serve_1_clien_0_intern = serve_1_clien_0;



    if (WSAStartup(0x0101, &w) != 0){
        fprintf(stderr, "Could not open Windows connection.\n");
        return;
        exit(0);
    }


    sd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sd == INVALID_SOCKET){
        fprintf(stderr, "Could not create socket.\n");
        WSACleanup();
        return;
        exit(0);
    }

    memset((void *)&server, '\0', sizeof(struct sockaddr_in));


    server.sin_family = AF_INET;
    server.sin_port = htons(port_number);
    {
        server.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a1;
        server.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)a2;
        server.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)a3;
        server.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)a4;
    }


    if(serve_1_clien_0_intern==1){

        if (bind(sd,(struct sockaddr *)&server,sizeof(struct sockaddr_in)) == SOCKET_ERROR){
            fprintf(stderr, "Could not bind name to socket, Maybe wrong IP-ADRESS??\n");
            closesocket(sd);
            WSACleanup();
            return;
            exit(0);
        }


        if(listen(sd,10) == SOCKET_ERROR){
            perror("listen");
            exit(0);
        }

        int sin_size;
        sin_size = sizeof(struct sockaddr_in);
        if ((sd2 = accept(sd, (struct sockaddr *)&client,&sin_size)) == SOCKET_ERROR){
            perror("accept");
            exit(0);
        }
    }   
}
役に立ちましたか?

解決

The problem in your case is that you use the wrong function to print the error. On Windows the socket functions doesn't set errno, so the perror function can't be used.

Instead you have to use WSAGetLastError to get the error code. That is why your error message doesn't really make any sense.


You should probably use WSAGetLastError in the other cases to. For example, bind can fail because of other reasons too.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top