简体   繁体   中英

Socket not receiving from the client

int main()
{
    CRc5 dec;
    WSADATA wsaData;
    int err;
    if((err =WSAStartup(0x0002, &wsaData)) !=0)
    {
        printf("Init WSAStartup() failed[%d].", err);
        return false;
    }
    //socket structure
    SOCKADDR_IN addr;//addr = socket structure
    int addrlen = sizeof(addr);

    //making the socket
    SOCKET sListen;//listenig to the incoming connections
    SOCKET sConnect;//operating the connection

    //setuping the socket
    sConnect=socket(AF_INET,SOCK_STREAM,NULL);//sock_stream = that the socket is a connection_oriented

    //setup the structure
    addr.sin_addr.s_addr=inet_addr("127.0.0.1");// ip of the connection
    addr.sin_family= AF_INET;
    //seting the prot
    addr.sin_port= htons(9958);

    //sertuping Listen socket
    sListen=socket(AF_INET,SOCK_STREAM,NULL);
    //binding connection
    bind(sListen,(SOCKADDR*)&addr,sizeof(addr));
    //listening 
    listen(sListen,SOMAXCONN);//listing with out any limit
    printf("Attempting Socket Connection\n");
    printf("Wating For An Incoming Connection!\n");
    for(;;)
    {
        if((sConnect=accept(sListen,(SOCKADDR*)&addr,&addrlen)) != INVALID_SOCKET)
        {
            char buf[500];
            int len = strlen(buf);
            recv(sConnect,buf,len,0);

        }
        else
        {
            printf("Error accepting %d\n",WSAGetLastError());
        }
    }
}

but it's not receiving anything it's accepting the socket from the game client and then nothing happend why!!?

         char buf[500];
         int len = strlen(buf);
         recv(sConnect,buf,len,0);

The strlen(buf) is clearly an error. Don't know if that is the reason that recv() doesn't work, but you should definately use sizeof(buf) instead.

You should also know that recv(socket, buf, 500, 0) will not necessarily receive 500 bytes, even if 500 bytes are sent by the sender. It might receive only 1 byte or any number up to 500.

Also it won't necessarily receive everything that the sender sends with a single send() call. A socket is purely a stream and there are no message borders.

I'm just mentioning those two things since they are the "number one mistakes" beginners make with sockets.

{
    if((sConnect=accept(sListen,(SOCKADDR*)&addr,&addrlen)) != INVALID_SOCKET)
    {
        char buf[500];
        int len = strlen(buf);
        recv(sConnect,buf,len,0);

    }

You should probably not re-use addr to find the address of incoming connections. There might not be anything wrong with it, but knowing that you used the same variable for two different things in your program gets harder the larger your program grows. Give each variable a specific task and only reuse variables with very good reason.

But the problem is most likely that strlen(buf) call. Nothing has zeroed the char buf[500] that you allocated on the stack. Your strlen() may return 0 , if there was a 0 byte sitting in that location by accident, or it may return 2000 , if that's how many bytes it has to look through before finding a 0 byte. (I'd guess something like 12 would be common. :) You could use sizeof buf; , but that can be brittle if you change your mind down the road and allocate the array using malloc . So just use a constant for both the length in the allocation and the recv() call. It'll be harder to miss that when you decide to make it dynamic in the future.

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