 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
DaveB Guest
|
Posted: Tue Apr 25, 2006 4:12 pm Post subject: Strange SocketChannel.connect() behaviour |
|
|
I currently have a client that is connecting to a server, however the
connect() is completing before the server even performs the accept. Should
the connect not be waiting for the server accept before indicating it is
complete?
This is being run under Java 5.0 and does the same when run on any of 3
systems: two Linux boxes (RHEL 3 and Fedora 5) and Win2000.
The following are two minimal apps that show the behaviour.
Server.java
-------------------------------------------------------------
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class Server
{
public static void main(
String[] args )
{
try
{
ServerSocketChannel ssChannel = ServerSocketChannel.open();
ssChannel.configureBlocking( false );
ssChannel.socket().bind( new InetSocketAddress( 2020 ) );
SocketChannel sChannel = null;
while ( sChannel == null )
{
sChannel = ssChannel.accept();
}
System.out.println( "Received connect from " + sChannel );
}
catch ( IOException e )
{
}
}
}
Client.java
-------------------------------------------------------------
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
public class Client
{
public static void main(
String[] args )
{
try
{
SocketChannel sChannel = SocketChannel.open();
sChannel.configureBlocking( false );
sChannel.connect( new InetSocketAddress( "127.0.0.1", 2020 ) );
while ( ! sChannel.finishConnect() )
{
// Could be doing something else
}
System.out.println( "Socket channel is now ready to use" );
}
catch ( IOException e )
{
}
}
}
When run as shown, the server prints the channel info, after responding to
the accept, and then terminates while the client prints out it is ready.
If I comment out the accept() in the server, it runs forever, not accepting
any connects, however the client still reports the channel is ready. I also
see this when I use a selector with OP_CONNECT and when I use all the isXXX
methods I can think of.
Any and all suggestions will be appreciated, especially those that tell me
how to get the behaviour that is expected.
Thanks,
Dave |
|
| Back to top |
|
 |
Gordon Beaton Guest
|
Posted: Tue Apr 25, 2006 5:12 pm Post subject: Re: Strange SocketChannel.connect() behaviour |
|
|
On Tue, 25 Apr 2006 11:52:02 -0400, DaveB wrote:
| Quote: | I currently have a client that is connecting to a server, however
the connect() is completing before the server even performs the
accept. Should the connect not be waiting for the server accept
before indicating it is complete?
|
The connection succeeds when the 3-way handshake is done, which
happens regardless of whether the server calls accept().
Try a simpler example:
public class MyClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost", port);
System.out.println("connected");
}
}
public class MyServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(port);
Thread.sleep(60000);
}
}
/gordon
--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e |
|
| Back to top |
|
 |
DaveB Guest
|
Posted: Tue Apr 25, 2006 8:12 pm Post subject: Re: Strange SocketChannel.connect() behaviour |
|
|
Hi Gordon, thanks for the quick reply!
Based on your information, I refreshed myself on the 3-way handshake. I
guess my problem is how this relates to the methods available in Java since
it is actually the C listen() method that handles the connections.
I guess I had been assuming that the Java open() was roughly equal to the C
socket(), the Java bind() was equal to the C bind(), and the Java accept()
was roughly equal to the combined C listen() and accept(). This would imply
that the Java accept() would need to be called before a client connect()
could complete. Since I'm getting the complete, the C listen() must be part
of the Java bind().
Without reading mounds of code, any idea how I could confirm this? Also,
does there exist an equivalent to the C listen() in Java or is this just
something out of a developers control?
Thanks again,
Dave
"Gordon Beaton" <n.o.t (AT) for (DOT) email> wrote in message
news:444e5266$1 (AT) news (DOT) wineasy.se...
| Quote: | On Tue, 25 Apr 2006 11:52:02 -0400, DaveB wrote:
I currently have a client that is connecting to a server, however
the connect() is completing before the server even performs the
accept. Should the connect not be waiting for the server accept
before indicating it is complete?
The connection succeeds when the 3-way handshake is done, which
happens regardless of whether the server calls accept().
Try a simpler example:
public class MyClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost", port);
System.out.println("connected");
}
}
public class MyServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(port);
Thread.sleep(60000);
}
}
/gordon
--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e |
|
|
| Back to top |
|
 |
Gordon Beaton Guest
|
Posted: Tue Apr 25, 2006 9:12 pm Post subject: Re: Strange SocketChannel.connect() behaviour |
|
|
On Tue, 25 Apr 2006 15:13:00 -0400, DaveB wrote:
| Quote: | the Java bind() was equal to the C bind(), and the Java accept() was
roughly equal to the combined C listen() and accept().
|
Look for the backlog argument; that should give you a pretty good idea
of where listen() is being called. I'd say that ServerSocket.bind()
wraps both bind() and listen(), and that it's invoked in 3 of the 4
ServerSocket constructors.
| Quote: | Without reading mounds of code, any idea how I could confirm this?
Also, does there exist an equivalent to the C listen() in Java or is
this just something out of a developers control?
|
If you really want to see what's happening at the OS level, one way is
to try running a simple Java server under control of strace or truss
to see what system calls are invoked.
/gordon
--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|