cpp循环中的UDP套接字永远存在

UDP socket in cpp loops for ever

本文关键字:套接字 永远 存在 UDP 循环 cpp      更新时间:2023-10-16

我有一段代码在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套接字保持关闭),出于某种原因,我看到这些消息在窗口中滚动。。。

./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)同时生活在一起

有人能给我一个提示吗。我真的不明白为什么两个线程同时破坏了我的应用程序…:-(

提前感谢

Lucas

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

int sock;

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

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

int tcp_sock;
int udp_sock;

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

下面是后者的一个示例(为了清楚起见,省略了错误处理)。

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)私有意味着对所有权的怀疑较少。

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

将此与从其中一种创建方法返回sock进行对比;很容易看出,最初sock由创建方法所有。当创建方法返回时,套接字的所有权将传递给调用者。从来没有超过一个函数或线程可以访问套接字,因此对套接字的并发访问从来都不是问题。

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