JAVA in Sockets
Sathishkumar Nagarajan
GM - Application Engineering at xerago | CISM, Cloud Architecture, Conversational AI, Enterprise Architect, Security Architecture, DevOps and Cloud Serivces
The java.net.Socket Class in Java
We use the Socket class to create the sockets for the client and server. The java.net.Socket class represents the socket. Both the client and the server programs use this socket to communicate with each other. There are some methods in the Socket class.
Methods of Socket Class
The java.net.ServerSocket Class in Java
The java.net.ServerSocket creates a server socket to obtain a port of the server and to listen to the client requests. The object of the ServerSocket class object helps to establish communication with the clients.
Methods of ServerSocket Class
Client-Side Socket Programming
For implementing client-side programming, we need to follow the below steps:
- Create a Socket
- Connect it to ServerSocket by specifying the IP address and the port number
- Get the reference of the OutputStream
- Attach this reference to OutputStreamWriter
- Write and close
- Get the reference of InputStream
- Attach this reference to InputStreamWriter
- Read and Buffer
- Parse, interpret and process it
- Close Connection