/* Implementation of Talk Using TCP */
// ChatServer.java : A Simple Talk Server Program
import java.io.*;
import java.net.*;
class ChatServer
{
public static void main(String args[]) throws Exception
{
try
{
int Port;
BufferedReader Buf =new BufferedReader(new
InputStreamReader(System.in));
System.out.print(" Enter the Port Address : " );
Port=Integer.parseInt(Buf.readLine());
ServerSocket sok =new ServerSocket(Port);
System.out.println(" Server is Ready To Receive a Message. ");
System.out.println(" Waiting ..... ");
Socket so=sok.accept();
if(so.isConnected()==true)
System.out.println(" Client Socket is Connected Succecfully. ");
InputStream in=so.getInputStream();
OutputStream ou=so.getOutputStream();
PrintWriter pr=new PrintWriter(ou);
BufferedReader br1=new BufferedReader(new
InputStreamReader(System.in));
BufferedReader br2=new BufferedReader(new
InputStreamReader(in));
String str;
while(true)
{
System.out.println(" Mesage From Client : " + br2.readLine() );
System.out.print(" Enter the Message : ");
str=br1.readLine();
pr.println(str);
pr.flush();
}
}
catch(Exception e)
{
System.out.println(" Error : " + e.getMessage());
}
}
}
// ChatClient.java : A Simple Talk Client Program
import java.io.*;
import java.net.*;
class ChatClient
{
public static void main(String args[]) throws Exception
{
try
{
int Port;
BufferedReader Buf =new BufferedReader(new
InputStreamReader(System.in));
System.out.print(" Enter the Port Address : " );
Port=Integer.parseInt(Buf.readLine());
Socket sok=new Socket("localhost",Port);
if(sok.isConnected()==true)
System.out.println(" Server Socket is Connected Successfully. ");
InputStream in=sok.getInputStream();
OutputStream ou=sok.getOutputStream();
PrintWriter pr=new PrintWriter(ou);
BufferedReader br1=new BufferedReader(new
InputStreamReader(in));
BufferedReader br2=new BufferedReader(new
InputStreamReader(System.in));
String str;
while(true)
{
System.out.print(" Enter the Message : ");
str=br2.readLine();
pr.println(str);
pr.flush();
System.out.println(" Message From Server : " + br1.readLine());
}
}
catch(Exception e)
{
System.out.println(" Error : " + e.getMessage());
}
}
}
OUTPUT :
ChatServer.java :
javac ChatServer.java
java ChatServer
Enter the Port Address : 1234
Server is Ready To Receive a Message.
Waiting .....
Client Socket is Connected Succecfully.
Mesage From Client : hi
Enter the Message : Welcome To JCE
Mesage From Client : I am Baran
Enter the Message : Save the file and do the work
Error : Connection reset
ChatClient.java :
javac ChatClient.java
java ChatClient
Enter the Port Address : 1234
Server Socket is Connected Successfully.
Enter the Message : hi
Message From Server : Welcome To JCE
Enter the Message : I am Baran
Message From Server : Save the file and do the work
Enter the Message : leave the lab
1 comment:
very useful...thanks.
Post a Comment