Java Socket Programming
Introduction to Java Socket Programming
Java Socket programming allows you to perform network communication between two computers. A java.net.Socket is an endpoint for communication between two machines and can be used to establish a connection for exchanging data. This tutorial will guide you through the basics of Java Socket Programming, explaining the fundamental concepts in a simple, beginner-friendly manner.
Understanding Sockets
A socket represents an endpoint in a network. It can be viewed as a door between your application process and the network. The application sends and receives data through this door to communicate with another application running on a different machine in the network.
In Java, the java.net package provides two classes – Socket and ServerSocket – which implement the client-side and server-side connections, respectively.
Java.net.Socket Class
Java.net.Socket class represents a socket, and is used for creating client sockets.
Here is a simple client socket example:
import java.net.*;
import java.io.*;
class MyClient{
public static void main(String args[])throws Exception{
Socket s=new Socket("localhost",3333);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}}
In the above example, localhost represents the current machine.
Java.net.ServerSocket Class
Java.net.ServerSocket is a java class that provides a system-independent implementation of the server side of a client/server socket connection. The constructor for ServerSocket throws an exception if it cannot listen on the specified port (for example, the port is already being used).
Here is a simple server socket example:
import java.net.*;
import java.io.*;
class MyServer{
public static void main(String args[])throws Exception{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}}
In the above example, the ServerSocket object is created with a specific port number, and the accept() method is used to get the client socket object.
Socket Communication Workflow
The server instantiates a ServerSocket object, denoting which port number communication is to occur on.
The server invokes the accept() method of the ServerSocket class. This method waits until a client connects to the server on the given port.
After the server is waiting, a client instantiates a Socket object, specifying the server name and the port number to connect to.
The constructor of the Socket class attempts to connect the client to the specified server and the port number. If communication is established, the client now has a Socket object capable of communicating with the server.
On the server side, the accept() method returns a reference to a new socket on the server that is connected to the client's socket.
After the connections are established, communication can occur using I/O streams. Each socket has both an OutputStream and an InputStream. The client's OutputStream is connected to the server's InputStream, and the client's InputStream is connected to the server's OutputStream.
TCP is a two-way communication protocol, hence data can be sent across both streams at the same time.
Conclusion
Java Socket programming is a key concept for network communication. Understanding the basics of how to create a client and server socket in Java, and how they interact with each other, is the first step towards mastering Java network programming. This tutorial provides a foundation to start from, but there is still much more to learn. Keep practicing and exploring more complex examples to become proficient in Java Socket programming.