|
|
Home » U++ Library support » U++ Callbacks and Timers » Problem with non-blocking mode
Problem with non-blocking mode [message #23493] |
Fri, 23 October 2009 11:02 |
Weras
Messages: 4 Registered: August 2009
|
Junior Member |
|
|
Hi!
I have a problem with sockets, namely, a non-blocking mode. I must say that until that moment I had never worked with sockets and my
question may seem silly.
In my project, I repeated the tutorial from the Help Topics "Connection-Oriented Socket Tutorial"
Function accept_socket.Accept (data_socket, & ip_addr)
suspends the entire program, so I point out that the socket nonblocking:
Socket::Init();
if( !ServerSocket(accept_socket, portInfo.portNumber) )
throw Exc("Couldn't bind socket on the local port.");
if( accept_socket.IsOpen() )
{
dword ip_addr;
accept_socket.Block(false);
accept_socket.Peek();
if( !accept_socket.IsError() && accept_socket.Accept(data_socket, &ip_addr) )
{
Cout() << "Connection from " << FormatIP(ip_addr) << "\n";
while(data_socket.IsOpen() && !data_socket.IsEof() && !data_socket.IsError())
Cout() << data_socket.Read();
}
}
But the program still awaits all perform the function Accept. What I missed or am doing wrong?
Also I read that non-blocking socket's have callback when connection started(or something like that). Could you give examples code using this opportunity socket?
P.S.
Sorry my english )
[Updated on: Fri, 23 October 2009 14:55] Report message to a moderator
|
|
|
Re: Problem with non-blocking mode [message #23505 is a reply to message #23493] |
Sat, 24 October 2009 18:49 |
|
mirek
Messages: 14038 Registered: November 2005
|
Ultimate Member |
|
|
Weras wrote on Fri, 23 October 2009 05:02 | Hi!
I have a problem with sockets, namely, a non-blocking mode. I must say that until that moment I had never worked with sockets and my
question may seem silly.
In my project, I repeated the tutorial from the Help Topics "Connection-Oriented Socket Tutorial"
Function accept_socket.Accept (data_socket, & ip_addr)
suspends the entire program, so I point out that the socket nonblocking:
Socket::Init();
if( !ServerSocket(accept_socket, portInfo.portNumber) )
throw Exc("Couldn't bind socket on the local port.");
if( accept_socket.IsOpen() )
{
dword ip_addr;
accept_socket.Block(false);
accept_socket.Peek();
if( !accept_socket.IsError() && accept_socket.Accept(data_socket, &ip_addr) )
{
Cout() << "Connection from " << FormatIP(ip_addr) << "\n";
while(data_socket.IsOpen() && !data_socket.IsEof() && !data_socket.IsError())
Cout() << data_socket.Read();
}
}
But the program still awaits all perform the function Accept. What I missed or am doing wrong?
Also I read that non-blocking socket's have callback when connection started(or something like that). Could you give examples code using this opportunity socket?
P.S.
Sorry my english )
|
I belive: Call Peek and only do Accept if it returns true.
Or you can sepcify timeout in Accept (e.g. to zero).
(I belive you in fact do not need non-blocking sockets in that case, but I might be wrong).
Mirek
|
|
|
|
Re: Problem with non-blocking mode [message #23515 is a reply to message #23507] |
Mon, 26 October 2009 00:22 |
rylek
Messages: 79 Registered: November 2005
|
Member |
|
|
Hello there!
As far as I know, in principle there are two basically different ways to make a socket communication run in background. One is to make the sockets non-blocking, the other to run the socket management in a different thread. Moreover, under Windows, the WSA socket management functions let you bind certain socket operation notifications to the message loop and so to avoid polling and emulate the worker thread behaviour in a single-threaded application.
The current socket wrapper in U++ doesn't support this option, mainly because it requires low-level interception of the message queue which would either have to be built into the U++ core (which would be ugly) or would require lots of low-level hackery; moreover it's available in Windows only, Linux has no similar functionality, which makes usability of such feature very limited in a cross-platform environment like U++.
Normally when you open a nonblocking server socket (using the 5-th "is_blocking" argument of the ServerSocket function), calls to Accept fail during the initial client-server handshake and you can use Peek on the server socket to check for pending client connections.
Regards
Tomas
|
|
|
Goto Forum:
Current Time: Fri Sep 20 05:49:47 CEST 2024
Total time taken to generate the page: 0.03212 seconds
|
|
|