Hmm done a lot of reserach on this, but still I cannot find out why this is failing...
Basically I have set up one test program to have one listening socket, and one client socket, I use Telnet to contact my test program, it works fine, has async sockets, and whenever I type it spits stuff onto the screen.
Now to the problem:
I have another program which uses an STL map to hold all the incoming sockets...
here's the relevant socket handling code..
Code:
HRESULT Cappsvr2View::socketMsg(WPARAM wp, LPARAM lp)
{
if(WSAGETSELECTERROR(lp))
{
WSACleanup();
TRACE0("Get select error, bailing out!");
exit(1);
}
int recvd = 0;
int sockNum = (int)wp;
int tsockNum = 0;
char * tmpbuf;
char buf[100];
ZeroMemory((void *)buf, 100);
sockaddr_in clientSock;
int lenclient = sizeof(clientSock);
int errs = 0;
CString tmp;
LANCLIENT * tmpclient = new LANCLIENT;
std::map<int, LANCLIENT*>::iterator it;
switch(WSAGETSELECTEVENT(lp))
{
case FD_ACCEPT:
//AfxMessageBox("Client connecting!");
tmpclient->ClientSocket = accept(wp, (LPSOCKADDR)&clientSock, &lenclient);
errs = WSAAsyncSelect(tmpclient->ClientSocket, GetSafeHwnd(), WM_ONSOCKET, FD_WRITE | FD_READ | FD_CLOSE);
TRACE1("Error code for AsyncSelect is %d", errs);
if(INVALID_SOCKET == tmpclient->ClientSocket)
{
errs = WSAGetLastError();
TRACE1("ERROR: Socket error: %d\n", errs);
WSACleanup();
}
tmpclient->clnt_addr.sin_addr = clientSock.sin_addr;
tmpclient->clnt_addr.sin_family = clientSock.sin_family;
tmpclient->clnt_addr.sin_port = clientSock.sin_port;
tmpclient->currentMsg = "";
connections[(int)tmpclient->ClientSocket] = tmpclient;
TRACE2("Client Socket is %d and sockNum is %d\n",
connections[(int)tmpclient->ClientSocket]->ClientSocket, sockNum);
TRACE1("Current sockets is %d", connections[(int)tmpclient->ClientSocket]->ClientSocket);
break;
case FD_READ:
recvd = recv((SOCKET)wp, buf, (int)strlen(buf), 0);
//TRACE2("Socket num is %d and wp is %d", connections[sockNum]->ClientSocket, (int)wp);
errs = WSAGetLastError();
//send(connections[sockNum]->ClientSocket, "welcome\n", (int)strlen("welcome\n"), 0);
if(recvd == SOCKET_ERROR)
{
recvd = WSAGetLastError();
TRACE1("Error with error code %d", recvd);
}
else if(errs == WSAEWOULDBLOCK)
{
TRACE0("Getting would block error!");
}
else if(recvd > 0)
{
buf[recvd] = '\0';
tmpbuf = new char[(int)strlen(buf) + 1];
strcpy(tmpbuf, buf);
connections[sockNum]->currentMsg += tmpbuf;
TRACE1("We received %s", tmp);
//send(Clients[clientCount-1], tmpbuf, (int)strlen(tmpbuf), 0);
send(wp, tmpbuf, (int)strlen(tmpbuf), 0);
delete tmpbuf;
tmpbuf = NULL;
}
//AfxMessageBox(buf);
break;
case FD_WRITE:
it = connections.begin();
while(it != connections.end())
{
send(it->second->ClientSocket, "Hello 2 you!\n", (int)strlen("Hello 2 you!\n"),0);
it++;
}
break;
case FD_CLOSE:
AfxMessageBox("Client terminated connection!");
break;
default:
break;
}
//delete tmpclient;
//tmpclient = NULL;
return 1;
}
here's the struct LANCLIENT
Code:
struct LANCLIENT
{
int InUse;
SOCKET ClientSocket;
struct sockaddr_in clnt_addr;
CString currentMsg;
};
I just lose the ability to talk with the telnet client when I use the map, perhaps I am being an idiot and not finding what I need to know, but all I wanted to do was accumulate what the telnet client typed for the moment, any help would be much appreciated, thanks!
Also whenever I type in the telnet client field, I get the FD_READ message, but it always contains 0 bytes when I do a recv... which I read means basically the connection is terminated.