在不等待用户输入的情况下创建服务器套接字连接

Creating server-socket connection without waiting for user input

我的目标是创建用户-服务器连接。最重要的是我不愿意使用线程。 现在,我希望它能像简单的聊天一样工作。我认为问题在于它在一个循环中从一个用户转到另一个用户,等待他们的 getline 输入,因此从技术上讲,一次只有一个用户可以发送消息。我希望我的代码可以连续处理来自一个用户的多条消息,而不必等待所有其他用户发送一条消息 那是服务器内部的 运行 循环:

while (running)
{
    fd_set copy = master;

    // See who's talking to us
    int socketCount = select(0, &copy, nullptr, nullptr, nullptr);

    // Loop through all the current connections / potential connect
    for (int i = 0; i < socketCount; i++)
    {
        // Makes things easy for us doing this assignment
        SOCKET sock = copy.fd_array[i];

        // Is it an inbound communication?
        if (sock == listening)
        {
            // Accept a new connection
            sockaddr_in client;
            int clientSize = sizeof(client);
            SOCKET clientsocket = accept(listening, (sockaddr*)&client, &clientSize);

            // Add the new connection to the list of connected clients
            FD_SET(clientsocket, &master);

            // Send a welcome message to the connected client
            string welcomeMsg = "Welcome to the Awesome Chat Server!\r\n";
            send(clientsocket, welcomeMsg.c_str(), welcomeMsg.size() + 1, 0);
        }
        else // It's an inbound message
        {
            char buf[4096];
            ZeroMemory(buf, 4096);

            // Receive message
            int bytesIn = recv(sock, buf, 4096, 0);
            if (bytesIn <= 0)
            {
                // Drop the client
                closesocket(sock);
                FD_CLR(sock, &master);
            }
            else
            {
                
                // Send message to other clients, and definiately NOT the listening socket

                for (int i = 0; i < master.fd_count; i++)
                {
                    SOCKET outSock = master.fd_array[i];
                    if (outSock != listening && outSock != sock)
                    {
                        ostringstream ss;
                        ss << "SOCKET #" << sock << ": " << buf << "\r\n";
                        string strOut = ss.str();

                        send(outSock, strOut.c_str(), strOut.size() + 1, 0);
                    }
                }
            }
        }
    }
}

我相信这段代码应该可以工作,问题出在我的客户端代码中:

    char buf[4096];
     string userInput;

    do
    {
        // Prompt the user for some text
        cout << "> ";
        getline(cin, userInput); //THATS THE ISSUE, IT WAITS FOR GETLINE
        if (userInput.size() > 0)       // Make sure the user has typed in something
        {
            // Send the text
            int sendResult = send(sock, userInput.c_str(), userInput.size() + 1, 0);
            if (sendResult != SOCKET_ERROR)
            {
                // Wait for response
                ZeroMemory(buf, 4096);
                int bytesReceived = recv(sock, buf, 4096, 0);
                if (bytesReceived > 0)
                {
                    // Echo response to console
                    cout << "SERVER> " << string(buf, 0, bytesReceived) << endl;
                }
            }
        }

    } while (userInput.size() > 0);

我的问题是它是否真的是由 getline 引起的,如果是,如何解决?

您的问题的答案是“是”,但有一个很大的附带条件:设计合理的服务器不应该关心客户端在做什么。您可能想查看 select() or, if you anticipate a large user community, poll()。您不希望 multi-user 服务器依赖于 on/wait 单个客户端。