Introduction
In this article, we discuss socket programming in Java.
Before explaining sockets we need to learn some concepts of networking.
Networking
Networking is a concept of connecting two or more computing devices together so that we can share resources. The term network programming refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network.
Advantages
- Provides flexible access to files and data over a network.
- Sharing resources.
- Security.
- Speed.
- Centralized software management.
- Provide security like sending sensitive (password protected) files and programs on a network.
Network Terminology
The following are networking terms:
- MAC Address
- IP address
- Protocol
- Port no.
- Connection-oriented and Connectionless protocol
- Socket
IP Address
IP Address is a unique number assigned to a node of a network, for example, 192.168.0.152. It is composed of octets that range from 0 to 255.
Protocol
Protocol is a set of defined rules for communication between client and server. For example:
- SMTP
- TCP
- FTP
- Telnet
- POP etc.
Now the discussion.
What Socket Programming in Java?
Socket Programming is used for communication between machines using a Transfer Control Protocol (TCP). It can be connectionless or connection-oriented. ServerSocket and Socket classes are used for connection-oriented socket programming. After creating a connection, the server develops a socket object on its end of the connection. The server and client now starts communicating by writing to and reading from the socket.
The client needs to learn two basic peices of information, which are:
- Port number
- IP address of server
Socket class in Java
A socket is simply an endpoint for communications between the machines. The Socket class can be used to create a socket.
The following figure shows a communication structure between two machines, in this figure, the communication is done using a socket over the internet.
Some commonly used public methods of the Socket class are:
- synchronized void close()
- Socket (String host, int port) thows UnknownHostException, IOException
- Socket()
- OutputStream getOutputStream()
- InputStream getInputStream()
ServerSocket class in Java
This class can be used to create a server socket. This object establishes the communication with the clients.
The java.net.ServerSocket class is used by server applications to obtain a port and listen for client requests
Some commonly used public methods of the ServerSocket class are:
- OutputStream getOutputStream()
- synchronized void close()
- Socket accept()
- InputStream getInputStream()
Example
In this example, we create two classes OurServer.java and OurClient.java. The first class (OurServer.java) is for the server and the second class (OurClient.java) is for the client. When we run our program it shows the communication between both the client and the server, when we run the client program a message is generated on the server side that indicates the connection between them.


Join the conversation! Your thoughts help the community grow.