A Simple Client-Server Chat Application in Java.
This article will explore a basic implementation of a client-server chat application using Java. This application demonstrates how two programs can communicate over a network using sockets. The server listens for incoming connections from a client, and the client sends a message to the server upon connection.
Introduction to Java Sockets
Java Sockets are the endpoints for communication between two machines. The Java Socket class belongs to the java.net package and enables reading from and writing to a remote server for network operations. These sockets are very important in developing client-server applications.
Server component
The server is the backbone of the chat application. It waits for clients to connect and handles incoming messages. Below is a simplified version of a server designed to accept a connection from a single client and print a received message:
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
System.out.println("this is the server running...");
ServerSocket serverSocket = new ServerSocket(8090);
System.out.println("waiting for client to connect...");
Socket socket = serverSocket.accept();
System.out.println("Client connected...");
//read the message coming from client
InputStream inputStream = socket.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
String message = dataInputStream.readUTF();
System.out.println(message);
}
}
This server listens on port 8090. Once a client connects, it reads a UTF-encoded message from the client and displays it.
Client component
The client's role is to initiate the connection to the server, send a message, and then terminate. Here’s how a basic client looks:
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
System.out.println("this is the client running...");
Socket socket =new Socket("localhost", 8090);
System.out.println("server connected");
//send a message to the server
OutputStream outputStream = socket.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
dataOutputStream.writeUTF("Hello server, I am the client");
dataOutputStream.flush();
dataOutputStream.close();
socket.close();
}
}
The client connects to the server at "localhost" and port 8090, sends a greeting message, and then closes the connection.
Recommended by LinkedIn
Running the application
Before starting the client, the server must be running and ready to accept incoming connections. The server listens for a client connection on a specified port.
Socket socket = serverSocket.accept();
The accept() method blocks the execution of the server's code until a client tries to connect. This means the server will wait indefinitely at this point until a client initiates a connection.
Once a client attempts to connect, the accept() method returns a Socket object, representing the established connection between the server and the client. At this point, the server is ready to communicate with the client.
When the client runs, it immediately tries to connect to the server using the specified IP address and port.
Socket socket = new Socket("localhost", 8090);
As soon as this line of code is executed, the client successfully connects to the server, assuming the server is already running and listening on the specified port. The server, which has been waiting for a connection, now receives the connection request and proceeds to the next step.
After the connection is established, the client sends a message to the server using the output stream. Once the message is sent, the client closes the connection and terminates the program.
This simple client-server application is a fundamental example of network programming in Java. It demonstrates how to set up a basic communication channel between two programs using sockets. Understanding this basic concept is crucial for more complex network applications like chat programs, multiplayer games, or distributed systems.