Well, I am writing a system thing you can telnet into, this is only my test, So I''ll probably only turn it into a really simple chat.
I''m using Asynchronous sockets, The problem is that I get a call to my (Dialog Box) Window procedure saying that I have Data, but when I call recv it returns zero, and doesn''t fill my buffer. It can''t be the wrong socket, because I only have one client while testing (me!)
I''m not sure exactly whihc code to post for so far, but I''ll give it a shot:
The actual Procedure calling recv:
BOOL cClient::RecieveData ( )
{
char DataBuffer[512];
int BytesRcvd;
// Receive all the data we have on this socket
if (BytesRcvd = recv (Socket, DataBuffer, 511, NULL) == SOCKET_ERROR)
return FALSE;
if (BytesRcvd == 0)
return FALSE; // It always does this
// ''Fix'' the end of the data stream
DataBuffer[BytesRcvd] = ''\0'';
// Process the DAta
ProcessData( DataBuffer );
return TRUE;
}
This is where I initialise Winsock:
BOOL WinsockInitialise ()
{
// Local Variables
WSADATA wsaData;
// Set up Address Structure
Server_Address.sin_family = AF_INET;
Server_Address.sin_addr.s_addr = INADDR_ANY;
Server_Address.sin_port = htons(PORT);
// Start Up Winsock
if (WSAStartup(MAKEWORD(1, 0), &wsaData))
return FALSE;
// Create a Socket
Listening_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// make the socket asynchronous
WSAAsyncSelect(Listening_Socket, hDialogue, WM_WSAASYNC, FD_READ | FD_WRITE | FD_ACCEPT | FD_CLOSE);
// Bind the Socket to Port
bind(Listening_Socket, (struct sockaddr *)&Server_Address, sizeof(Server_Address));
// Listen at the Binded Socket for Connections
listen(Listening_Socket, SOMAXCONN);
return TRUE;
}
and... er.. this is the message procedure that accepts:
BOOL Winsock_Message ( WPARAM wParam, LPARAM lParam )
{
cClient *NewClient;
switch (WSAGETSELECTEVENT(lParam))
{
case FD_ACCEPT:
SetState("Connecting");
if (WSAGETSELECTERROR(lParam))
return FALSE;
// Create a new Client
NewClient = new cClient;
NewClient->Connect (accept(wParam, NULL, NULL));
AddClient(NewClient); // Adds the client to the client list
return TRUE;
case FD_CLOSE: // A client is remotely disconnecting
RemoteDisconnect(wParam);
return TRUE;
case FD_READ: // Data has arrived
Assert(GetClientfromSocket(wParam));
GetClientfromSocket(wParam)->RecieveData();
}
return TRUE;
}
I hate posting loads of code, but if anyone could spend a minute looking through I would greatly appreciate it!