![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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);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;
}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 ![]() |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I would suggest changing while (clientS == INVALID_SOCKET) to:
while ((clientS == INVALID_SOCKET) && !WM_DESTROY) |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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); 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 |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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.
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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?
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|