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
954 views
in Technique[技术] by (71.8m points)

asynchronous - C - Understanding Client/Server Chat ioctl-function

in university we have the task to program a Chatserver(IPv6) and a Client. It works already, but I have the issue that one side(server,client) can't send multiple messages one after another, because the server, for example, has to receive the clients message before it can send. Our professor told us to use select() and ioctl() function to solve this issue but I don't understand how this is supposed to work. In which order do I have to use select(), ioctl() etc.?

In my understanding the select() would always listen on the socket. As soon as the clients sends a message, select is triggered. But it won't work when the gets-function is waiting for input and blocks the program. Thanks for advice! PS: Threading is not allowed

Server-Code

while (1) {

    FD_ZERO(&fdSet); //fdSet is an Array of Sockets - FD_Zero initalizes them with 0
    FD_SET(ListenSocket, &fdSet); //add the socket which listens and awaits connections from client

        if (client != INVALID_SOCKET) {
            FD_SET(client, &fdSet); // Add client socket also
        }


        //wait for something to happen on any of the sockets in fdSet, last parameter indicates the timeout (NULL means wait indefinitely)
        iResult = select(0, &fdSet, NULL, NULL, NULL);
        if (iResult == SOCKET_ERROR) {
            printf("Error on select.
");
            exit(1);
        }


    
        //check, if ListenSocket is in fdSet (fdSet is an Array of Sockets)
        if (FD_ISSET(ListenSocket, &fdSet)) {
            if (client == INVALID_SOCKET) {
                client = accept(ListenSocket, NULL, NULL);
                printf("New client connection accepted.
");
                //break;
            }
        }

        if (FD_ISSET(client, &fdSet)) {
            iResult = recv(client, &message_cli, sizeof(message_cli), 0); //read the incoming message from the client
            //if recv returns 0, the client has closed the connection
            if (iResult == 0 || iResult == SOCKET_ERROR) {
                printf("Client %d has disconnected
", client);
                closesocket(client); //close the socket   
                client = INVALID_SOCKET; //make room for new clients
            }
            else {
                //add null character to printf message in buffer

                printf("%s > %s
", &message_cli.sNumber, &message_cli.txt);
                printf("%s > ", &message_serv.sNumber);
                gets(&message_serv.txt);



                iResult = send(client, &message_serv, sizeof(message_serv), 0);
                if (iResult == SOCKET_ERROR) {
                    printf("Send failed: %d
", WSAGetLastError());
                    closesocket(client);
                    WSACleanup();
                    exit(1);


                }


            }

Client-Code

  while (1) {
       
        printf("%s> ", &message_cli.sNumber);
        gets(&message_cli.txt);


      

        iResult = send(ClientSocket, &message_cli, sizeof(message_cli), 0);
        if (iResult == 0 || iResult == SOCKET_ERROR) {
            printf("Server has disconnected.
");
            closesocket(ClientSocket);
            WSACleanup();
            exit(1);
        }


        iResult = recv(ClientSocket, &message_serv, sizeof(message_serv), 0);
                if (iResult > 0) {
     
                    printf("%s> %s
",&message_serv.sNumber, &message_serv.txt);
            
                }
      
    }
    closesocket(ClientSocket);
    WSACleanup();
    return 0;
}
question from:https://stackoverflow.com/questions/65877611/c-understanding-client-server-chat-ioctl-function

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

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

...