java.net.Socket Class in Java

29 Mar, 2021
The java.net.Socket class allows us to create socket objects that help us in implementing all fundamental socket operations. We can perform various networking operations such as sending, reading data and closing connections. Each Socket object that has been created using with java.net.Socket class has been associated exactly with 1 remote host, for connecting to another different host, we must create a new socket object.
The syntax for importing Socket class from java.net package :
import java.net.Socket;
 Methods used in Socket class : 
Applications of Socket Class:
1. Socket class is implemented in creating a stream socket and which is connected to a specified port number and port address.
public Socket(InetAddress address, int port)
2. Socket class is used for the creation of socket and connecting to the specified remote address on the specified remote port in javax.net
    SocketFactory.createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
3. In javax.ssl.net, the Socket class is used to return a socket layered over an existing socket connected to the named host at a given port.
SSLSocketFactory.createSocket(Socket s, String host, int port, boolean autoClose)
4. In javax.rmi.ssl, Socket class is used for creating an SSL socket.
SslRMIClientSocketFactory.createSocket(String host, int port)
5. In java.nio.channels, Socket class is used for retrieving a socket associated with its channel
SocketChannel.socket()
Java Example for Socket class Implementation :
1. For Server Side:
Java
import java.io.*;import java.net.*;public class MyServer {    public static void main(String[] args)    {        try {            ServerSocket ss = new ServerSocket(6666);                        // establishes connection            Socket soc = ss.accept();                        // invoking input stream            DataInputStream dis                = new DataInputStream(s.getInputStream());                        String str = (String)dis.readUTF();                        System.out.println("message= " + str);                        // closing socket            ss.close();                    } // for catching Exception in run time        catch (Exception e) {            System.out.println(e);        }    }}
Output on Client :
2. For Client Side :
Java
import java.io.*;import java.net.*;public class MyClient {    public static void main(String[] args)    {        try {                        // initializing Socket            Socket soc = new Socket("localhost", 6666);                        DataOutputStream d = new DataOutputStream(                soc.getOutputStream());                        // message to display            d.writeUTF("Hello GFG Readers!");                              d.flush();                        // closing DataOutputStream            d.close();                        // closing socket            soc.close();        }                // to initialize Exception in run time        catch (Exception e) {            System.out.println(e);        }    }}
Output on server :
Java-net-package
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
ArrayList in Java
Collections in Java
Stream In Java
Multidimensional Arrays in Java
Singleton Class in Java
Set in Java
Stack Class in Java
Initializing a List in Java
Introduction to Java