Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bind several times to the same port in Mac OS X



Hmmm, something like this came up recently under the subject:
ServerSocket on Mac OS X.  Here was the conclusion of the conversation:

Hello again,

Thank you very much for all your help. The problem was indeed that java
used ipv6 instead of ipv4. To solve it, it sufficed to add the line:

System.setProperty("java.net.preferIPv4Stack","true");

Thanks
Frank

And here's the original question:

All I want to do at the moment is figuring out how to determine whether a
port is in use. Knowing java from different systems / programming languages
this should be pretty straightforward. The snippet:

boolean bInUse;
try{
	ServerSocket oSocket = new ServerSocket();
	oSocket.bind(new InetSocketAddress("host",nPort));
	bInUse = false;
	oSocket.close();
}
catch(BindException oBind)
{
	bInUse = true;
}

should give me the answer right away. The problem though is that I have a
C++ application sitting on the port first. And still the snippet tells me
that this port would not be in use.

Intrigued by that I wrote another small example accepting a connection to
this Java program. Here the outcome:

1. start C++ application on port X	--> success
2. start Java application on port X --> success
3. telnet to localhost port X --> java responds (and shuts down)
4. telnet to localhost port X --> C++ responds

Now I'm completely clueless. I mean the C++ application will not bind to the
port if Java is already on the port, so what is going on here.


Jesse Wilson wrote:

My PowerBook lets me bind to the same port twice using NIO. The following code throws an IOException under Windows and Linux, but not
under Mac OS X:


*public* *void* bindTwice(*int* port) *throws* IOException { InetSocketAddress listenAddress = *new* InetSocketAddress(port);

ServerSocketChannel firstChannel = ServerSocketChannel.open(); ServerSocket firstSocket = firstChannel.socket(); firstSocket.bind(listenAddress);

ServerSocketChannel secondChannel = ServerSocketChannel.open(); ServerSocket secondSocket = secondChannel.socket(); secondSocket.bind(listenAddress); }

It wouldn't be too big a deal but I want my application to fail if it
 can't bind! One horrible problem that has come up in my tests is
this: 1. Bind to port X as server. 2. Connect to self on port X as
client. Now the client has fully connected but server has not
received anything! Another server process that has binded to the same
port is receiving the data instead.

Is this a bug in the Mac JVM?

Any information on this problem will be appreciated, thanks!

Cheers, Jesse Wilson


------------------------------------------------------------------------


_______________________________________________ Do not post admin requests to the list. They will be ignored. Java-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Java-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >Bind several times to the same port in Mac OS X (From: Jesse Wilson <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.