Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 29th, 2005, 1:08 AM   #1
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Unhappy WinSock accept() hangs program in Do loop...

Hey guys, well, I started over with my WinSock functions, and it's going tons better then it did before... So I had a question. I start up Winsock. I create a socket, I bind it, I listen on it. Now, after I start the listening socket, I go into a Do loop, and accept clients... Heres the loop which I'm having troubles with.
do{
             clientS = accept(listenS, NULL, NULL);
             /*if (WM_DESTROY == 1){
             closesocket(listenS);
             WSACleanup();
             PostQuitMessage (0);  // exit  
             } */ 
             Sleep(250);  // PROGRAM HANGS IN THIS LOOP, IT STOPS RESPONDING... AND I TRIED THE IF WM_DESTROY :(
}while (clientS == INVALID_SOCKET);
Ok, for one, I want to wait for a client in a loop and STILL check to see if WM_DESTROY command has been sent.... As you can see, I commented out the part that I tried to check if WM_DESTROY was initiated. Any suggestions on that?


Now, I have this code:
char IpMem[40];
char sPort[10];
GetWindowText(hPortData, sPort, sizeof(sPort)); 
char sIp[30];
int iIp, iPort;
GetWindowText(hIPData, sIp, sizeof(sIp));
strcat(IpMem, sIp);
strcat(IpMem, ":");
strcat(IpMem, sPort);
MessageBox(NULL, IpMem, "Ip and Port", MB_OK);
iIp = atoi(sIp);
iPort = atoi(sPort);
listenS = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenS == INVALID_SOCKET) 
{
                          WSACleanup();
                           closesocket(listenS);
                            MessageBox(NULL, "Failed to create socket, SweetTalk will automatically exit now.", "Error", MB_ICONEXCLAMATION | MB_OK);
                            return EXIT_SUCCESS;
                         }
                         SOCKADDR_IN info;
                         info.sin_family = AF_INET;
                         info.sin_addr.s_addr = INADDR_ANY; // should I replace INADDR_ANY with iIp?
                         info.sin_port = htons(8888); // should I replace 8888 with iPort?
                         ret = bind(listenS,(LPSOCKADDR)&info,sizeof(struct sockaddr));
                         if (ret == SOCKET_ERROR)
                         {
                            WSACleanup();
                            closesocket(listenS);
                            MessageBox(NULL, "Failed to bind socket, SweetTalk will automatically exit now.", "Error", MB_ICONEXCLAMATION | MB_OK);
                            return EXIT_SUCCESS;
                         }
And there you can see my comments, which are my questions...
Should I replace "INADDR_ANY" with iIp? And should I replace "htons(8888)" with iPort?
If you need any more info on what I need help with, please ask...
Thanks a ton times a million
layer is offline   Reply With Quote
Old Apr 29th, 2005, 11:06 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I would suggest changing while (clientS == INVALID_SOCKET) to:
while ((clientS == INVALID_SOCKET) && !WM_DESTROY)
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 29th, 2005, 11:18 AM   #3
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Unhappy hmmm

Hm... Good call. But you placed an extra "(" after the while Although, now I have this:
ret = listen(listenS, 5);
                         do{
                                   clientS = accept(listenS, NULL, NULL);
                                   Sleep(250);  
                         }while (clientS == INVALID_SOCKET && !WM_DESTROY);
As soon as I enter that loop, the title of the GUI changes to "Program Not Responding" (stupid microsoft) and I can't do anything with it obviously. But if you're thinkin' the CPU jumps, it stays at 00 If you need, I'll upload a screenshot on what it looks like when I go into that loop...
Thanks and sorry for wasting your time :o
layer is offline   Reply With Quote
Old Apr 29th, 2005, 11:23 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
If you look closely, you'll see that extra bracket was closed half-way through. This is to distinguish it from while (clientS == (INVALID_SOCKET && !WM_DESTROY)), which is totally different.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 29th, 2005, 11:46 AM   #5
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Alright, now I get it, but it's still hanging, like not responding... I know it's not looping for any events when it goes into the do loop, but I should still be able to at least move the window without the program crashing, shouldn't I?
layer is offline   Reply With Quote
Old Apr 29th, 2005, 12:28 PM   #6
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Ok, I think I've got it, or I'll get it, but I have one little question, first, see the code:
info.sin_addr.s_addr = INADDR_ANY; // should I replace INADDR_ANY with iIp?
info.sin_port = htons(8888);          // should I replace 8888 with iPort?
Basically, should I replace "INADDR_ANY" with the actual Ip address? And replace "8888" with the actual listening port? I couldn't find any answers while googling...
layer is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:57 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC