Socket bind Example
Socket class bind example. This example shows you how to use bind method.
Socket class bind example. public void bind(SocketAddress bindpoint) throws IOException Binds the socket to a local address. If the address is null, then the system will pick up an ephemeral port and a valid local address to bind the socket.
Here is the code
/*
* @ # Bind.java
* A class repersenting use to Bind method
* of Socket class in java.net package
* version 19 June 2008
* author Rose India
*/
import java.net.*;
public class Bind {
public static void main(String args[]) throws Exception {
Socket socket = new Socket();
//Connects this socket to the server.
socket.connect(new InetSocketAddress("192.168.10.80", 9090));
//Binds the socket to a local address.
socket.bind(new InetSocketAddress("192.168.10.80",9090));
//close socket
socket.close();
}
} |
Output
Exception in thread "main" java.net.SocketException: Already bound
at java.net.Socket.bind(Socket.java:555)
at Bind.main(Bind.java:21) |
|