problem usink accept() [winsocket]
-
Hi, I'm trying to connect between few PC's using TCP/IP(multycast connection). this is my listening thread: [bind() and listen() is in another function]
UINT ListeningThread(LPVOID lpvoid)
{
CMultycast_serverDlg *dlg = (CMultycast_serverDlg *)lpvoid;
FD_SET SocketSet; // set of socket descriptors for select()
int port; // looping veriable for ports
struct sockaddr_in echoClntAddr;// client address
SOCKET clntSock; // socket discriptor for client
unsigned int clntLen; // length of client address data structure
int SelectResult = 0;dlg->SelectFlag = false; dlg->ExitFlag = false; while(!dlg->ExitFlag) { FD\_ZERO(&SocketSet); for(port = 0 ; port < dlg->NumberOfPorts ; port++) FD\_SET((unsigned int)dlg->SocketsArray\[port\] , &SocketSet); SelectResult = select(dlg->MaxDescriptor + 1 , &SocketSet , NULL , NULL , &(dlg->selTimeout)); if(SelectResult == 0) continue;//AfxMessageBox("Error : No echo requests for the time you specified....server still listenning"); else if(SelectResult == SOCKET\_ERROR) { AfxMessageBox("Error : select() has failed "); return 0 ; } else { //dlg->SelectFlag = true; for(port = 0 ; port < dlg->NumberOfPorts ; port++) { if(FD\_ISSET(dlg->SocketsArray\[port\] , &SocketSet)) { clntLen = sizeof(echoClntAddr);//set the size of the in- out parameter //if(!dlg->SelectFlag) clntSock=accept(dlg->SocketsArray\[port\],(struct sockaddr\*)&echoClntAddr,(int \*)&clntLen); //dlg->SelectFlag = true; if(clntSock != INVALID\_SOCKET) //wait for a client to connect { dlg->HandleClient(clntSock);//clntSock is connected to a client dlg->m\_RecievedDataListBox.AddString(dlg->RecievedString); //WSACleanup(); } } } }
}
for(port = 0 ; port < dlg->NumberOfPorts ; port++)
closesocket(dlg->SocketsArray[port]);
return 1;
}let me explain what i want to do: i want my PC to listen to several ports,each port is for another PC,and i want to be able to transfer some data between all the PC's and my PC,when all the PC'c try to connect my PC only once(and that's the problem,because accept() is blocking my application after one connection. the important thing is that i can't use broadcast(the other PC must connect as peer to pe
-
Hi, I'm trying to connect between few PC's using TCP/IP(multycast connection). this is my listening thread: [bind() and listen() is in another function]
UINT ListeningThread(LPVOID lpvoid)
{
CMultycast_serverDlg *dlg = (CMultycast_serverDlg *)lpvoid;
FD_SET SocketSet; // set of socket descriptors for select()
int port; // looping veriable for ports
struct sockaddr_in echoClntAddr;// client address
SOCKET clntSock; // socket discriptor for client
unsigned int clntLen; // length of client address data structure
int SelectResult = 0;dlg->SelectFlag = false; dlg->ExitFlag = false; while(!dlg->ExitFlag) { FD\_ZERO(&SocketSet); for(port = 0 ; port < dlg->NumberOfPorts ; port++) FD\_SET((unsigned int)dlg->SocketsArray\[port\] , &SocketSet); SelectResult = select(dlg->MaxDescriptor + 1 , &SocketSet , NULL , NULL , &(dlg->selTimeout)); if(SelectResult == 0) continue;//AfxMessageBox("Error : No echo requests for the time you specified....server still listenning"); else if(SelectResult == SOCKET\_ERROR) { AfxMessageBox("Error : select() has failed "); return 0 ; } else { //dlg->SelectFlag = true; for(port = 0 ; port < dlg->NumberOfPorts ; port++) { if(FD\_ISSET(dlg->SocketsArray\[port\] , &SocketSet)) { clntLen = sizeof(echoClntAddr);//set the size of the in- out parameter //if(!dlg->SelectFlag) clntSock=accept(dlg->SocketsArray\[port\],(struct sockaddr\*)&echoClntAddr,(int \*)&clntLen); //dlg->SelectFlag = true; if(clntSock != INVALID\_SOCKET) //wait for a client to connect { dlg->HandleClient(clntSock);//clntSock is connected to a client dlg->m\_RecievedDataListBox.AddString(dlg->RecievedString); //WSACleanup(); } } } }
}
for(port = 0 ; port < dlg->NumberOfPorts ; port++)
closesocket(dlg->SocketsArray[port]);
return 1;
}let me explain what i want to do: i want my PC to listen to several ports,each port is for another PC,and i want to be able to transfer some data between all the PC's and my PC,when all the PC'c try to connect my PC only once(and that's the problem,because accept() is blocking my application after one connection. the important thing is that i can't use broadcast(the other PC must connect as peer to pe
You have two options : 1. separate accept() in different thread ( one thread per a listening socket ) which, when I think about it now, means all the stuff - socket, listen, bind accept etc. goes in the thread - the so called "ServerThread" 2. make your socket descriptors non blocking in windows I think this is done by using ioctlsocket( FIONBIO .... ) . I've never tryed it in windows, in unix select returns the listening nonblocking socket that accepted a connection, so I assume it is the same in windows.
-
Hi, I'm trying to connect between few PC's using TCP/IP(multycast connection). this is my listening thread: [bind() and listen() is in another function]
UINT ListeningThread(LPVOID lpvoid)
{
CMultycast_serverDlg *dlg = (CMultycast_serverDlg *)lpvoid;
FD_SET SocketSet; // set of socket descriptors for select()
int port; // looping veriable for ports
struct sockaddr_in echoClntAddr;// client address
SOCKET clntSock; // socket discriptor for client
unsigned int clntLen; // length of client address data structure
int SelectResult = 0;dlg->SelectFlag = false; dlg->ExitFlag = false; while(!dlg->ExitFlag) { FD\_ZERO(&SocketSet); for(port = 0 ; port < dlg->NumberOfPorts ; port++) FD\_SET((unsigned int)dlg->SocketsArray\[port\] , &SocketSet); SelectResult = select(dlg->MaxDescriptor + 1 , &SocketSet , NULL , NULL , &(dlg->selTimeout)); if(SelectResult == 0) continue;//AfxMessageBox("Error : No echo requests for the time you specified....server still listenning"); else if(SelectResult == SOCKET\_ERROR) { AfxMessageBox("Error : select() has failed "); return 0 ; } else { //dlg->SelectFlag = true; for(port = 0 ; port < dlg->NumberOfPorts ; port++) { if(FD\_ISSET(dlg->SocketsArray\[port\] , &SocketSet)) { clntLen = sizeof(echoClntAddr);//set the size of the in- out parameter //if(!dlg->SelectFlag) clntSock=accept(dlg->SocketsArray\[port\],(struct sockaddr\*)&echoClntAddr,(int \*)&clntLen); //dlg->SelectFlag = true; if(clntSock != INVALID\_SOCKET) //wait for a client to connect { dlg->HandleClient(clntSock);//clntSock is connected to a client dlg->m\_RecievedDataListBox.AddString(dlg->RecievedString); //WSACleanup(); } } } }
}
for(port = 0 ; port < dlg->NumberOfPorts ; port++)
closesocket(dlg->SocketsArray[port]);
return 1;
}let me explain what i want to do: i want my PC to listen to several ports,each port is for another PC,and i want to be able to transfer some data between all the PC's and my PC,when all the PC'c try to connect my PC only once(and that's the problem,because accept() is blocking my application after one connection. the important thing is that i can't use broadcast(the other PC must connect as peer to pe
select() is normally used for non-blocking socket. try to set the listen socket non-blocking and your problem should be solved (btw. connect() with a non-blocking socket seems not to be possible!). Don't try it, just do it! ;-)
-
select() is normally used for non-blocking socket. try to set the listen socket non-blocking and your problem should be solved (btw. connect() with a non-blocking socket seems not to be possible!). Don't try it, just do it! ;-)
Hi, First,thanks for your help. Do you have some tutorial that can help me solve this problem? With best regards, Eli