简体   繁体   中英

Socket send from server failing, error: Socket operation on non-socket

I have below functions written for a server side socket implementation -

void SetUpServerSocket()
{
    /* Setting up Server socket*/
    struct sockaddr_in srv_addr;
    int yes=1;

    /* Open datagram socket */
    if((bind_my_socket = socket(AF_INET,SOCK_STREAM,0)) < 0)
    {
        print_log("Bind socket failed");
    return ;
    }

    bzero((char*) &srv_addr,sizeof(srv_addr));
    srv_addr.sin_family = AF_INET;
    srv_addr.sin_addr.s_addr = inet_addr(my_ip); 
    srv_addr.sin_port = htons(my_port);

    if(setsockopt(bind_my_socket,SOL_SOCKET,SO_REUSEPORT,&yes,sizeof(yes))!=0)
    {
        print_log("Can't set socket options'SO_REUSEPORT'");
        return;
    }
    if(bind(bind_my_socket,(struct sockaddr*) &srv_addr,sizeof(srv_addr)) < 0)
    {
        print_log("Bind failed");
        return ;
    }
    if (listen (bind_my_socket,1024) < 0)
    {
        print_log("Cannot listen on socket\n");
        return;
    }
}

void* AcceptMsgConnFromClient()
{
    /*This will accept connection from client*/
    struct sockaddr_in client_addr;
    int client_addr_len, r;
    my_socket=0;
    client_addr_len=sizeof(client_addr);
    r=accept(bind_my_socket,(struct sockaddr *)&client_addr,&client_addr_len);
    my_socket=r;
    return NULL;
}

void* ReceiveClientMsg()
{
    /* This will accept messages from client */
    u_char buf[2000];
    struct sockaddr_in  client_addr;
    int client_addr_len;
    int len;
    pthread_t thread;
    int connfd;
    u_char* pCmd[100];

    AcceptMsgConnFromClient();

    //Working here
    /*if(my_socket>0){
        if(send(my_socket, "Server Hello", strlen("Server Hello"), 0) < 0)
        {
            print_log("Send data failed\n");
            print_log(strerror(errno));
            print_log("\n");
        }
    }*/

    memset(buf,0,2000);

    for(;;)
    {
        len=recvfrom(my_socket,buf,2000,0,(struct sockaddr *)&client_addr,&client_addr_len);
        if(len<=0)
        {
            AcceptMsgConnFromClient();
            continue;
        }
        //my logic to handle buffer
    } // ;; for
    usleep(1000);
    close(bind_my_socket);
    return NULL;
}

void SendMsgtoClient()
{
    if(send(my_socket, "Server Hello to Client", strlen("Server Hello to Client"), 0) < 0)
    {
        print_log("Send data to client failed\n");
        print_log(strerror(errno));
        print_log("\n");
        return;
    }
}

From main(in different file) I've called 'SetUpServerSocket()' once and 'ReceiveClientMsg()' in a thread(required to keep processing the buffer). From a different function I'm calling 'SendMsgtoClient()' whenever I want to send something to Client. However, this fails with error as 'Socket operation on non-socket'.

But when I send something on same my_socket from ReceiveClientMsg thread, it works, and not when I do it from another function. I tried and printed value of my_socket in SendMsgtoClient(), it has an integer which is same as what is being assigned from AcceptMsgConnFromClient().

I have declared the socket variables used as-

static int bind_my_socket = 0, my_socket = 0;

Any help would be appreciated, thank you!

You're not printing the correct error message. The first call to print_log() is likely to change errno , so you're printing that error message, not the error from send() . Save errno in another variable.

void SendMsgtoClient()
{
    if(send(my_socket, "Server Hello to Client", strlen("Server Hello to Client"), 0) < 0)
    {
        int saved_errno = errno;
        print_log("Send data to client failed\n");
        print_log(strerror(saved_errno));
        print_log("\n");
        return;
    }
}

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