![]() |
| | #1 |
| Registered User Join Date: Apr 2004
Posts: 102
| Make accept() stop I just need a way to cause an error within accept() to make it stop. thanks |
| b00l34n is offline | |
| | #2 |
| 'AlHamdulillah Join Date: Feb 2003
Posts: 790
| what you are looking at is one of the problems of non-blocking sockets. This can easily(and I do mean easily) be resolved if you poll a socket to check to see if there is any incoming data, if so then you can try to use accept(a socket listening for connections should do just that, and not try to listen for other data, IMO).
__________________ there used to be something here, but not anymore |
| EvBladeRunnervE is offline | |
| | #3 |
| Carnivore ('-'v) Join Date: May 2002
Posts: 2,866
| That is one of the problems of blocking sockets. Two options are, port your program to using asynchronous sockets, or do the polling thing that EvBladeRunnervE suggested (look into select()).
__________________ Just Google It. √ (\ /) ( . .) c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination. |
| Hunter2 is offline | |
| | #4 |
| Yes, my avatar is stolen Join Date: Dec 2002
Posts: 2,544
| Just for kicks I wrote an abortable accept function. It is untested so let me know if it works. Error checking could be better. Code: /*
* Works like the normal accept(), except that it takes an abort event. When this event is
* signalled this function will return INVALID_SOCKET immediately.
*/
SOCKET AbortableAccept(SOCKET s, struct sockaddr* addr, int* addrlen, HANDLE hEventAbort)
{
SOCKET sockRet;
HANDLE hEvents[2];
ULONG setting[2] = { 0, 0 };
if (hEventAbort == NULL)
{
/* Normal, non-abortable accept(). */
return accept(s, addr, addrlen);
}
hEvents[0] = CreateEvent(NULL, FALSE, FALSE, NULL); /* Will be signalled for an incoming connection. */
hEvents[1] = hEventAbort; /* Will be signalled by another thread to abort the accept operation. */
WSAEventSelect(s, hEvents[0], FD_ACCEPT); /* Puts socket in non-blocking mode and
* tells windows to signal hEvents[0] when the
* accept() has completed or an error occurs. */
if (WaitForMultipleObjects(2, hEvents, FALSE, INFINITE) == WAIT_OBJECT_0)
{
/* An incoming connection has arrived or a network error has occurred. */
sockRet = accept(s, addr, addrlen);
}
else
{
/* Abort event was signalled or WaitForMultipleObjects error. */
sockRet = INVALID_SOCKET;
}
/* Cleanup event signalling */
WSAEventSelect(s, hEvents[0], 0);
CloseHandle(hEvent);
/* Put sockets back into blocking mode */
ioctlsocket(s, FIONBIO, &setting[0]);
ioctlsocket(sockRet, FIONBIO, &setting[1]);
return sockRet;
}
/* Sample usage. */
/* Create a global manual-reset event. */
g_hAbortEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
/* Start an abortable accept. */
sock = AbortableAccept(s, (struct sockaddr*) &addr, &addrlen, g_hAbortEvent);
/* In another thread - signal the event and abort the accepts that are using it. */
SetEvent(g_hAbortEvent);
|
| anonytmouse is offline | |
| | #5 |
| Registered User Join Date: Apr 2004
Posts: 102
| So, Hunter2, could you please point me to a link that explains how to convert my program to use asynchronous sockets? anonytmouse, even though that code looks very interesting, I think it's a little ahead of me at the moment. |
| b00l34n is offline | |
| | #6 |
| Carnivore ('-'v) Join Date: May 2002
Posts: 2,866
| I'm pretty sure there's some links to tutorials at the top of this forum, most of them will deal with asynchronous Winsock to some extent. Here's Johnnie's tutorial, it covers the very very basics of using asynchronous winsock at the bottom of the tutorial.. you can probably find more if you search on Google. http://www.hal-pc.org/~johnnie2/winsock.html
__________________ Just Google It. √ (\ /) ( . .) c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination. |
| Hunter2 is offline | |
| | #7 |
| Registered User Join Date: Apr 2004
Posts: 102
| Ok, so I got it semi-working. If I only call WSAAsyncSelect() once, it's fine. But if I try to call it again for another socket, it fails. why can this be? |
| b00l34n is offline | |
| | #9 |
| Registered User Join Date: Apr 2004
Posts: 102
| Strange, It's now working because I took out a piece of unrelated code. Is there anyway to get the port back from a socket? I tried: Code: sockaddr_in test; SOCKET sock = accept((SOCKET)wParam,(sockaddr*)&test,&x); wsprintf(addstrbuff,""Port:%d ",(int)ntohs(test.sin_port)); MessageBox(0,addstrbuff,"",0); |
| b00l34n is offline | |
| | #10 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 3,020
| What are you getting as the value for the port? At a glance, your code looks correct. Also make sure that x is initialized to sizeof(struct sockaddr) before you pass its address to accept(). |
| bithub is offline | |
| | #11 |
| Registered User Join Date: Apr 2004
Posts: 102
| I usualy get a random number above 3000, If the first random number is 3980 the next will be 3981, and so on, no matter which port it is. |
| b00l34n is offline | |
| | #12 |
| Registered User Join Date: Apr 2004
Posts: 102
| Just figured it out, It's giving me the remote port, not local. Is it possible to get the local port at which a connection is established? |
| b00l34n is offline | |
| | #13 |
| Yes, my avatar is stolen Join Date: Dec 2002
Posts: 2,544
| Typically, for a server socket, the local port is whatever you bound it to. However, if you need a function to retrieve this information, you can use getsockname(). |
| anonytmouse is offline | |
| | #14 |
| I lurk Join Date: Aug 2002
Posts: 1,361
| Simply closing the socket from another thread will cause accept() to return. |
| Eibro is offline | |
| | #15 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| random: Johnie2 updated his tutorial? Nice job man. I remember him asking me about why I called it incomplete once. Impressively better. |
| master5001 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Why does my prog just stop? | Queatrix | Windows Programming | 4 | 03-01-2006 05:12 PM |
| Win32 Common Controls in C++, how do i make and use them? | C+noob | Windows Programming | 6 | 01-09-2006 11:53 AM |
| want to make this small program... | psycho88 | C++ Programming | 8 | 11-30-2005 02:05 AM |
| Make Cortana Stop Kicking Me!!!??? | drdroid | C++ Programming | 6 | 02-27-2003 05:27 PM |
| how to make files | quiksilver9531 | C++ Programming | 6 | 02-22-2002 06:44 PM |