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 a flexible access of 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 is
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
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
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.
OurServer.java
import java.net.*;
import java.io.*;
public class OurServer
{
public static void main(String args[])
{
try
{
ServerSocket serskt=new ServerSocket(9999);
Socket skt=serskt.accept();//establishes connection
DataInputStream dinptstr=new DataInputStream(skt.getInputStream());
String st=(String)dinptstr.readUTF();
System.out.println("message= "+st);
serskt.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
OurClient.java
import java.net.*;
import java.io.*;
public class OurClient
{
public static void main(String args[])
{
try
{
Socket skt=new Socket("localhost",9999);
DataOutputStream doutstr=new DataOutputStream(skt.getOutputStream());
doutstr.writeUTF("Connect To Server");
doutstr.flush();
doutstr.close();
skt.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
Output
To get the proper output, in other words output that shows a communication, we need to open two command shells. To start the compilation, first we must compile and run the OurServer.java file then we must wait untill the OurClient.java file is not run on the second command shell.
In the previous figure we saw a message is generated showing ("Connect To Server"). This message is generated after compiling and running the OurClient.java file. This shows a communication between both the client and the server.