In this Program We Need Two Java Classes Named:-
1. Client
2. Server
3. ClientServerDemo
Program:-
Client.java:-
package com.gpm.tcp_client_server_2;
import java.io.*;
import java.net.*;
public class Client {
private Socket
client_socket = null;
private
DataOutputStream output_stream = null;
private
DataInputStream input_stream = null;
public
Client(String host_address, int port_number) throws UnknownHostException,
IOException {
try {
//socket
initialization
client_socket =
new Socket(host_address, port_number);
System.out.println("Connected to
Server");
output_stream =
new DataOutputStream(client_socket.getOutputStream());
//client to server
communication
System.out.println("Sending message to
server >>");
output_stream.writeUTF("Hello Server
..!!!");
//Server to client
communication
System.out.println("Accepting message
from server >>");
input_stream = new
DataInputStream(client_socket.getInputStream());
String message =
input_stream.readUTF().toString();
System.out.println(message);
System.out.println();
//Closing
connections
output_stream.close();
client_socket.close();
input_stream.close();
} catch
(UnknownHostException e) {
System.out.println(e);
} catch
(IOException e) {
System.out.println(e);
}}
Server.java:-
package
com.gpm.tcp_client_server_2;
import
java.io.*;
import
java.net.*;
import
java.util.ArrayList;
public
class Server
{
private ServerSocket server_socket = null;
private Socket client_socket =null;
private DataInputStream input_stream = null;
private DataOutputStream output_stream = null;
private ArrayList<Socket>
clientSocketList;
private int maxClientSize = 2;
public Server(int port_number)
{
clientSocketList = new ArrayList<Socket>();
try
{
//Initialization of server side socket
server_socket = new ServerSocket(port_number);
System.out.println("Server started
>>");
//size 0 for client 1 and size 1 for client 2
while(clientSocketList.size() <
maxClientSize)
{
System.out.println("Waiting for a client
>>");
client_socket = server_socket.accept();
System.out.println("Client
accepted");
//Receiving message from client socket
input_stream = new
DataInputStream(newBufferedInputStream(client_socket.getInputStream()));
String message = input_stream.readUTF();
System.out.println(message);
//Sending reply to client
output_stream = new
DataOutputStream(client_socket.getOutputStream());
System.out.println("Sending reply to
client >>");
output_stream.writeUTF("Hello
Client...!!");
System.out.println();
//Adding client in arraylist
clientSocketList.add(client_socket);
}
System.out.println("Max Clients limit has
been reached");
System.out.println("Displaying data of
client connected");
System.out.println();
AdvertiseClient();
}
catch(IOException e)
{ System.out.println(e); }
}
private InputStream
newBufferedInputStream(InputStream inputStream) {
return inputStream;
}
private void AdvertiseClient()
{
// print data of all clients
for(Socket client_socket : clientSocketList)
{
System.out.println("Client Data
>>");
System.out.println(client_socket.toString());
System.out.println();
}
}
public static void main(String args[])
{
Server server = new Server(7000);
}
}
ClientServerDemo.java:-
package
com.gpm.tcp_client_server_2;
import
java.io.IOException;
import
java.net.UnknownHostException;
public
class ClientServerDemo
{
public static void main(String args[])
{
try
{
//local host address
String host_address = "127.0.1.1";
System.out.println("Starting client 1
>>");
Client client1 = new
Client(host_address,7000);
System.out.println("Starting client 2
>>");
Client client2 = new
Client(host_address,7000);
}
catch(UnknownHostException e)
{ System.out.println(e); }
catch(IOException e)
{ System.out.println(e);
}
}
}
Output:-
Server
ClientServerDemo