Problem in using CreateIoCompletionPort.
I am trying to write http proxy server but it waiting at
::GetQueuedCompletionStatus(g_main_port, &read_bytes,
(DWORD*)&key,&over, INFINITE); When I tried to connect through browser
.
I debug I found it is listening but not accepting because it is
waiting at (GQCS) so what is the mistake I did in using
CreateIoCompletionPort
unsigned int WINAPI ThreadProc(void *);
#define HTTP "http://"
HANDLE g_work_port;
HANDLE g_main_port;
typedef struct _session
{
SOCKET acceptfd;
}SESSION;
void main()
{
int clinu=0;
unsigned threadID;
//-------------------------------server ---------------------------
SOCKET acceptfd;
int server[150],nready,socketfd,nrec=0,nsend=0,listenfd=0,len;
char serverbuf[1024]={0};
struct sockaddr_in serv;
WSADATA wsa;
WSAStartup(MAKEWORD(2,0),&wsa);
listenfd=::WSASocket(AF_INET, SOCK_STREAM,IPPROTO_IP, NULL, 0,
WSA_FLAG_OVERLAPPED);
serv.sin_family=AF_INET;
serv.sin_port=htons(9393);
serv.sin_addr.s_addr=INADDR_ANY;
//-----------------------------------------------------------------------------------------------
printf("server port: 9393\n");
printf("client port: 8080\n\n");
len=sizeof(serv);
//server
printf("bind %d",bind(listenfd,(struct sockaddr *)&serv,len));
printf("listen %d\n",listen(listenfd,5));
printf("listen fd %d\n",listenfd);
g_main_port = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL,
0,0);
g_work_port = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL,
0,0);
if(g_work_port == NULL || g_main_port == NULL)
{
printf("can not create io completion port");
return ;
}
// creating 4 threads
printf("_beginthread \n");
unsigned int i;
for(i = 0; i < 4; i++)
{
unsigned int thread_id;
HANDLE thread = (HANDLE) _beginthreadex(NULL, 0, ThreadProc, 0, 0,
&thread_id);
if(thread == NULL)
{
printf("can not create thread");
return ;
}
}
HANDLE handle =::CreateIoCompletionPort((HANDLE)&listenfd,
(HANDLE)g_main_port,(int) &listenfd, 0);
DWORD* key;
DWORD read_bytes;
OVERLAPPED* over;
while(1)
{
printf("wait for GetQueuedCampletionStatus g_main_port \n");
int ret = ::GetQueuedCompletionStatus(g_main_port,
&read_bytes, (DWORD*)&key,&over, INFINITE);
// here it is waiting
printf("wait for accept ret %d read Bites %d\n",ret,read_bytes);
if(ret==0||read_bytes<=0)
{
continue ;
}
DWORD dwRead;
char *acceptbuff =(char*)malloc(1024);
OVERLAPPED ol;
bool bl;
bl=AcceptEx(listenfd,acceptfd,acceptbuff,0,sizeof( struct
sockaddr_in)+16,sizeof( struct sockaddr_in)+16,&dwRead, &ol);
printf("\naccepted............... %d bool %d \n",acceptfd,bl);
SESSION session;
session.acceptfd=acceptfd;
HANDLE hPort = ::CreateIoCompletionPort((HANDLE)&session,
(HANDLE)g_work_port, (int) &session, 0);
char *serverbuff,tbuff[1024];
int nrec=0;
serverbuff=(char*)malloc(1024);
nrec=recv(acceptfd,serverbuff,1024,0);
printf("nrecv %d \n",nrec);
strcpy(tbuff,serverbuff);
if(nrec==0 || nrec == SOCKET_ERROR )
{
printf("ERROR SERVER FD_ISSET(server[i],&rset)==0\n");
printf(" thread breaks acceptfd %d.........\n",acceptfd);
closesocket(acceptfd);
return;
}
}//End if(FD_ISSET(listenfd,&rset))
//}//End while
}
//------------------------Thread ----------------------
unsigned int WINAPI ThreadProc(void *arg)
{
printf("in thread\n");
DWORD* key;
DWORD read_bytes;
OVERLAPPED* over;
while(1)
{
printf("GetQCP g_main_port\n");
int ret = ::GetQueuedCompletionStatus(g_work_port, &read_bytes,
(DWORD*)&key,&over, 10000);
printf("ret %d read bites %d\n",ret,read_bytes);
if(ret==0||read_bytes<=0)
continue ;