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

Creation of A Simple Chat Program

This document describes a simple Java chat program with client and server components. The client program establishes a socket connection to the server, reads user input, sends it to the server, receives responses from the server and displays them. The server program listens for connections on port 2000, receives messages from connected clients, displays them, reads user input and sends responses back to the clients. Both programs close all network connections and input/output streams when the conversation is finished.

Uploaded by

Sai prasad 11
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)
25 views2 pages

Creation of A Simple Chat Program

This document describes a simple Java chat program with client and server components. The client program establishes a socket connection to the server, reads user input, sends it to the server, receives responses from the server and displays them. The server program listens for connections on port 2000, receives messages from connected clients, displays them, reads user input and sends responses back to the clients. Both programs close all network connections and input/output streams when the conversation is finished.

Uploaded by

Sai prasad 11
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

5.

Creation of a Simple Chat Program

Client program

import java.io.*;
import java.net.*;
public class chatclient1
{
public static void main(String args[]) throws Exception
{
Socket sk=new Socket(InetAddress.getLocalHost(),2000);
BufferedReader sin=new BufferedReader(new InputStreamReader(sk.getInputStream()));
PrintStream sout=new PrintStream(sk.getOutputStream());
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String s;
while ( true )
{
System.out.print("Client : ");
s=stdin.readLine();
sout.println(s);
s=sin.readLine();
System.out.print("Server : "+s+"\n");
if ( s.equalsIgnoreCase("BYE") )
break;
}
sk.close();
sin.close();
sout.close();
stdin.close();
}
}

Server program

import java.net.*;
import java.io.*;

public class chatserver1


{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(2000);
Socket sk=ss.accept();
BufferedReader cin=new BufferedReader(new InputStreamReader(sk.getInputStream()));
PrintStream cout=new PrintStream(sk.getOutputStream());
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String s;
while ( true )
{
s=cin.readLine();
if (s.equalsIgnoreCase("END"))
{
cout.println("BYE");
break;
}
System. out.print("Client : "+s+"\n");
System.out.print("Server : ");
s=stdin.readLine();
cout.println(s);
}
ss.close();
sk.close();
cin.close();
cout.close();
stdin.close();
}
}

You might also like