Two way Chatting example in java using tcp socket
Java Chat application using TCP java socket programing example. The program communicates with other through port number 3000 and 2000, developer can change this to what ever they want. avoid using first 1024 reserved ports. The program search the other system by its computer name, so please provide the name correctly. The program is so simple, easy to understand. The interface here used is to create two variable for storing port number. And explaining the code: Getting user input as string, converting them to bytes, storing it in a variable then sent through datagram socket as datagram packets through a port number specified. On the other hand the program keep on listening to that port number, if a request comes through that socket , it will receive it as byte convert to string the display the message. JAVA thread is used for listening
System 1
/**
Chatting example in java
javabelazy
*/
import java.io.*;
import java.net.*;
interface pen{
final int cp=3000;
final int sp=2000;
}
public class Cli5 implements pen{
public static void main(String[] args)throws Exception {
byte buf1[]=new byte[1024];
DatagramSocket d=new DatagramSocket();
DataInputStream b=new DataInputStream(System.in);
tchat t=new tchat();
t.start();
System.out.println("Chatting programme");
while(true){
String s=b.readLine();
buf1=s.getBytes();
d.send(new DatagramPacket(buf1,s.length(),InetAddress.getByName("Redmi"),cp));
if(s.equals("end"))
System.exit(0);
}
}
}
class tchat extends Thread implements pen{
tchat() {}
byte buf2[]=new byte[1024];
public void run(){
try{
DatagramSocket dr=new DatagramSocket(sp);
while(true){
DatagramPacket p=new DatagramPacket(buf2,buf2.length);
dr.receive(p);
String s =new String(p.getData(),0,p.getLength());
System.out.println("Server :"+s);
System.out.println("Client :");
if(s.equals("end"))
System.exit(0);
}
}
catch(Exception e)
{}
}
}
-----------------------------------------------------------------------------
System 2
How to create a bi directional chat application using java networking
/**
Chatting example in java
javabelazy
*/
import java.io.*;
import java.net.*;
interface pen{
final int cp=2000;
final int sp=3000;
}
public class Cli4 implements pen{
public static void main(String[] args)throws Exception {
byte buf1[]=new byte[1024];
DatagramSocket d=new DatagramSocket();
DataInputStream b=new DataInputStream(System.in);
tchat t=new tchat();
t.start();
System.out.println("Chatting program");
while(true){
String s=b.readLine();
buf1=s.getBytes();
d.send(new DatagramPacket(buf1,s.length(),InetAddress.getByName("Watsapp"),cp));
if(s.equals("end"))
System.exit(0);
}
}
}
class tchat extends Thread implements pen{
tchat() {}
byte buf2[]=new byte[1024];
public void run(){
try{
DatagramSocket dr=new DatagramSocket(sp);
while(true){
DatagramPacket p=new DatagramPacket(buf2,buf2.length);
dr.receive(p);
String s =new String(p.getData(),0,p.getLength());
System.out.println("client :"+s);
if(s.equals("end"))
System.exit(0);
}
}
catch(Exception e)
{}
}
}
The chat application currently work on your intranetwork
Thanking you....
No comments:
Post a Comment
Your feedback may help others !!!