problem with WM_SOCKET
The second player almost automatically quits. (Never the second one)
This happens when I run the the server once and the client app twice on my computer, but also when I run one server and one client, and another computer just the client app.
The servercode that generates the error is here :
//listening...accepting...connecting...
//so far so goed
printf("Starting Game!\n");
closesocket (s[0]);
char buffer[2];
sprintf (buffer,"%c%d",HL_STARTGAME,0);
error = send (s[1],buffer,2,0);
printf("error %4d\n", error);
if ((error==0)||(error==SOCKET_ERROR))
{
printf("Error: Player 1 quit!\n");
WSACleanup ();
return;
}
error = send (s[2],buffer,2,0);
printf("error %4d\n", error);
//here, 'error' has the value of -1
//as a consequence, a WSACleanup is performed.
if ((error==0)||(error==SOCKET_ERROR))
{
printf("Error: Player 2 quit!\n");
WSACleanup ();
return;
}
Does anyone have a clue why this error is generated ?
This is the client code. I left out my ip.
void game_init()
{
WSADATA w;
int error = WSAStartup (0x0202,&w);
if (error)
{
MessageBox (hWnd,"Error: You need WinSock 2.2!","Error",MB_OK);
PostQuitMessage (0);
return;
}
if (w.wVersion!=0x0202)
{
MessageBox (hWnd,"Error: Wrong WinSock version!","Error",MB_OK);
PostQuitMessage (0);
WSACleanup ();
return;
}
//no errors here
s = socket (AF_INET,SOCK_STREAM,0);
if (s == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return;
}
//no errors here either
//WSAAsyncSelect (s,hWnd,WM_SOCKET,(FD_CLOSE | FD_CONNECT | FD_READ));
//I disabled WSAASyncSelect because when I enable it,
//an error is generate in WndProc ( in the WM_SOCKET block)
//(see a bit further)
ip[0] = (char)***;
ip[1] = (char)***;
ip[2] = (char)***;
ip[3] = (char)***;
char a[16];
sprintf (a,"%d.%d.%d.%d",(int)ip[0],(int)ip[1],(int)ip[2],(int)ip[3]);
you.sin_family = AF_INET;
you.sin_port = htons (5555);
you.sin_addr.s_addr = inet_addr (a);
if (connect(s,(LPSOCKADDR)&you,sizeof(you))==SOCKET_ERROR)
{
if (WSAGetLastError()==WSAEWOULDBLOCK)
{
printf("wsaewouldblock error...\n");
Sleep (750);
connect(s,(LPSOCKADDR)&you,sizeof(you));
}break;
sprintf (txtbuffer,"Error: %d",WSAGetLastError());
MessageBox (hWnd,txtbuffer,"Error ",MB_OK);
}
return;
}
//here's the WM_SOCKET part from wndProc
case WM_SOCKET:
{
printf("wm_socket opgeroepen...");
if (WSAGETSELECTERROR(lParam))
{
sprintf (txtbuffer,"Error2: %d",WSAGetLastError());
MessageBox (hWnd,txtbuffer,"Error",MB_OK);
return 0;
}
switch (WSAGETSELECTEVENT(lParam))
{
case FD_READ:
//and so on...
Here, the MessageBox from the second clien gives an error which sais 'Error2: 0'
From the moment that the second cliend has had this error, the first client gets it too. The first one gets it too from the WM_SOCKET part.
I don't know why it does that
Has not using
WSAAsyncSelect (s,hWnd,WM_SOCKET,(FD_CLOSE | FD_CONNECT | FD_READ));
have anything to do with it maybe?
Does anyone have a clue ?
Thanks in advance.

