c++ 中的 tcp 服务器中的多线程

发布于 2024-12-26 05:06:12 字数 2443 浏览 0 评论 0原文

我已经用 C++ 为多线程 tcp 服务器编写了这个类 .. 并通过 :: 进行编译

g++ -o server server.cpp -lpthread

,但我收到以下错误 ::

invalid conversion from "void*" to "void* (*)(void*)"
initializing argument 3 of "int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)"

我应该做什么?我的代码::

#include "PracticalSocket.h" 
#include <iostream>          
#include <cstdlib>           
#include <pthread.h>         
using namespace std;

class MultiThreadedServer{

private:
static const int RCVBUFSIZE = 1024;
string agent_ip;
int agent_port;

public:

string startServer(unsigned short port, string agentIP, int agentPort)
{
agent_ip = agentIP;
agent_port=agentPort;

try 
{
    TCPServerSocket servSock(port);   // Socket descriptor for server  

    for (;;) 
    {   
        // Create separate memory for client argument  
        TCPSocket *clntSock = servSock.accept();  
        pthread_t threadID;              
        if (pthread_create(&threadID, NULL, (void*) &ThreadMain,(void *) &clntSock) != 0) 
        {
            cerr << "Unable to create thread" << endl; 
            exit(1);
        }

    }
} 
catch (SocketException &e)
{
    cerr << e.what() << endl;
    exit(1);
}
// NOT REACHED
}


// TCP client handling function
void static HandleTCPClient(TCPSocket *sock) 
{
cout << "Handling client ";
try 
{
    cout<<"Foreign address: "<< sock->getForeignAddress() << ":";
} 
catch (SocketException &e) 
{
    cerr << "Unable to get foreign address" << endl;
}
try 
{
    cout<<"Foreign port: "<< sock->getForeignPort();
} 
catch (SocketException &e)
{
    cerr << "Unable to get foreign port" << endl;
}
cout << " with thread " << pthread_self() << endl;

char echoBuffer[RCVBUFSIZE];
int recvMsgSize;
while ((recvMsgSize = sock->recv(echoBuffer, RCVBUFSIZE)) > 0) 
{
    cout<<"echoBuffer::::"<<echoBuffer;
    //sock->send(echoBuffer, recvMsgSize);
}
   // Destructor closes socket
 }

 static void ThreadMain(void *clntSock)
 {
// Guarantees that thread resources are deallocated upon return  
pthread_detach(pthread_self()); 

// Extract socket file descriptor from argument  
HandleTCPClient((TCPSocket *) clntSock);

delete (TCPSocket *) clntSock;
//return NULL;
 }



 };

I have written this class for Multithreaded tcp server in C++ .. and compile by ::

g++ -o server server.cpp -lpthread

BUT I get the following errors ::

invalid conversion from "void*" to "void* (*)(void*)"
initializing argument 3 of "int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)"

What should I do ?? My Code ::

#include "PracticalSocket.h" 
#include <iostream>          
#include <cstdlib>           
#include <pthread.h>         
using namespace std;

class MultiThreadedServer{

private:
static const int RCVBUFSIZE = 1024;
string agent_ip;
int agent_port;

public:

string startServer(unsigned short port, string agentIP, int agentPort)
{
agent_ip = agentIP;
agent_port=agentPort;

try 
{
    TCPServerSocket servSock(port);   // Socket descriptor for server  

    for (;;) 
    {   
        // Create separate memory for client argument  
        TCPSocket *clntSock = servSock.accept();  
        pthread_t threadID;              
        if (pthread_create(&threadID, NULL, (void*) &ThreadMain,(void *) &clntSock) != 0) 
        {
            cerr << "Unable to create thread" << endl; 
            exit(1);
        }

    }
} 
catch (SocketException &e)
{
    cerr << e.what() << endl;
    exit(1);
}
// NOT REACHED
}


// TCP client handling function
void static HandleTCPClient(TCPSocket *sock) 
{
cout << "Handling client ";
try 
{
    cout<<"Foreign address: "<< sock->getForeignAddress() << ":";
} 
catch (SocketException &e) 
{
    cerr << "Unable to get foreign address" << endl;
}
try 
{
    cout<<"Foreign port: "<< sock->getForeignPort();
} 
catch (SocketException &e)
{
    cerr << "Unable to get foreign port" << endl;
}
cout << " with thread " << pthread_self() << endl;

char echoBuffer[RCVBUFSIZE];
int recvMsgSize;
while ((recvMsgSize = sock->recv(echoBuffer, RCVBUFSIZE)) > 0) 
{
    cout<<"echoBuffer::::"<<echoBuffer;
    //sock->send(echoBuffer, recvMsgSize);
}
   // Destructor closes socket
 }

 static void ThreadMain(void *clntSock)
 {
// Guarantees that thread resources are deallocated upon return  
pthread_detach(pthread_self()); 

// Extract socket file descriptor from argument  
HandleTCPClient((TCPSocket *) clntSock);

delete (TCPSocket *) clntSock;
//return NULL;
 }



 };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2025-01-02 05:06:12

您可以直接使用方法名称而不进行强制转换:

pthread_create(&threadID, NULL, ThreadMain,(void *) &clntSock)

此外,该方法需要返回 void* 而不是 void

static void* ThreadMain(void *clntSock)

You can just use the method name directly without a cast:

pthread_create(&threadID, NULL, ThreadMain,(void *) &clntSock)

Also the method needs to return void* not void:

static void* ThreadMain(void *clntSock)
心欲静而疯不止 2025-01-02 05:06:12

这是错误的:

pthread_create(&threadID, NULL, (void*) &ThreadMain,(void *) &clntSock)

pthread_create 需要一个指向正确签名的函数(ThreadMain)的指针。

pthread_create(&threadID, NULL, MyThreadedServer::ThreadMain, (void *) &clntSock)

这应该足够了。

编辑:正如 Tudor 在他的回答中指出的那样,ThreadMain 函数的返回类型必须是 void*:

void* ThreadMain(void* d);

This is wrong:

pthread_create(&threadID, NULL, (void*) &ThreadMain,(void *) &clntSock)

pthread_create expects a pointer to function (ThreadMain) of the correct signature.

pthread_create(&threadID, NULL, MyThreadedServer::ThreadMain, (void *) &clntSock)

This should be enough.

EDIT: as pointed out by Tudor in his answer, the return type of the ThreadMain function must be void*:

void* ThreadMain(void* d);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文