0% found this document useful (0 votes)
12 views2 pages

Chat Server

This document contains code for a chat server program with client and server sides written in Java. The client code connects to the server socket on port 2019, reads user input and sends it to the server. It also receives and prints responses from the server. The server code waits for a client connection on port 2019, reads messages from the client and prints them, then takes user input and sends it back to the client. The programs run in a continuous loop for an ongoing chat session.

Uploaded by

Vishu Aasliya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Chat Server

This document contains code for a chat server program with client and server sides written in Java. The client code connects to the server socket on port 2019, reads user input and sends it to the server. It also receives and prints responses from the server. The server code waits for a client connection on port 2019, reads messages from the client and prints them, then takes user input and sends it back to the client. The programs run in a continuous loop for an ongoing chat session.

Uploaded by

Vishu Aasliya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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