JAVA CODE FOR TCP CHAT APPLICATION | URDINESH

Software Programming, Tutorials, Interview Preparations,Stock Market,BSE/NSE, General informations

Tuesday, May 20, 2014

JAVA CODE FOR TCP CHAT APPLICATION


PROGRAM: (SERVER)
import java.io.*;
import java.net.*;
class ChatS
{
public static void main(String args[])throws IOException
{
ServerSocket s=new ServerSocket(3300);
Socket s1=s.accept();
for(;;)
{
DataInputStream d1=new DataInputStream(s1.getInputStream());
String s2=d1.readUTF();
System.out.println("client:"+s2);
DataInputStream b=new DataInputStream(System.in);
String s3=b.readLine();
DataOutputStream d3=new DataOutputStream(s1.getOutputStream());
System.out.print(“server: “);
d3.writeUTF(s3);
}  
}  
}
PROGRAM: (CLIENT)
import java.io.*;
import java.net.*;
class ChatC
{
public static void main(String args[])throws IOException
{
Socket s1=new Socket("127.0.0.1",3300);
for(;;)
{
DataInputStream dis=new DataInputStream(System.in);
String str=dis.readLine();
DataOutputStream ds=new DataOutputStream(s1.getOutputStream());
System.out.print(“client: “);
ds.writeUTF(str);
DataInputStream d1=new DataInputStream(s1.getInputStream());
String Str1=d1.readLine();
System.out.println("server:"+Str1);    
}   
}  
}






OUTPUT: (CLIENT)

client: Hai
From server:How are you
client: fine


OUTPUT: (SERVER)


From client:Hai
server: How are you

From client: fine

No comments:

Post a Comment

Thanks for your valuable comments

Followers