English 中文(简体)
c++的硬盘服务器多读
原标题:multithreading in tcp server in c++

我已经为C++的多读制笔服务器撰写了这一课程,并编集了该课程。

g++ -o server server.cpp -lpthread

BUT • 我发现以下错误:

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;
 }



 };
最佳回答

你们只能直接使用这种方法的名称,而不作成:

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

这种方法还需要交回<代码> 避免* 而不是 撤销:

static void* ThreadMain(void *clntSock)
问题回答

这是错误的:

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)

这应当足够。

EDIT:正如Tudor在其答复中指出的,ThreadMain功能的返回类型必须是无效的*:

void* ThreadMain(void* d);




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签