同时从服务器发送和接收

时间:2019-04-14 03:41:16

标签: c++ sockets

当前编写代码的方式仅允许在接受输入并发送消息后直接读取来自服务器的消息。但是,此代码用于聊天服务器,并且必须允许在发送消息的任何时间进行读取。

#include <iostream>
#include <string>
#include <cstring>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

#define SERVER_ADDRESS "127.0.0.1"
constexpr int server_port = 15555;

#define SERVER_SUCCESS "1"
#define SERVER_FAILURE "-1"

constexpr int msg_buffer_size = 4096;

int main(int argc, char *argv[])
{
    struct sockaddr_in serv_addr;
    int sock;

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        std::cerr << "Socket creation failed!" << std::endl;
        return 1;
    }

    memset(&serv_addr, '0', sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(server_port);

    if (inet_pton(AF_INET, SERVER_ADDRESS, &serv_addr.sin_addr) <= 0)
    {
        std::cerr << "Invalid server address!" << std::endl;
        return 1;
    }

    if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
    {
        std::cerr << "Failed to connect to chat server!" << std::endl;
        return 1;
    }

    int valread;

    while (true)
    {
        std::cout << ">> ";

        char msg[msg_buffer_size];
        char return_msg[msg_buffer_size];

        std::string input;
        std::getline(std::cin, input);

        if (input == "quit")
            return 0;

        if (input.length() > 4000)
        {
            std::cout << "Input length must be less than 4000 characters." << std::endl;
            continue;
        }

        strcpy(msg, input.c_str());
        if (send(sock, msg, strlen(msg), 0) < 0)
        {
            std::cout << "Error sending data." << std::endl;
            continue;
        }

        if (recv(sock, return_msg, msg_buffer_size, 0) < 0)
        {
            std::cout << "Error receiving data." << std::endl;
            continue;
        }

        std::string code(strtok(return_msg, " "));
        if (code == SERVER_FAILURE)
            std::cout << "Failure: " << strtok(NULL, "") << std::endl;
        else
            std::cout << strtok(NULL, "") << std::endl;

        memset(msg, 0, msg_buffer_size);
        memset(return_msg, 0, msg_buffer_size);
    }

    std::cout << "Exiting." << std::endl;
    close(sock);

    return 0;
}

从服务器发送消息后,允许客户端立即接收消息的正确方法是什么?我当时正在考虑创建一个线程,但由于要在两个地方接收,所以似乎有点多余。

0 个答案:

没有答案