www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - strange problem with socket.accept()

reply Charma <Motoko_Kusanagi web.de> writes:
hello,
I have a very strange problem with the accept function of TpcSocket. I 
programmed a mini-server who is waiting for a user and does something as 
soon a user has connected(this has no concern) anyway, i got this part 
of code:

...
scope TcpSocket server = new TcpSocket( );
Socket user;
server.blocking(true);
server.bind( addr );
server.listen( 10 );

...
while(!killServer)
{
writef( "Waiting for user..." ); // **
user = server.accept( );
writef("[OK]")
...
}

Now my problem is that the line marked with ** is only displayed when a 
user connects... which is very strange... i can't figure out the 
problem, since i tell him to FIRST write that line and THEN wait for a 
user...

Any ideas?
thanks
May 30 2007
next sibling parent reply Daniel Giddings <daniel.giddings gmail.com> writes:
Perhaps the output is not being flushed before the socket blocks your 
thread waiting for a user?

Charma wrote:
 hello,
 I have a very strange problem with the accept function of TpcSocket. I 
 programmed a mini-server who is waiting for a user and does something as 
 soon a user has connected(this has no concern) anyway, i got this part 
 of code:
 
 ...
 scope TcpSocket server = new TcpSocket( );
 Socket user;
 server.blocking(true);
 server.bind( addr );
 server.listen( 10 );
 
 ...
 while(!killServer)
 {
 writef( "Waiting for user..." ); // **
 user = server.accept( );
 writef("[OK]")
 ...
 }
 
 Now my problem is that the line marked with ** is only displayed when a 
 user connects... which is very strange... i can't figure out the 
 problem, since i tell him to FIRST write that line and THEN wait for a 
 user...
 
 Any ideas?
 thanks
May 30 2007
parent reply Daniel Giddings <daniel.giddings gmail.com> writes:
I don't see a flush command in std.stdio but it mentions it extends off 
of std.c.stdio so perhaps try adding:

import std.c.stdio;

...

writef( "Waiting for user..." ); // **
fflush(stdout);
user = server.accept( );

Daniel Giddings wrote:
 Perhaps the output is not being flushed before the socket blocks your 
 thread waiting for a user?
 
 Charma wrote:
 hello,
 I have a very strange problem with the accept function of TpcSocket. I 
 programmed a mini-server who is waiting for a user and does something 
 as soon a user has connected(this has no concern) anyway, i got this 
 part of code:

 ...
 scope TcpSocket server = new TcpSocket( );
 Socket user;
 server.blocking(true);
 server.bind( addr );
 server.listen( 10 );

 ...
 while(!killServer)
 {
 writef( "Waiting for user..." ); // **
 user = server.accept( );
 writef("[OK]")
 ...
 }

 Now my problem is that the line marked with ** is only displayed when 
 a user connects... which is very strange... i can't figure out the 
 problem, since i tell him to FIRST write that line and THEN wait for a 
 user...

 Any ideas?
 thanks
May 30 2007
parent reply Charma <Motoko_Kusanagi web.de> writes:
thanks!
this did fix the problem... I didn't know that writef is writing to 
buffer only...

Daniel Giddings wrote:
 I don't see a flush command in std.stdio but it mentions it extends off 
 of std.c.stdio so perhaps try adding:
 
 import std.c.stdio;
 
 ...
 
 writef( "Waiting for user..." ); // **
 fflush(stdout);
 user = server.accept( );
 
 Daniel Giddings wrote:
 Perhaps the output is not being flushed before the socket blocks your 
 thread waiting for a user?

 Charma wrote:
 hello,
 I have a very strange problem with the accept function of TpcSocket. 
 I programmed a mini-server who is waiting for a user and does 
 something as soon a user has connected(this has no concern) anyway, i 
 got this part of code:

 ...
 scope TcpSocket server = new TcpSocket( );
 Socket user;
 server.blocking(true);
 server.bind( addr );
 server.listen( 10 );

 ...
 while(!killServer)
 {
 writef( "Waiting for user..." ); // **
 user = server.accept( );
 writef("[OK]")
 ...
 }

 Now my problem is that the line marked with ** is only displayed when 
 a user connects... which is very strange... i can't figure out the 
 problem, since i tell him to FIRST write that line and THEN wait for 
 a user...

 Any ideas?
 thanks
May 30 2007
parent Daniel Giddings <danielg microforte.com> writes:
Charma wrote:
 thanks!
 this did fix the problem... I didn't know that writef is writing to 
 buffer only...
 
No probs, in most languages the output is buffered by default as it is very inefficient to write to the console (or file) with every write call. So it will decide to flush the buffer periodically, but that would only happen when you call an ouput function. :-) Dan
May 30 2007
prev sibling parent BCS <ao pathlink.com> writes:
Reply to Charma,

 writef( "Waiting for user..." ); // **
 ...
 Now my problem is that the line marked with ** is only displayed when
 a user connects... which is very strange... i can't figure out the
another way to get writef to flush is send a '\n'.
May 30 2007