Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

c - Can I have more than one socket file descriptor per client?

I'm coding a server program and a client program. As the client process can be infinite (it only ends after an interruption), I thought I could create a thread in the client process to disconnect itself when the server process disconnects, so the server could send a message to the client in order to disconnect the client too.

The issue here is that connecting the client to the server only returns one socket file descriptor which can be in use at the time when the server disconnects and use some data in the socket buffer so it would affect negatively to the main purpose of the program so I'd like to use another socket file descriptor to the server for the disconnection thread. How can I do this?

This are the functions I use to connect both processes:

SERVER:

int socketConfig (connection_info cinfo) {

    int socketfd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (socketfd < 0) {
        write(1, "Socket error
", strlen("Socket error
"));
        return -1;
    }

    struct sockaddr_in s_addr;
    memset (&s_addr, 0, sizeof (s_addr));
    s_addr.sin_family = AF_INET;
    s_addr.sin_port = htons(cinfo.port);
    s_addr.sin_addr.s_addr = INADDR_ANY;

    if (bind (socketfd, (void *) &s_addr, sizeof (s_addr)) < 0) {
        write(1, "Bind error
", strlen("Bind error
"));
        return -1;
    }

    listen(socketfd, 3);

    return socketfd;
}

int receiveClient(int serverfd) {
    struct sockaddr_in client;
    socklen_t len = sizeof(client);

    return accept(serverfd, (void *) &client, &len);
}

CLIENT:

int connect_to_server(Config config) {
    struct sockaddr_in client;
    int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    write(1, "Connecting Jack...
", strlen("Connecting Jack...
"));

    if (sockfd < 0) {
        write(1, "Error creating the socket
", strlen("Error creating the socket
"));
        return -1;
    }

    memset(&client, 0, sizeof(client));

    client.sin_family = AF_INET;
    client.sin_port = htons(config.port_jack);

    if (inet_aton(config.ip_jack, &client.sin_addr) == 0) {
        write(1, "Invalid IP address
", strlen("Invalid IP address
"));
        return -1;
    }

    if (connect(sockfd, (void *) &client, sizeof(client)) < 0) {
        write(1, "Error connecting to Jack
", strlen("Error connecting to Jack
"));
        return -1;
    }

    return sockfd;
}
question from:https://stackoverflow.com/questions/65645555/can-i-have-more-than-one-socket-file-descriptor-per-client

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The server does not need to "send a message" when it closes the connection, the TCP/IP stack will do that for you.

Then the client when doing a read will receive 0 bytes to indicate the other side closed the connection. There is no need for a separate socket.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...