Java Program -
Client.java -
package com.gpm.tcp_client_server_3;
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 {
client_socket = new Socket(host_address, port_number);
System.out.println("Connected to the server");
output_stream = new DataOutputStream(client_socket.getOutputStream());
System.out.println("Sending message to the server");
output_stream.writeUTF("Hello Server");
System.out.println("Accepting message from the server");
input_stream = new DataInputStream(client_socket.getInputStream());
String message = input_stream.readUTF().toString();
System.out.println(message);
System.out.println();
output_stream.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_3;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.net.*;
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;
private Server(int port_number)
{
clientSocketList = new ArrayList<Socket>();
try
{
server_socket = new ServerSocket(port_number);
System.out.println("Server started");
while(clientSocketList.size()<maxClientSize)
{
System.out.println("Waiting for client");
client_socket = server_socket.accept();
System.out.println("Client accepted");
input_stream = new DataInputStream(new BufferedInputStream(client_socket.getInputStream()));
String message = input_stream.readUTF();
System.out.println(message);
output_stream = new DataOutputStream(client_socket.getOutputStream());
System.out.println("Sending reply to client");
output_stream.writeUTF("Hello Client");
System.out.println();
clientSocketList.add(client_socket);
}
System.out.println("Max clients limit has been reached");
System.out.println("Displaying the data of client connected");
System.out.println();
AdvertiseClient();
}
catch(IOException e)
{
System.out.println(e);
}
//private void AdvertiseClient()
}
private void AdvertiseClient() {
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(5000);
}
}
ClientServerDemo.java -
package com.gpm.tcp_client_server_3;
import java.io.IOException;
import java.net.UnknownHostException;
public class ClientServerDemo {
public static void main(String args[])
{
try
{
String host_address = "192.148.0.1";
System.out.println("Starting client 1");
Client client1 = new Client(host_address,5000);
System.out.println("Starting client 2");
Client client2 = new Client(host_address,5000);
}
catch (UnknownHostException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Output -
First of all, run the Server.java then run the ClientServerDemo.java.
0 Comments