Chat Server

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

‭12‬

‭Lab 7. Program to implement Chat Server‬


‭Code:‬
‭ClientSide.java‬
i‭mport java.net.Socket;‬
‭import java.io.BufferedReader;‬
‭import java.io.IOException;‬
‭import java.io.InputStreamReader;‬
‭import java.io.OutputStream;‬
‭import java.io.PrintStream;‬
‭public class ClientSide‬
‭{‬
‭public static void main(String[] args) throws IOException {‬
‭Socket s = new Socket("localhost",2019);‬
‭System.out.println("Client Started, waiting for server response..");‬
‭BufferedReader br = new BufferedReader(‬
‭new InputStreamReader(System.in)‬
‭);‬

‭ utputStream os = s.getOutputStream();‬
O
‭BufferedReader br1 = new BufferedReader(‬
‭new InputStreamReader(s.getInputStream())‬
‭);‬
‭PrintStream ps = new PrintStream(os);‬
‭do{‬
‭System.out.print("Client: ");‬
‭String msg = br.readLine();‬
‭ps.println(msg);‬
‭String res = br1.readLine();‬
‭System.out.println("Server Send:"+res+"\n\n");‬
‭}‬
‭while(true);‬
‭}‬
‭};‬

‭ServerSide.java‬
i‭mport java.net.ServerSocket;‬
‭import java.net.Socket;‬
‭import java.io.BufferedReader;‬
‭import java.io.IOException;‬
‭import java.io.OutputStream;‬
‭import java.io.PrintStream;‬
‭import java.io.InputStreamReader;‬
‭public class ServerSide‬
‭{‬
‭public static void main(String[] args) throws IOException‬
‭13‬

‭{‬
‭ erverSocket s = new ServerSocket(2019);‬
S
‭System.out.println("Server Started, waiting for client");‬
‭Socket s1 = s.accept();‬
‭// Client Send‬
‭BufferedReader br = new BufferedReader(‬
‭new InputStreamReader(s1.getInputStream())‬
‭);‬
‭OutputStream out = s1.getOutputStream();‬
‭PrintStream ps = new PrintStream(out);‬

‭BufferedReader br1 = new BufferedReader(‬


‭new InputStreamReader(System.in)‬
‭);‬

‭do{‬
‭String res = br.readLine();‬

‭System.out.println("Client Send: "+res);‬

‭ ystem.out.print("Server: ");‬
S
‭String msg = br1.readLine();‬
‭System.out.print("\n\n");‬
‭ps.println(msg);‬

}‭ ‬
‭while(true);‬
‭}‬
‭}‬

‭Output:‬

You might also like