0% found this document useful (0 votes)
3 views

Assignment 3

Uploaded by

Karan Margaje
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment 3

Uploaded by

Karan Margaje
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

3. Create a MultiClient Chat application where multiple Clients are communicating to


the server.

Code:

ClientTwo.java

package Multi_Client_Chat;

import java.io.*;

import java.net.*;

import java.util.*;

/**

* @author Karan

*/

public class ClientTwo {

public static void main(String args[]) throws IOException{

try{

Scanner scan=new Scanner(System.in);

InetAddress ip=InetAddress.getByName("localhost");

Socket s=new Socket(ip,5056);

DataInputStream dis = new DataInputStream(s.getInputStream());

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

while(true){

Subject: MCA L502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

System.out.println(dis.readUTF());

String tosend = scan.nextLine();

dos.writeUTF(tosend);

if(tosend.equals("Exit"))

System.out.println("Closing this connection : " + s);

s.close();

System.out.println("Connection closed");

break;

String received = dis.readUTF();

System.out.println(received);

scan.close();

dis.close();

dos.close();

catch(Exception e)

e.printStackTrace();

Subject: MCA L502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

ServerTwo.java

package Multi_Client_Chat;

import java.io.*;

import java.net.*;

import java.text.*;

import java.util.*;

/**

* @author Karan

*/

public class ServerTwo {

public static void main(String args[]) throws IOException{

ServerSocket ss =new ServerSocket(5056);

while(true){

Socket s=null;

try{

s =ss.accept();

System.out.println("A new client is connected : " + s);

DataInputStream dis = new DataInputStream(s.getInputStream());

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

System.out.println("Assigning new thread for this client");

Subject: MCA L502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Thread t = new ClientHandler(s, dis, dos);

t.start();

catch(Exception e)

s.close();

e.printStackTrace();

class ClientHandler extends Thread

DateFormat fordate = new SimpleDateFormat("yyyy/MM/dd");

DateFormat fortime = new SimpleDateFormat("hh:mm:ss");

final DataInputStream dis;

final DataOutputStream dos;

final Socket s;

public ClientHandler(Socket s, DataInputStream dis, DataOutputStream dos)

Subject: MCA L502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

this.s = s;

this.dis = dis;

this.dos = dos;

@Override

public void run()

String received;

String toreturn;

while (true)

try {

dos.writeUTF("What do you want?[Date | Time]..\n"+

"Type Exit to terminate connection.");

received = dis.readUTF();

if(received.equals("Exit"))

System.out.println("Client " + this.s + " sends exit...");

System.out.println("Closing this connection.");

this.s.close();

Subject: MCA L502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

System.out.println("Connection closed");

break;

Date date = new Date();

switch (received) {

case "Date" :

toreturn = fordate.format(date);

dos.writeUTF(toreturn);

break;

case "Time" :

toreturn = fortime.format(date);

dos.writeUTF(toreturn);

break;

default:

dos.writeUTF("Invalid input");

break;

} catch (IOException e) {

e.printStackTrace();

try

Subject: MCA L502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

this.dis.close();

this.dos.close();

}catch(IOException e){

e.printStackTrace();

Output:

Server

Client-1

Subject: MCA L502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Server

Client-2

Client-1

Subject: MCA L502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Server

Client-2

Subject: MCA L502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Server

Subject: MCA L502 [Choice Based] Open Source System for ADC Lab Nov 2020

You might also like