Java API Tutorial - Java Socket.bind(SocketAddress bindpoint)








Syntax

Socket.bind(SocketAddress bindpoint) has the following syntax.

public void bind(SocketAddress bindpoint)  throws IOException

Example

In the following code shows how to use Socket.bind(SocketAddress bindpoint) method.

/*from ww w  .j av a  2 s .co  m*/

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Main {
  public static void main(String[] argv) throws Exception {
    int port = 80;
   
    Socket socket = new Socket("google.com", port,InetAddress.getByName("java2s.com"),port);
    
    socket.bind(new InetSocketAddress("java2s.com", port));
  }
}