Create a simplest chat program in Java
This is a simplest chat program .
package Chat;
import java.net.*;
import java.io.*;
import java.util.*;
class ChatServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(9093);
Socket s=ss.accept();
System.out.println(s);
BufferedReader bf=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
try{
while(true)
{
String name=bf.readLine();
System.out.println("Client :" +name);
String x=br.readLine();
pw.println("Server : " + x);
}
}
catch(SocketException se)
{
System.out.println("Client has been closed its connection");
}
}
}
class ChatClient
{
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter ip of server");
String ip=br.readLine();
Socket ss=new Socket(ip,9093);
System.out.println(ss);
PrintWriter pw=new PrintWriter(ss.getOutputStream(),true);
BufferedReader bf=new BufferedReader(new InputStreamReader(ss.getInputStream()));
while(true)
{
String st=br.readLine();
System.out.println("Client : " + st);
pw.println(st);
System.out.println(bf.readLine());
}
}
}
In this above program this program is basically contain one Client and one Server
This chat program can run on any two computer or you can run this simple program
on your local system
Here i execute this program on my local system
First of all you need to complile this program.
now for execution you need to do this
type in your command prompt
start and press "Enter"
it will open duplicate window of your CMD
now in one of the window type
java Chat.ChatServer and then press enter

and now in another window type
java Chat.ChatClient then press "Enter"
and enter the ip address of computer (default ip address of any computer is 127.0.0.1)
so type it.
now it will connect to the server
now start and enjoy your chat.
package Chat;
import java.net.*;
import java.io.*;
import java.util.*;
class ChatServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(9093);
Socket s=ss.accept();
System.out.println(s);
BufferedReader bf=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
try{
while(true)
{
String name=bf.readLine();
System.out.println("Client :" +name);
String x=br.readLine();
pw.println("Server : " + x);
}
}
catch(SocketException se)
{
System.out.println("Client has been closed its connection");
}
}
}
class ChatClient
{
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter ip of server");
String ip=br.readLine();
Socket ss=new Socket(ip,9093);
System.out.println(ss);
PrintWriter pw=new PrintWriter(ss.getOutputStream(),true);
BufferedReader bf=new BufferedReader(new InputStreamReader(ss.getInputStream()));
while(true)
{
String st=br.readLine();
System.out.println("Client : " + st);
pw.println(st);
System.out.println(bf.readLine());
}
}
}
In this above program this program is basically contain one Client and one Server
This chat program can run on any two computer or you can run this simple program
on your local system
Here i execute this program on my local system
First of all you need to complile this program.
now for execution you need to do this
type in your command prompt
start and press "Enter"
it will open duplicate window of your CMD
now in one of the window type
java Chat.ChatServer and then press enter
and now in another window type
java Chat.ChatClient then press "Enter"
and enter the ip address of computer (default ip address of any computer is 127.0.0.1)
so type it.
now it will connect to the server
now start and enjoy your chat.
Comments
Post a Comment