elsayed_mazen
June 20th, 2004, 04:12 AM
I want to ask something about WinSock. I use it to make connection-Oriented program, I make "PREPARING" for the client and server socket: socket(); setsockopt(SO_RCVTIMEO); set IP address and port for the socket; and finally bind();
For the client, connect(); send(); and there is no SOCKET_ERROR.
Then, for the server, I make listen(); and then the following code:
while(TRUE)
{
iRet=0;
iRet = select(2,&serverReadFD,NULL,NULL,&selectTimeout);
if(iRet == SOCKET_ERROR)
{
AfxMessageBox("ERROR SOCKET SELECT");
closesocket(serverSocket);
return 0;
}
else if(iRet >0)
{
if(FD_ISSET(serverSocket,&serverReadFD))
{
int addrLength=sizeof(sockaddr);
clientConnection = accept(serverSocket,
(struct sockaddr*)&connectionInfo,&addrLength);
if(clientConnection == INVALID_SOCKET)
{
AfxMessageBox("ERROR SOCKET ACCEPT");
closesocket(clientConnection);
closesocket(serverSocket);
return 0;
}
FD_SET(clientConnection,&mainReadFD);
}
else
{
//
iRet = recv(clientConnection,msgData,sizeof(msgData),0);
if(iRet == SOCKET_ERROR)
{
AfxMessageBox("ERROR SOCKET RECEIVE");
closesocket(clientConnection);
closesocket(serverSocket);
return 0;
}
else if(iRet >0)
{
//AfxMessageBox("IN SOCKET RECEIVE");
CString tmp;
tmp.Format("%d",_T(msgData));
::PostMessage(data->mainHandler,WM_DATA_MSG,
(WPARAM)AllocBuffer(tmp),0);
tmp.Insert(0,"REPLAY: ");
iRet = send(clientConnection,(const char*)tmp.GetString(),
tmp.GetLength(),0);
if(iRet == SOCKET_ERROR)
{
AfxMessageBox("ERROR SOCKET SEND");
closesocket(clientConnection);
closesocket(serverSocket);
return 0;
}
memset(msgData,'\0',sizeof(msgData));
}//END RECV CONNECTION
}
}// END SELECT SUCCESS
FD_ZERO(&serverReadFD);
serverReadFD=mainReadFD;
}//while(TRUE)
There is no SOCKET_ERROR either, but the server infinitly receive the Client message "There is no problem in the accept() function".
By debug, I saw that select() return every time the socket that make the connection with the client is ready to receive data.
about the send() functin code in the client thread, here is the code:
while(TRUE)
{
::PeekMessage(&threadMsg,NULL, WM_USER, WM_APP, PM_REMOVE);
if(threadMsg.message == WM_SEND_MSG)
{
iRet = send(clientSocket, (const char*)threadMsg.wParam,
sizeof(threadMsg.wParam), 0);
if( iRet == SOCKET_ERROR )
{
AfxMessageBox("ERROR SOCKET SEND FROM CLIENT");
errorMsgHandler(data->mainHandler, WSAGetLastError());
closesocket(clientSocket);
return 0;
}
}
}//END WHILE (TRUE)
HELP ME PLEASE . . . .
For the client, connect(); send(); and there is no SOCKET_ERROR.
Then, for the server, I make listen(); and then the following code:
while(TRUE)
{
iRet=0;
iRet = select(2,&serverReadFD,NULL,NULL,&selectTimeout);
if(iRet == SOCKET_ERROR)
{
AfxMessageBox("ERROR SOCKET SELECT");
closesocket(serverSocket);
return 0;
}
else if(iRet >0)
{
if(FD_ISSET(serverSocket,&serverReadFD))
{
int addrLength=sizeof(sockaddr);
clientConnection = accept(serverSocket,
(struct sockaddr*)&connectionInfo,&addrLength);
if(clientConnection == INVALID_SOCKET)
{
AfxMessageBox("ERROR SOCKET ACCEPT");
closesocket(clientConnection);
closesocket(serverSocket);
return 0;
}
FD_SET(clientConnection,&mainReadFD);
}
else
{
//
iRet = recv(clientConnection,msgData,sizeof(msgData),0);
if(iRet == SOCKET_ERROR)
{
AfxMessageBox("ERROR SOCKET RECEIVE");
closesocket(clientConnection);
closesocket(serverSocket);
return 0;
}
else if(iRet >0)
{
//AfxMessageBox("IN SOCKET RECEIVE");
CString tmp;
tmp.Format("%d",_T(msgData));
::PostMessage(data->mainHandler,WM_DATA_MSG,
(WPARAM)AllocBuffer(tmp),0);
tmp.Insert(0,"REPLAY: ");
iRet = send(clientConnection,(const char*)tmp.GetString(),
tmp.GetLength(),0);
if(iRet == SOCKET_ERROR)
{
AfxMessageBox("ERROR SOCKET SEND");
closesocket(clientConnection);
closesocket(serverSocket);
return 0;
}
memset(msgData,'\0',sizeof(msgData));
}//END RECV CONNECTION
}
}// END SELECT SUCCESS
FD_ZERO(&serverReadFD);
serverReadFD=mainReadFD;
}//while(TRUE)
There is no SOCKET_ERROR either, but the server infinitly receive the Client message "There is no problem in the accept() function".
By debug, I saw that select() return every time the socket that make the connection with the client is ready to receive data.
about the send() functin code in the client thread, here is the code:
while(TRUE)
{
::PeekMessage(&threadMsg,NULL, WM_USER, WM_APP, PM_REMOVE);
if(threadMsg.message == WM_SEND_MSG)
{
iRet = send(clientSocket, (const char*)threadMsg.wParam,
sizeof(threadMsg.wParam), 0);
if( iRet == SOCKET_ERROR )
{
AfxMessageBox("ERROR SOCKET SEND FROM CLIENT");
errorMsgHandler(data->mainHandler, WSAGetLastError());
closesocket(clientSocket);
return 0;
}
}
}//END WHILE (TRUE)
HELP ME PLEASE . . . .