michellemybell
June 1st, 2008, 06:18 AM
Hi
I wrote a small server using asynchronous sockets for windows (winsock). The problem I have is that when the server window is not in focus / not active (say, I have another window in focus instead) then it does not respond to winsock messages. As soon as I put my mouse pointer over the window, or click it, all of a sudden the message queue is read and it then sends its own messages as it should!
I am wondering why, and if there is a way to fix this? IE so that the program is always responsive even when not in focus?
Thanks
Chirieac
June 1st, 2008, 05:21 PM
You use WSAAsyncSelect for your asynchronous socket? Some of your code should be useful.
michellemybell
June 1st, 2008, 07:09 PM
hi chirieac
I do use WSAAsyncselect. Here is some of my code. The code works fine, aslong as the window is in focus. As soon as I switch to another window, the program stops responding to messages. Then when I reactivate the focus, it resumes as normal.
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
//start winsock
WSADATA w;
int error = WSAStartup(0x0202, &w);
if(error)
{
MessageBox(hwnd, "Cannot start winsock.", "Error", MB_OK);
return 1;
}
else
if(w.wVersion != 0x0202)
{
MessageBox(hwnd, "Wrong winsock version.", "Error", MB_OK);
WSACleanup();
return 1;
}
//create listening socket
ls = socket(AF_INET, SOCK_STREAM, 0);
//bind socket
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(5001);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(ls, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
{
MessageBox(hwnd, "Could not bind socket.", "Error", MB_OK);
WSACleanup();
return 1;
}
if(listen(ls,5) == SOCKET_ERROR)
{
MessageBox(hwnd, "Socket could not listen.", "Error", MB_OK);
WSACleanup();
return 1;
}
//bind socket to SOCKET_MESSAGE
WSAAsyncSelect (ls, hwnd, SOCKET_MESSAGE, FD_READ | FD_WRITE | FD_ACCEPT | FD_CLOSE);
break;
case SOCKET_MESSAGE:
{
if (WSAGETSELECTERROR(lParam))
{
closesocket (ls);
WSACleanup();
MessageBox(hwnd, "Socket message error.", "Error", MB_OK);
return 1;
}
switch (WSAGETSELECTEVENT(lParam))
{
case FD_READ:
{
//find sender
Player *p = findSender();
if(p == NULL)
{
MessageBox(hwnd, "Can't find socket.", "Error", MB_OK);
break;
}
char rbuff[MAX_MESSAGE_SIZE];
unsigned int bytes = recv(wParam, rbuff, sizeof(rbuff), 0);
for(unsigned int i = 0; i < bytes; i++)
p->addToBuffer(rbuff[i]);
break;
}
case FD_ACCEPT:
SOCKET c = accept(wParam, NULL, NULL);
Player *p = new Player(0, 0, 0, 0, 0, "", c);
players.push_back(p);
waiting_for++;
break;
case FD_CLOSE:
shutdown(wParam, SD_BOTH);
closesocket(wParam);
break;
}
break;
Chirieac
June 1st, 2008, 07:54 PM
Sorry, but I don't see a problem to you code. The window should receive the socket events even if the window don't have the focus. Maybe the problem is in another place...
michellemybell
June 2nd, 2008, 12:35 AM
Sorry, but I don't see a problem to you code. The window should receive the socket events even if the window don't have the focus. Maybe the problem is in another place...
Hmmm, I guess maybe you are right.. although it will hard to prove this. But I will try. Atleast now I know that this is not the correct behaviour for asynchronous sockets.
Thanks :)