cpp中的UDP套接字永远循环

UDP socket in cpp loops for ever

我有一个用 C++ 实现多个线程的代码,它运行良好。其中一个线程是从 UDP 客户端接收消息的 UDP 服务器。太好了。

现在我想在不同的线程上实现 TCP 服务器,以便 UDP 客户端和 TCP 客户端都能够将消息发送到其适当的服务器(它们在不同的端口上 运行)。这样做之后,UDP 服务器会发疯......(我真的不知道如何解释发疯)。请尝试关注我:

最小代码:

// How to compile using mysql.h
// g++ -o aserver aserver.cpp $(mysql_config --libs) -lpthread
//
//// to operate with I/O functions
#include <iostream>
#include <fstream>
// to operate with strings
#include <string>
// to operate with string streams
#include <sstream>
// to opereta with time
#include <time.h>
// to operate with directories
#include <dirent.h>
// to operate with sleep function
#include <unistd.h>
// to operate with threads
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
// to operate with sockets
#include <sys/socket.h>
#include <sys/types.h>
// Defines the structure of the socket
#include <netinet/in.h>
// Uses memset to clear the structure
#include <string.h>
#include <cerrno>

using namespace std;

// **************************************************************
// * GLOBAL VARIABLES                                           *
// **************************************************************
int logto_id;
int udp_port;
int tcp_port;
int sock;

const int success = 0;
const int general_error = -1;
const string general_error_str = "Error";

void logto(string text, int debug_id) {

    int append_status;

    switch (debug_id) {
    case 1:
        cout << text + "\n";
        break;
    case 2:
        break;
    case 3:
        break;
    default:
        cout << "";
    }

}

int create_udp_socket() {

    // UDP Socket Variables
    unsigned int serverlen;
    sockaddr_in udpServer;
    int bind_status = 0;

    string function_name="create_udp_socket: ";

    /* Create the UDP socket */
    sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sock < 0) {
        cout << function_name + "Could not create UDP socket...\n";
        return general_error;
    }

    /* Construct the server sockaddr_in structure */
    memset(&udpServer, 0, sizeof(udpServer));       /* Clear struct */
    udpServer.sin_family = AF_INET;                 /* Internet/IP */
    udpServer.sin_addr.s_addr = htonl(INADDR_ANY);  /* Any IP address */
    udpServer.sin_port = htons(udp_port);           /* server port */

    /* Bind the socket */
    serverlen = sizeof(udpServer);
    bind_status= bind(sock, (struct sockaddr *) &udpServer, serverlen);
    if (bind_status < 0) {
        cout << function_name + "Could not bind UDP socket...\n";
        return general_error;
    } else {
        cout << function_name + "UDP Socket created and binded...\n";
        return success;
    }

}

int create_tcp_socket() {

    // TCP Socket Variables
    unsigned int serverlen;
    sockaddr_in tcpServer;
    int bind_status = 0;
    int listen_status = 0;

    string function_name="create_tcp_socket: ";

    /* Create the TCP socket */
    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock < 0) {
        cout << function_name + "Could not create TCP socket...\n";
        return general_error;
    }

    /* Construct the server sockaddr_in structure */
    memset(&tcpServer, 0, sizeof(tcpServer));       /* Clear struct */
    tcpServer.sin_family = AF_INET;                 /* Internet/IP */
    tcpServer.sin_addr.s_addr = htonl(INADDR_ANY);  /* Any IP address */
    tcpServer.sin_port = htons(tcp_port);           /* server port */

    /* Bind the socket */
    serverlen = sizeof(tcpServer);
    bind_status = bind(sock, (struct sockaddr *) &tcpServer, serverlen);
    if (bind_status < 0) {
        cout << function_name + "Could not bind TCP socket...\n";
        return general_error;
    } else {
        cout << function_name + "TCP Socket created and binded...\n";

        /* Listen */
        listen_status = listen(sock,10);
        if (listen_status < 0) {
            cout << function_name + "Could not listen on the TCP socket...\n";
            return general_error;
        } else {
            cout << function_name + "TCP Socket listening...\n";
            return success;
        }

    }

}

