Advertisement
LegendSujay2019

A Group chat application in Java

Aug 26th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.31 KB | None | 0 0
  1. A Group chat application in Java
  2. In this post, a group chat application using MulticastSocket (Java Platform SE 7) class is discussed. A MulticastSocket is a (UDP) DatagramSocket, with additional capabilities for joining “groups” of other multicast hosts on the internet.
  3.  
  4. Implementation
  5.  
  6. filter_none
  7. edit
  8. play_arrow
  9.  
  10. brightness_4
  11. import java.net.*;
  12. import java.io.*;
  13. import java.util.*;
  14. public class GroupChat
  15. {
  16.     private static final String TERMINATE = "Exit";
  17.     static String name;
  18.     static volatile boolean finished = false;
  19.     public static void main(String[] args)
  20.     {
  21.         if (args.length != 2)
  22.             System.out.println("Two arguments required: <multicast-host> <port-number>");
  23.         else
  24.         {
  25.             try
  26.             {
  27.                 InetAddress group = InetAddress.getByName(args[0]);
  28.                 int port = Integer.parseInt(args[1]);
  29.                 Scanner sc = new Scanner(System.in);
  30.                 System.out.print("Enter your name: ");
  31.                 name = sc.nextLine();
  32.                 MulticastSocket socket = new MulticastSocket(port);
  33.              
  34.                 // Since we are deploying
  35.                 socket.setTimeToLive(0);
  36.                 //this on localhost only (For a subnet set it as 1)
  37.                  
  38.                 socket.joinGroup(group);
  39.                 Thread t = new Thread(new
  40.                 ReadThread(socket,group,port));
  41.              
  42.                 // Spawn a thread for reading messages
  43.                 t.start();  
  44.                  
  45.                 // sent to the current group
  46.                 System.out.println("Start typing messages...\n");
  47.                 while(true)
  48.                 {
  49.                     String message;
  50.                     message = sc.nextLine();
  51.                     if(message.equalsIgnoreCase(GroupChat.TERMINATE))
  52.                     {
  53.                         finished = true;
  54.                         socket.leaveGroup(group);
  55.                         socket.close();
  56.                         break;
  57.                     }
  58.                     message = name + ": " + message;
  59.                     byte[] buffer = message.getBytes();
  60.                     DatagramPacket datagram = new
  61.                     DatagramPacket(buffer,buffer.length,group,port);
  62.                     socket.send(datagram);
  63.                 }
  64.             }
  65.             catch(SocketException se)
  66.             {
  67.                 System.out.println("Error creating socket");
  68.                 se.printStackTrace();
  69.             }
  70.             catch(IOException ie)
  71.             {
  72.                 System.out.println("Error reading/writing from/to socket");
  73.                 ie.printStackTrace();
  74.             }
  75.         }
  76.     }
  77. }
  78. class ReadThread implements Runnable
  79. {
  80.     private MulticastSocket socket;
  81.     private InetAddress group;
  82.     private int port;
  83.     private static final int MAX_LEN = 1000;
  84.     ReadThread(MulticastSocket socket,InetAddress group,int port)
  85.     {
  86.         this.socket = socket;
  87.         this.group = group;
  88.         this.port = port;
  89.     }
  90.      
  91.     @Override
  92.     public void run()
  93.     {
  94.         while(!GroupChat.finished)
  95.         {
  96.                 byte[] buffer = new byte[ReadThread.MAX_LEN];
  97.                 DatagramPacket datagram = new
  98.                 DatagramPacket(buffer,buffer.length,group,port);
  99.                 String message;
  100.             try
  101.             {
  102.                 socket.receive(datagram);
  103.                 message = new
  104.                 String(buffer,0,datagram.getLength(),"UTF-8");
  105.                 if(!message.startsWith(GroupChat.name))
  106.                     System.out.println(message);
  107.             }
  108.             catch(IOException e)
  109.             {
  110.                 System.out.println("Socket closed!");
  111.             }
  112.         }
  113.     }
  114. }
  115. Save the file as GroupChat.java and compile it using javac and then run the program using two command line arguments as specified. A multicast host is specified by a class D IP address and by a standard UDP port number. Class D IP addresses are in the range 224.0.0.0 to 239.255.255.255, inclusive. The address 224.0.0.0 is reserved and should not be used.
  116. For more information visit this site:
  117. https://www.youtube.com/channel/UCJ0nVqHF9lBebRXugUOin4A
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement