All About Class Socket in Java



Socket class implements client socket.A Socket is an end point for communication between two machines.The actual work of the socket is performed by an instance of the SocketImpl class.An application ,by changing the socket factory that creates the socket implementation,can configure itself to create socket appropriate to local firewall.

The structure of  class Socket is given as:

public final class java.net.Socket extends java.lang.Object{
//constructors
public Socket(InetAddress address,int port);
public Socket(InetAddress address,int port,boolean stream);
public Socket(String host,int port);
public Socket(String host,int port,boolean stream);
//methods
public void close();
public InetAddress getInetAddress();
public InputStream getInputStream();
public int getLocalPort();
public int getPort();
public static void setSocketImplFactory(SocketImplFactory fac);
public OutputStream getOutputStream();
public String toString();
}
 
public Socket(InetAddress address,int port);
Creates a StreamSocket and connects it to the specified port number at the specified IP address.If the application has specified a socket factory,the factory's createSocketImpl() method is called to create the actual socket implementation.Otherwise a plain socket is created.It may throw IOException if an I/O error occurs while opening the socket or creating the socket.
public Socket(InetAddress address,int port,boolean stream);
Creates a socket and connects it to the specified port number at the specified IP address.If the stream argument is true ,this creates a Stream socket.If false it creates a DatagramSocket. It may throw IOException if an I/O error occurs while opening the socket or creating the socket.
public Socket(String host,int port);
Creates a stream socket and connects it to the specified port number on the named host.It may throw IOException if an I/O error occurs while opening the socket or creating the socket.
public Socket(String host,int port,boolean stream);
Creates a stream socket and connects it to the specified port number on the named host.If the stream argument is true ,this creates a Stream socket.If false it creates a DatagramSocket. It may throw IOException if an I/O error occurs while opening the socket or creating the socket.
All About Class Socket in Java All About Class Socket in Java Reviewed by Animesh Chatterjee on October 12, 2018 Rating: 5

No comments:

Powered by Blogger.