void *thread_udp_server(void *arg) {
    // **************************************************************
    // * LOCAL VARIABLES                                            *
    // * we define this internal variables that before were Global  *
    // **************************************************************

    /* here we store the SQL INSERT query */
    string node_query;
    /* here we find the data to build the query 
     * this variable is always passed by reference to all the functions
     */
    string node_line;
    /* UDP Socket related variables */
    char udp_buffer[255];
    int received = 0;
    unsigned int echolen, clientlen;
    sockaddr_in udpClient;

    // Name of thread
    string thread_name = (char*)arg;

    // We start the whole thing ...
    if (create_udp_socket()==success) {

        /* Endless loop */
        //for(;;) {
        while(1) {
            logto(udp_buffer,logto_id);
            /* Receive a message from the client */
            clientlen = sizeof(udpClient);
            received = recvfrom(sock, udp_buffer, 255, 0, (struct sockaddr *) &udpClient, &clientlen);

            if (received < 0) {

                logto(thread_name + " Failed to receive message",logto_id);
                std::cout << "Something went wrong! errno " << errno << ": ";
                std::cout << strerror(errno) << std::endl;

            } else {

                logto("\n---------\n" + thread_name,logto_id);
                /* We now copy the content of the buffer into 'node_line' */
                node_line=udp_buffer;

                logto(thread_name + node_line,logto_id);

            }

        }

    } else {

        logto(thread_name + " Could not bring up UDP socket...",logto_id);
        std::cout << "Something went wrong! errno " << errno << ": ";
        std::cout << strerror(errno) << std::endl;
        return NULL;

    }

}

void *thread_tcp_server(void *arg) {
    // **************************************************************
    // * LOCAL VARIABLES                                            *
    // * we define this internal variables that before were Global  *
    // **************************************************************

    /* here we store the SQL INSERT query */
    string node_query;
    /* here we find the data to build the query 
     * this variable is always passed by reference to all the functions
     */
    string node_line;
    /* TCP Socket related variables */
    char tcp_buffer[255];
    int recTcp = 0;
    unsigned int echolen, clientlen;
    sockaddr_in tcpClient;

    // Name of thread
    string thread_name = (char*)arg;

    // We start the whole thing ...
    if (create_tcp_socket()==success) {

        /* Endless loop */
        for(;;) {
            logto(tcp_buffer,logto_id);
            /* Receive a message from the client */
            clientlen = sizeof(tcpClient);
            recTcp = accept(sock, (struct sockaddr *) &tcpClient, &clientlen);
            if (recTcp < 0) {

                logto(thread_name + " Failed to receive message",logto_id);
                std::cout << "Something went wrong! errno " << errno << ": ";
                std::cout << strerror(errno) << std::endl;

            } else {

                logto("\n---------\n" + thread_name,logto_id);
                /* We now copy the content of the buffer into 'node_line' */
                node_line=tcp_buffer;

                logto(thread_name + node_line,logto_id);

            }

        }

    } else {

        logto(thread_name + " Could not bring up TCP socket...",logto_id);
        std::cout << "Something went wrong! errno " << errno << ": ";
        std::cout << strerror(errno) << std::endl;

        return NULL;

    }

}

// -----------------
// - main function -
// -----------------
int main () {

    // **************************************************************
    // * VARIABLES                                          *
    // **************************************************************

    // Labels of the threads
    string label_udp = "UDP_thread";
    string label_tcp = "TCP_thread";

    // We define the threads...
    pthread_t udp_server_id=20;
    pthread_t tcp_server_id=50;

    udp_port = 10101;
    tcp_port = 10102;
    logto_id = 1;

    // **************************************************************
    // * START                                                      *
    // **************************************************************

    if ( pthread_create( &udp_server_id, NULL, thread_udp_server, (void*) label_udp.c_str()) ) {
        logto("Error creating thread_udp_server...",logto_id);
        return general_error;
    }

    if ( pthread_create( &tcp_server_id, NULL, thread_tcp_server, (void*) label_tcp.c_str()) ) {
        logto("Error creating thread_tcp_server...",logto_id);
        return general_error;
    }

    if ( pthread_join ( udp_server_id, NULL ) ) {
        logto("UDP_thread couldn't join the main thread...",logto_id);
        return general_error;
    }

    if ( pthread_join ( tcp_server_id, NULL ) ) {
        logto("TCP_thread couldn't join the main thread...",logto_id);
        return general_error;
    }

}

