Check connection open or closed ?(in C in Linux)

后端 未结 3 2050
生来不讨喜
生来不讨喜 2021-02-02 18:06

In socket programming in Linux I need to write data in socket but I don\'t know socket is open or close . how to I know that socket is open and close without read ?



        
3条回答
  •  梦如初夏
    2021-02-02 18:51

    I use send() instead write() that handle no signal :

    bzero(buffer, MAX_SIZE_BUFFER);
    n = read(sockfd, buffer, MAX_SIZE_BUFFER - 1);
    printf("after read%d\n", n);
    if (n <= 0)
    {
        break;
    }
    n2 = send(newsockfd, buffer, n, MSG_NOSIGNAL);
    if (n2 == -1)
    {
        close(sockfd);
        close(newsockfd);
        return;
    }
    if (n2 != n)
    {
        break;
    }
    

提交回复
热议问题