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

Ex 3.b - Chat Application

The document describes a Java program to create a chat application using client-server communication. The program uses sockets to establish a connection between the client and server. The server side creates a server socket and accepts client requests, then sends and receives messages using input/output streams. The client side creates a socket to the server IP, and also sends and receives messages using input/output streams. The client and server can then continuously send messages back and forth to enable chatting.

Uploaded by

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

Ex 3.b - Chat Application

The document describes a Java program to create a chat application using client-server communication. The program uses sockets to establish a connection between the client and server. The server side creates a server socket and accepts client requests, then sends and receives messages using input/output streams. The client side creates a socket to the server IP, and also sends and receives messages using input/output streams. The client and server can then continuously send messages back and forth to enable chatting.

Uploaded by

NivedhithaV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Chat Application using Client Server Communication

Aim:

To write a java program to implement Chat application using Java.

Algorithm:

Server Side:

Step -1: Start.

Step -2: Import the necessary packages.

Step -3: Under ChatServer class, create a server socket to communicate with the client
using ServerSocket() constructor.

Step – 4: Accept the request from the client using accept() method.

Step – 5: Establish socket connection between client and server using BufferedReader.

Step -6: Send and receive messages to and from client using getInputStream() and
getOutputStream().

Step – 7: Close the socket.

Client Side:

Step - 1: Start.

Step - 2: Import the necessary packages.

Step - 3: Under ChatClient class, create a new socket with IP address of the server system
using Socket() constructor.

Step – 4: Establish socket connection between client and server using BufferedReader.

Step - 5: Send and receive messages to and from client using getInputStream() and
getOutputStream().

Step – 6: Close the socket.


ChatServer.java

import java.io.*;

import java.net.*;

public class ChatServer

public static void main(String[] args) throws Exception

ServerSocket sersock = new ServerSocket(3000);

System.out.println("Server ready for chatting");

Socket sock = sersock.accept( );

BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));

OutputStream ostream = sock.getOutputStream();

PrintWriter pwrite = new PrintWriter(ostream, true);

InputStream istream = sock.getInputStream();

BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

String receiveMessage, sendMessage;

while(true)

if((receiveMessage = receiveRead.readLine()) != null)

System.out.println(receiveMessage);

sendMessage = keyRead.readLine();

pwrite.println(sendMessage);
pwrite.flush();

ChatClient.java

import java.io.*;

import java.net.*;

public class ChatClient

public static void main(String[] args) throws Exception

Socket sock = new Socket("10.10.16.41", 3000);

BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));

OutputStream ostream = sock.getOutputStream();

PrintWriter pwrite = new PrintWriter(ostream, true);

InputStream istream = sock.getInputStream();

BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

System.out.println("Start the chat, type and press Enter key");

String receiveMessage, sendMessage;

while(true)

sendMessage = keyRead.readLine();

pwrite.println(sendMessage);

pwrite.flush();

if((receiveMessage = receiveRead.readLine()) != null)


{

System.out.println(receiveMessage);

Output:

Result:
Thus the Java program to implement Chat application using Client Server Communication is
written, executed and the output is verified.

You might also like