启动程序后,错误号如下,具体取决于启动的套接字:

TCP 正常!:

./aserver
create_tcp_socket: TCP Socket created and binded...
create_tcp_socket: TCP Socket listening...

create_udp_socket: Could not bind UDP socket...
UDP_thread Could not bring up UDP socket...
Something went wrong! errno 22: Invalid argument

UDP 正常!:

./aserver
create_udp_socket: UDP Socket created and binded...
create_tcp_socket: TCP Socket created and binded...
create_tcp_socket: Could not listen on the TCP socket...
TCP_thread Could not bring up TCP socket...
Something went wrong! errno 95: Operation not supported

还有第三种情况,其中 UDP 被启动(TCP 套接字保持关闭)并且为了一些放松,我得到这些消息一直滚动 window...

./aserver
create_tcp_socket: Could not bind TCP socket...
TCP_thread Could not bring up TCP socket...
Something went wrong! errno create_udp_socket: UDP Socket created and binded...

22: UDP_thread Failed to receive message
Something went wrong! errno 107: Transport endpoint is not connectedInvalid argument

UDP_thread Failed to receive message
Something went wrong! errno 107: Transport endpoint is not connected

UDP_thread Failed to receive message
Something went wrong! errno 107: Transport endpoint is not connected

UDP_thread Failed to receive message
Something went wrong! errno 107: Transport endpoint is not connected

但是,如果我注释掉其中一个线程(TCP 或 UDP),剩下的线程可以正常工作...

底线:我无法让两个线程(UDP 和 TCP)同时共存...

任何人都可以给我一个提示。我真的不知道为什么两个线程同时破坏了我的应用程序......:-(

提前致谢,

卢卡斯

看起来您正在为两个线程使用相同的全局套接字。

int sock;

如果 create_udp_socket 函数先运行,它创建的套接字将被 create_tcp_socket 覆盖,反之亦然。

可能的解决方案,要么使用两个全局套接字:

int tcp_sock;
int udp_sock;

或(更好)使 create_xxx_socket 函数 return 套接字直接到调用者,避免使用全局变量。

这是后者的示例(为清楚起见省略了错误处理)。

int create_tcp_socket()
{
    int sock;

    /* Create the TCP socket */
    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    /* Bind and listen... */

    return sock;
}

TCP 线程会像这样调用 create_tcp_socket

void *thread_tcp_server(void *arg)
{
    /* ... */

    int sock = create_tcp_socket();
    if(sock < 0)
    {
        logto(thread_name + " Could not bring up TCP socket...", logto_id);
        return NULL;
    }

    /* Socket created, start accept'ing connections */
}

出于多种原因,全局变量不好。

特别是在多线程代码中,保持数据(sock 在这种情况下)私有意味着对 所有权 的怀疑更少。

代码可能会假设谁拥有全局变量,但随着程序规模的增长,这在实践中变得不可能管理。

将此与其中一种创建方法中的 returning sock 进行对比;很容易看出,最初,sock 属于创建方法。当创建方法 returns 时,套接字的所有权被传递给调用者。永远不会有超过一个函数或线程可以访问套接字,因此并发访问套接字永远不会成为问题。

了解谁拥有数据还可以更轻松地在不再需要资源时释放或解除分配资源。在这种情况下,如果一个服务器线程要退出,它——作为套接字的所有者——将负责在退出时关闭它。它可以安全地这样做,因为没有其他人可以使用套接字。