Chat Application Write a GUI Java program to implement a client/server chat application using Java sockets and threads.
Posted: Fri May 20, 2022 10:50 am
import java.net.*;import java.util.*;import java.io.*;class ReceiverThread extends Thread{ Socket s; public ReceiverThread(Socket s2){ super(); this.s=s2; } public void run(){ Scanner sr; try { sr = new Scanner (s.getInputStream()); String str; do{ str=sr.nextLine(); System.out.println("Client "+str); }while(true); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}class SenderThread extends Thread{ Socket s; public SenderThread(Socket s2){ super(); this.s=s2; } public void run(){ Scanner sc = new Scanner (System.in); try { PrintWriter pw = new PrintWriter(s.getOutputStream(),true); String st; do{ st=sc.nextLine(); pw.println("Server "+st); }while(true); } catch (IOException e) { e.printStackTrace(); } }}public class SERVER { public static void main(String[] args) throws UnknownHostException, IOException { ServerSocket ss = new ServerSocket(1534); Socket s= ss.accept(); SenderThread t = new SenderThread(s); ReceiverThread t1 = new ReceiverThread(s); t.start(); t1.start(); }}package ss;import java.net.*;import java.util.*;import java.io.*;class ReceiverThread extends Thread{ Socket s; public ReceiverThread(Socket s2){ super(); this.s=s2; } public void run(){ Scanner sr; try { sr = new Scanner (s.getInputStream()); String str; do{ str=sr.nextLine(); System.out.println("Server "+str); }while(true); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}class SenderThread extends Thread{ Socket s; public SenderThread(Socket s2){ super(); this.s=s2; } public void run(){ Scanner sc = new Scanner (System.in); try { PrintWriter pw = new PrintWriter(s.getOutputStream(),true); String st; do{ st=sc.nextLine(); pw.println("Client "+st); }while(true); } catch (IOException e) { e.printStackTrace(); } }}public class Client { public static void main(String[] args) throws UnknownHostException, IOException { Socket s = new Socket(InetAddress.getLocalHost(),1534);SenderThread t = new SenderThread(s);ReceiverThread t1 = new ReceiverThread(s);t.start();t1.start(); }}
import java.net.*;
import java.util.*;
import java.io.*;
class ReceiverThread extends Thread{
Socket s;
public ReceiverThread(Socket s2){
super();
this.s=s2;
}
public void run(){
Scanner sr;
try {
sr = new Scanner (s.getInputStream());
String str;
do{
str=sr.nextLine();
System.out.println("Client "+str);
}while(true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class SenderThread extends Thread{
Socket s;
public SenderThread(Socket s2){
super();
this.s=s2;
}
public void run(){
Scanner sc = new Scanner (System.in);
try {
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
String st;
do{
st=sc.nextLine();
pw.println("Server "+st);
}while(true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class SERVER {
public static void main(String[] args) throws UnknownHostException, IOException {
ServerSocket ss = new ServerSocket(1534);
Socket s= ss.accept();
SenderThread t = new SenderThread(s);
ReceiverThread t1 = new ReceiverThread(s);
t.start();
t1.start();
}
}
package ss;
import java.net.*;
import java.util.*;
import java.io.*;
class ReceiverThread extends Thread{
Socket s;
public ReceiverThread(Socket s2){
super();
this.s=s2;
}
public void run(){
Scanner sr;
try {
sr = new Scanner (s.getInputStream());
String str;
do{
str=sr.nextLine();
System.out.println("Server "+str);
}while(true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class SenderThread extends Thread{
Socket s;
public SenderThread(Socket s2){
super();
this.s=s2;
}
public void run(){
Scanner sc = new Scanner (System.in);
try {
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
String st;
do{
st=sc.nextLine();
pw.println("Client "+st);
}while(true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket(InetAddress.getLocalHost(),1534);
SenderThread t = new SenderThread(s);
ReceiverThread t1 = new ReceiverThread(s);
t.start();t1.start();
}
}
Chat Application Write a GUI Java program to implement a client/server chat application using Java sockets and threads. Server Server Client Connected to localhost Send Project instructions