-
Program freezing..
Here is my code:
Code:
BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
HWND display = GetDlgItem(hwnd,IDL_DISPLAY);
int n = 0;
switch(Message)
{
case WM_INITDIALOG:
{
WSADATA w;
if(WSAStartup(0x0202,&w))
SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Error in initializing WinSock.");
else
SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Winsock initialized successfully.");
n++;
if(w.wVersion != 0x0202)
{
SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Wrong version of Winsock.");
WSACleanup();
}
else
SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Right version of Winsock (2.2)");
n++;
}
break;
case WM_CREATE:
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_LISTEN:
{
SOCKET listener;
UINT listenport;
listenport = GetDlgItemInt(hwnd,IDC_SERVERPORT,NULL,false);
listener = socket(AF_INET,SOCK_STREAM,0);
if(listener == INVALID_SOCKET)
{
SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Listening socket error.");
WSACleanup();
}
else
SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Listening socket created successfully");
n++;
sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(listenport);
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(listener,(LPSOCKADDR)&serveraddr,sizeof(serveraddr)) == SOCKET_ERROR)
{
SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Error binding listening port.");
WSACleanup();
}
if(listen(listener,1) == SOCKET_ERROR)
{
SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Error listening.");
closesocket(listener);
WSACleanup();
}
else
{
SOCKET client;
client = accept(listener,NULL,NULL);
if(client == INVALID_SOCKET)
{
SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Cannont accept connection.");
closesocket(listener);
WSACleanup();
}
else
{
char *buffer;
while(buffer != "---")
{
buffer = "---";
recv(client,buffer,sizeof(buffer),0);
if(buffer != "---")
{
if(strlen(buffer) > 0)
{
SendMessage(display,LB_ADDSTRING,n,(LPARAM)buffer);
n++;
}
else
{
closesocket(listener);
SendMessage(display,LB_ADDSTRING,n,(LPARAM)"-- Connection closed");
WSACleanup();
}
}
}
}
}
}
break;
}
break;
case WM_CLOSE:
EndDialog(hwnd,0);
break;
case WM_DESTROY:
break;
default:
return FALSE;
}
return TRUE;
}
When I run the program and click listen it freezes. I think I have the problem singled out to the "accept". If I take out all the code from accept and below, it works fine. If I just leave the accept and take out the socket code below it, it freezes. What could be the problem?
-
I'm suggesting you use asynchronous sockets. I think it freezes is because you are using blocking sockets. Have you tried connecting to it after it 'freezes'? If you would like to convert to asynchronous sockets (they handle like windows messages, less bugs) contact me and I can help you.
-
Do you even have a client connecting? If not, then of course it will "freeze", since it will block until the socket is readable (i.e. there is a client waiting to connect). You could, as neandrake suggested, convert to asynchronous sockets, or you could use a loop of some sort using select() to determine whether or not the listening socket is readable (and handle your windows messages and all that somewhere in the loop), and then once select() returns that there is a client(s) connecting, then you can call accept().