开发者

How can I get a SocketServer in Java to Block the port in use?

This is my first question here, I'm going to try and give as much info as开发者_如何学编程 I can.

I have a local installation of Apache 2.2 installed on my PC. I have it default to bind to port 100 (Don't ask me why). When I run Apache, I cannot create a socket server with port of 100 because this port is already in use. What I want to do is duplicate this functionality in a Java Server I'm writing.

Currently, I create a socket server like this:

ServerSocket srv = new ServerSocket(100);

The problem is, this doesn't block other applications from using this port. I can run more then one copy of my Java application, or even start my Apache server after this. I want the java Server Socket to mimic the behaviour of Apache and block other applications from binding to the chosen port.

I tried using Google, and the only thing I could find was this http://www.dreamincode.net/forums/topic/124376-block-port/ which implies that creating a socket server as shown above should block the port from use in other applications.

I'm using Windows 7 Ultimate-64bit, NetBeans IDE 6.9.1, and Java 1.6.0_23.

Thanks user658991,

It appears that the port will not be blocked until after a client connects or unless I call ServerSocket.accept();

try
{
    ServerSocket server = new ServerSocket(12345);
    System.out.println("Socket Server Established on port " + server.getLocalPort());
    server.accept(); // Code Stops here until connection is completed. 

    // Do Stuff
}
catch(IOException e)
{
    System.out.println("Socket Server Connection Failed!");
    System.out.println(e);
    System.exit(-1);
}


Creating a ServerSocket itself should block the port. Look at your code, somewhere it could be going wrong.


Are you absolutely sure your program is running? I guess it might have finished running. Please post your complete code. The code below should surely block the port.

ServerSocket server = new ServerSocket(3000);

while(true){
    Socket socket = server.accept();
    socket.close();
}


That shouldn't be the case.

ServerSocket ss = new ServerSocket(9987);

if i have another process that attempts to ServerSocket ss = new ServerSocket(9987);

it will give me this error.

java.net.BindException: Address already in use: JVM_Bind at java.net.PlainSocketImpl.socketBind(Native Method) at java.net.PlainSocketImpl.bind(Unknown Source) at java.net.ServerSocket.bind(Unknown Source) at java.net.ServerSocket.(Unknown Source) at java.net.ServerSocket.(Unknown Source) at MainDriver.main(MainDriver.java:15)


Does the ServerSocket ctor actually bind to the port, or does this only happen when you later call 'listen'? I suspect that the ctor just stores the '100' in some field.

Rgds, Martin


If you are invoking the close() method of the ServerSocket object, the port will no longer be blocked, and can be used bound to, by other applications.

If you wish to have a long running application listen on a specific port, just like how Apache does, then you should have your ServerSocket object accept connections in one thread, and spawn off threads for each single connection received.

This is done by invoking the accept() method of the ServerSocket class, so that every request will create a unique Socket object, which is then read from and written into.


Have in mind you may have more than one network interface on the host. Normally you will have a physical interface and (optional) loopback interface (127.0.0.1).

ServerSocket allows to bind to all (0.0.0.0) or to a specific one with ServerSocket#bind().

So, on the same host you may listen to the same port, but on different network interfaces.


To see if the socket successfully bound, use the srv.isBound() method. This could validate @biziclop 's comment.


You are mistaken. Creating the ServerSocket will prevent other programs binding to that port number for as long as the ServerSocket exists. Apache doesn't have any special magic, this is a basic property of TCP. Maybe your socket is being garbage-collected?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