This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Can I use the same socket to to reconnect after the first connection failed. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Can I use the same socket to to reconnect after the first connection failed." Watch "Can I use the same socket to to reconnect after the first connection failed." New topic
Author

Can I use the same socket to to reconnect after the first connection failed.

Johaness Springel
Greenhorn

Joined: Apr 21, 2009
Posts: 3
Hi,

I use socket to connect first to one service, but if the connection fails (is refused)
then I want to reuse this socket to connect to different service. But when I try to
connect to second service (which I'm sure works) I get:
java.net.SocketException: Socket operation on nonsocket: connect

when I call connect() method. Does anybody know why?

The code below reproduces this problem (I use Windows XP and Java 1.5)


public class Runner {

public static void main(String[] args) throws Exception {
new Thread(new Runnable() {
public void run() {
try {
ServerSocket ss = new ServerSocket(50007);
Socket s = ss.accept();
} catch (Exception e) {
System.out.println(e);
}

}
}).start();

Thread.sleep(5000);

Socket socket = new Socket();
InetSocketAddress localAddress = new InetSocketAddress(50000);
socket.bind(localAddress);

try {
socket.connect(new InetSocketAddress("localhost", 50006));
} catch (Exception e) {
System.out.println(e);
}
try {
socket.connect(new InetSocketAddress("localhost", 50007));
} catch (Exception e) {
System.out.println(e);
}
}
}
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Once a socket has been closed, it is not available for further networking use (i.e. can't be reconnected or rebound). A new socket needs to be created.

java.net.Socket documentation


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Johaness Springel
Greenhorn

Joined: Apr 21, 2009
Posts: 3
Yes, it's true, but I don't close it explicitly. And I don't see that connect method does it. Moreover if socket was closed, then during second connection try there would be message:
java.net.SocketException: Socket is closed

(according to source code of class Socket) which is different from the one I get.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I use the same socket to to reconnect after the first connection failed.
 
Similar Threads
multiple clients
Sockets vs URL Based classes
the max number of clients to multithread the socket server
how to connect to other machine rather than localhost.
socket "connection reset"