Create chat application using either TCP or UDP protocol.

Practical : 1
Subject : Advanced Java
Aim : Create chat application using either TCP or UDP protocol.


1. Program: Using TCP

Server Code: 
Download :
Share via Facebook / Twitter / Google Plus to see Link Download
--------------------------------------------------------------------------------
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Server {
            public static void main(String[] args) throws Exception
            {
                        ServerSocketss=new ServerSocket(7888);
                        Socket s=ss.accept();
                        DataInputStream din=new DataInputStream(s.getInputStream());
                        String str;
                        str=din.readUTF();
                        System.out.println("Client:\t"+str);
                        DataOutputStreamdout=new DataOutputStream(s.getOutputStream());
                        DataInputStreammsg=new DataInputStream(System.in);
                        while(true)
                        {
                                    str=din.readUTF();
                                    System.out.print("Client:\t"+str);
                                    System.out.print("Server:\t");
                                    str=msg.readLine();
dout.writeUTF(str);
                        }
            }
}
--------------------------------------------------------------------------------
Client Code: 
Download : Client.java
--------------------------------------------------------------------------------
import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client
{
            public static void main(String[] args) throws Exception
            {
                        Socket s=new Socket("127.0.0.1",7888);
                        if(s.isConnected())
                        {
                                    System.out.println("Connected to server");
                        }
                        DataInputStreammsg=new DataInputStream(System.in);
                        String str="Start Chat..............................................................................................";
                        DataOutputStreamdout=new DataOutputStream(s.getOutputStream());
                        dout.writeUTF(str);
                        System.out.println(str);
                        DataInputStream din=new DataInputStream(s.getInputStream());
                        while(true)
                        {
                                    System.out.print("Client:\t");
                                    str=msg.readLine();
                                    dout.writeUTF(str+"\n");
                                    str=din.readUTF();
                                    System.out.println("Server:\t"+str);
                        }
            }
}
--------------------------------------------------------------------------------
Output:
Server

Client



2. Program: Using UDP

Server Code: 
Download : Server.java
--------------------------------------------------------------------------------
import java.io.*;
import java.net.*;
public class Server {
            public static void main(String[] args) throws Exception
            {
                        DatagramSocket socket=new DatagramSocket(9861);
                        byte receiveByte[]=new byte[1024];
                        byte sendByte[]=new byte[1024];
                        while(true)
                        {
                                    //data receiving
                                    DatagramPacketreceivePacket=new DatagramPacket(receiveByte,receiveByte.length);
                                    socket.receive(receivePacket);
                                    String receiveStr=new String(receivePacket.getData());
                                    receiveStr=receiveStr.trim();
                                    System.out.println("Client:"+receiveStr);
                                   
                                    //data sending
                                    DataInputStream din=new DataInputStream(System.in);
                                    System.out.print("Server:");
                                    String sendStr=din.readLine();
                                    sendByte=sendStr.getBytes();
                                    InetAddressip=receivePacket.getAddress();
                                    int port=receivePacket.getPort();
                                    DatagramPacketsendPacket=new DatagramPacket(sendByte,sendByte.length,ip,port);
                                    socket.send(sendPacket);
                        }
            }

}
--------------------------------------------------------------------------------
Client Code: 
Download : Client.java
--------------------------------------------------------------------------------
import java.net.*;
import java.io.*;
public class Client {
            public static void main(String[] args) throws Exception
            {
                        DatagramSocket socket=new DatagramSocket();
                        InetAddressip=InetAddress.getLocalHost();
                        byte sendByte[]=new byte[1024];
                        byte receiveByte[]=new byte[1024];
                        while(true)
                        {
                                    //Data Sending
                                    DataInputStream din=new DataInputStream(System.in);
                                    System.out.print("Client:");
                                    String sendStr=din.readLine();
                                    sendByte=sendStr.getBytes();
                                    DatagramPacketsendPacket=new DatagramPacket(sendByte,sendByte.length,ip,9861);
                                    socket.send(sendPacket);
                                   
                                    //Data Receiving
                                    DatagramPacketreceivePacket=new DatagramPacket(receiveByte,receiveByte.length);
                                    socket.receive(receivePacket);
                                    String receiveStr=new String(receivePacket.getData());
                                    receiveStr=receiveStr.trim();
                                    System.out.println("Server:"+receiveStr);
                        }
            }

}
--------------------------------------------------------------------------------
Output:
Server


Client


First

4 comments

Write comments
Unknown
AUTHOR
18 January 2017 at 06:18 delete

readline() method is depricated fix it....

Reply
avatar
Birju
AUTHOR
18 January 2017 at 07:01 delete

Yea,but it still works in latest JDK.

Reply
avatar
crimy2000
AUTHOR
15 July 2018 at 16:31 delete

c o o l tutorial :)
thank you sir

Reply
avatar
24 May 2019 at 22:03 delete

Thanks for sharing information on how to create chatting application using either TCP or UDP protocol.

Reply
avatar

Ads