Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
this function in thread that accept connection
C#
while(true)
{
  


        s = accept(Socket,(struct sockaddr*)&client,&len);

        if( s >-1)
            {
             re = new HandleResponse();
              re->set_Client(s);
              //re->Set_client(Socket);

               re->start();
                re->gt;join();
              delete re;
                    printf("count client");
            }

}

and this
HandleRequest thread

C#
int HandleResponse::Read()
{
    int r = read(Socket,bbuf,sizeof(bbuf));
  return r;
}
int  HandleResponse::Write()
{
    int w = write(Socket,bbuf,sizeof(bbuf));
    return w;
}

void* HandleResponse::run() // when call start it call this function thread
{
    while(true)
    {
    Read();
    Write();
    }
    return 0;
}

problem it only accept one client and respond to it but did't accept any other client
i think the problem is the code wait on re->join() and stop to accept if accept once
Posted
Updated 7-Aug-14 3:44am
v2

You can use pthreads on most platforms.

http://www.cs.nmsu.edu/~jcook/Tools/pthreads/pthreads.html[^]

On Windows, you can use beginthread() or beginthreadex().

http://msdn.microsoft.com/en-us/library/kdzttdcb.aspx[^]

As an alternative to creating threads, you can set the accepted socket to non-blocking and use select() for reads, writes, and errors.

http://unixhelp.ed.ac.uk/CGI/man-cgi?select+2[^]

Here's a beginthreadex() example:

C++
s = accept(Socket,(struct sockaddr*)&client,&len);
if( s >-1)
{
    re = new HandleResponse();
    re->set_Client(s);
    HANDLE handle = _beginthreadex(NULL, 8192, worker, re, 0, NULL);
    CloseHandle(handle);
}


Your worker thread procedure could look like ...

C++
static unsigned __cdecl worker(void *context)
{
    HandleResponse re = (HandleResponse *)context;
    re->run();
    return 0;
}
 
Share this answer
 
v4
Comments
Mahmoud_Gamal 8-Aug-14 4:03am    
ok but re->start already do this and Handleresponse inherit from thread that call run in every drived class
[no name] 8-Aug-14 12:31pm    
See new proposed solution ...
Drop the call to join(). There's no need for it.

Also, change the run() function:

C++
void* HandleResponse::run() // when call start it call this function thread
{
    int size = 0;
    do 
    {
        size = read(Socket, bbuf, sizeof bbuf);
        write(Socket, bbuf, size);
    }
    while (size >= 0); // stop loop when socket closes.
    return 0;
}


If run() is the only place that needs read/write, the Read and Write functions aren't really necessary.
 
Share this answer
 
Comments
Mahmoud_Gamal 9-Aug-14 17:22pm    
Thanks for time but it also did't solve the problem is if first thread that accept connection
accept client and begin to read and write the thread that accept wait to end read of the read thread but i need accept run all tim eand read write to other client also run and handle all time
[no name] 10-Aug-14 1:35am    
Please update the question with your latest code. Include a full description of HandleResponse. Make sure you aren't calling join or delete after accepting the socket. Your question was "How to create thread for each accepted client." If your accept() code won't accept more than one client, that's a different problem to be solved.
Mahmoud_Gamal 10-Aug-14 3:57am    
i know but when i commentated while true that contain read and write for each connected client the accepting more client work perfectly but every client open session read first and server return response one time i wont every client write and read from server all time

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900