| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
May 30, 2007 strange problem with socket.accept() | ||||
|---|---|---|---|---|
| ||||
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 Re: strange problem with socket.accept() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Charma | > 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? Output buffering? Since server.accept() blocks you have to wait for the user to connect until writef next called, whereupon the buffered output may be flushed? Maybe try something like the following instead import std.c.stdio; // ... while(/*...*/) { writef( "Waiting for user..." ); fflush(stdout); // or flushall(); // ... } Yes, I am just guessing. Perhaps best to ask in digitalmars.D.learn - Paul | |||
May 30, 2007 Re: strange problem with socket.accept() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Paul Findlay | Paul Findlay Wrote:
> > 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?
> Output buffering? Since server.accept() blocks you have to wait for the user to connect until writef next called, whereupon the buffered output may be flushed?
>
> Maybe try something like the following instead
>
> import std.c.stdio;
>
> // ...
> while(/*...*/)
> {
> writef( "Waiting for user..." );
> fflush(stdout); // or flushall();
> // ...
> }
>
> Yes, I am just guessing. Perhaps best to ask in digitalmars.D.learn
>
> - Paul
i do know that accept blocks... but it is called AFTER the writef... so it should black AFTER it... i still don't get it...
| |||
May 30, 2007 Re: strange problem with socket.accept() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Charma | Charma Wrote:
> Paul Findlay Wrote:
>
> > > 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?
> > Output buffering? Since server.accept() blocks you have to wait for the user to connect until writef next called, whereupon the buffered output may be flushed?
> >
> > Maybe try something like the following instead
> >
> > import std.c.stdio;
> >
> > // ...
> > while(/*...*/)
> > {
> > writef( "Waiting for user..." );
> > fflush(stdout); // or flushall();
> > // ...
> > }
> >
> > Yes, I am just guessing. Perhaps best to ask in digitalmars.D.learn
> >
> > - Paul
>
>
> i do know that accept blocks... but it is called AFTER the writef... so it should black AFTER it... i still don't get it...
I am fairly sure it is output buffering (as Paul suggested). Change writef to writefln (I believe the output code flushes itself when a newline is sent). Failing that manually flushing as Paul mentioned should fix it.
To explain, you call writef, it puts that in the output buffer but does not flush that to the screen. You call accept, this waits for a connection, a connection arrives and (for some reason) the output buffer gets flushed, perhaps because the socket code flushes all handles at some stage?
Regan
| |||
May 30, 2007 Re: strange problem with socket.accept() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath Wrote:
> Charma Wrote:
> > Paul Findlay Wrote:
> >
> > > > 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?
> > > Output buffering? Since server.accept() blocks you have to wait for the user to connect until writef next called, whereupon the buffered output may be flushed?
> > >
> > > Maybe try something like the following instead
> > >
> > > import std.c.stdio;
> > >
> > > // ...
> > > while(/*...*/)
> > > {
> > > writef( "Waiting for user..." );
> > > fflush(stdout); // or flushall();
> > > // ...
> > > }
> > >
> > > Yes, I am just guessing. Perhaps best to ask in digitalmars.D.learn
> > >
> > > - Paul
> >
> >
> > i do know that accept blocks... but it is called AFTER the writef... so it should black AFTER it... i still don't get it...
>
> I am fairly sure it is output buffering (as Paul suggested). Change writef to writefln (I believe the output code flushes itself when a newline is sent). Failing that manually flushing as Paul mentioned should fix it.
>
> To explain, you call writef, it puts that in the output buffer but does not flush that to the screen. You call accept, this waits for a connection, a connection arrives and (for some reason) the output buffer gets flushed, perhaps because the socket code flushes all handles at some stage?
>
> Regan
ok, i got it now!
fflush fixed it, thanks a lot!
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply