永久

时间:2015-10-27 02:52:44

标签: c++ multithreading udp tcpclient udpclient

我有一个代码在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;
    }

}

启动程序后,errno是以下内容,具体取决于启动的套接字:

TCP ok!:

./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 ok!:

./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)同时共存......

任何人都可以给我一个暗示。我真的迷失了为什么两个线程同时破坏了我的应用......: - (

提前致谢,

卢卡斯

1 个答案:

答案 0 :(得分:3)

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

Person* newPerson = new Person(name, age, location);
LL.insert(newPerson);

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

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

create_tcp_socket

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

以下是后者的示例(为清晰起见,省略了错误处理)。

create_xxx_socket

TCP线程会像这样调用int create_tcp_socket() { int sock; /* Create the TCP socket */ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); /* Bind and listen... */ return sock; }

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归创建方法所有。当创建方法返回时,套接字的所有权将传递给调用者。永远不会有多个函数或线程可以访问套接字,因此对套接字的并发访问绝不是问题。

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