Hi
I have a client server application(UNICODE).
Server being dialog based mfc app.
Client being sdi mfc app.
both r asynchronous in nature due to use of WSAAsyncSelect().
My problem is that the client donot receives the FD_CLOSE notification when the server closes its socket.
The server donot faces such a problem. It is notified when the client closes its socket.
I m pasting the code on the client side:
Client pressing the File->Connect sub menu item:
Code:
void CMainFrame::OnConnect()
{
WSADATA wsaData;
sockaddr_in local;
unsigned int addr;
struct hostent *hp;
const char* sname = "172.16.110.13";
WORD wVersionRequested = MAKEWORD(2,2);
int wsaret = WSAStartup(wVersionRequested, &wsaData);
if(wsaret != 0)
AfxMessageBox(_T("WSAStartup() failed!"));
else
{
AfxMessageBox(_T("Winsock version 2.2 dll loaded. "));
if(inet_addr(sname)==INADDR_NONE)
{
hp=gethostbyname(sname);
}
else
{
addr=inet_addr(sname);
hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
}
local.sin_family = AF_INET;
local.sin_addr.s_addr = *((unsigned long*)hp->h_addr);
local.sin_port = htons((u_short)40000);
client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(client != INVALID_SOCKET)
{
AfxMessageBox(_T("Client socket created successfully!"));
if(WSAAsyncSelect(client, m_hWnd, WM_WINSOCK_NOTIFY, FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE) == SOCKET_ERROR)
AfxMessageBox(_T("WSAAsyncselect() failed to xecute!"));
else
AfxMessageBox(_T("WSAAsyncselect() xecuted successfully!"));
int ret = connect(client, (sockaddr*)&local, sizeof(local));
if(ret == SOCKET_ERROR)
{
ret=0;
ret = WSAGetLastError();
if(ret == WSAEWOULDBLOCK)
{
AfxMessageBox(_T("This kind of error is normal with Asynchronous sockets. It is a non fatal error."));
}
else
{
//some major error.
AfxMessageBox(_T("Unable to connect with the server!"));
closesocket(client);
WSACleanup();
return;
}
}
else
{
AfxMessageBox(_T("Successful connection with server is established!"));
}
}
}
}
My message handler:
LRESULT CMainFrame::OnWinsockNotify(WPARAM wParam, LPARAM lParam)
{
int nError = WSAGETASYNCERROR(lParam);
if (nError != 0)
{
switch (nError)
{
case WSAECONNRESET:
AfxMessageBox(_T("Connection was aborted."));
//CloseSocket();
break;
case WSAECONNREFUSED:
AfxMessageBox(_T("Connection was refused."));
//CloseSocket();
break;
}
return 0;
}
nError = 0;
nError = (int)WSAGETSELECTEVENT(lParam);
switch (nError)
{
case FD_READ:
{
AfxMessageBox(_T(Data is ready tobe read!));
break;
}
case FD_WRITE:
AfxMessageBox(_T("Send some data to the server!"));
break;
case FD_CONNECT:
{
AfxMessageBox(_T("The server accepts the connection!"));
break;
}
case FD_CLOSE:
AfxMessageBox(_T("The server has closed his socket!"));
break;
}
return 0;
}
waiting for suggestions
Regards