Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Java Programming

A Micro-Project Report On
A Group Chat Application In Java
Submitted To
MSBTE
In Partial Fulfilment of Requirement of Diploma in computer Engineering
Under I Scheme
Submitted By
Mr. Siddhesh Talwadekar Roll No 32
Mr. Sarth Mathkar Roll No 31
Mr. Pratik Golatkar Roll No 42

Under the Guidance Of

Mr. S. Zende

FOR ACADEMIC YEAR 2020-2021

YASHWANTRAO BHONSALE POLYTECHNIC, SAWANTWADI.


Java Programming

MAHARASHTRA STATE BOARD OF TECHNICAL


EDUCATION

CERTIFICATE
This is to certify that,
Mr. Siddheh Talawadekar
Mr. Sarth Mathkar
Mr. Pratik Golathkar

Of Third semester of diploma in Computer Engineering of Institute


Yashwantrao Bhonsale Polytechnic (1742) has completed the Micro Project
satisfactorily in subject Java Programming (22412) for the academic year
2020 – 2021 as prescribed in the curriculum.

Subject Faculty HOD Principal

Seal of institution
Java Programming

Abstract
Now a days online transaction is necessary of every citizen for security of transaction One
Time Password (OTP) is essential. Also for log in purpose of many Social media sites, Apps,
And Wallets OTP is needed to make assured log in by genuine user of Account, So more ease
is provided to the customer. OTP is always received on register mobile number or e-mail of
customer with Bank or related Service provider or social media company. Somewhere public
wifis are also protected by OTP based log in process. So we make the Java program for OTP
generator using Java programming language.

What Is Java ?
Java is a high-level, class-based, object-oriented programming language that is designed to
have as few implementation dependencies as possible. It is a general-purpose programming
language intended to let application developers write once, run anywhere (WORA), meaning
that compiled Java code can run on all platforms that support Java without the need for
recompilation. Java applications are typically compiled to bytecode that can run on any Java
virtual machine (JVM) regardless of the underlying computer architecture. The syntax of Java
is similar to C and C++, but has fewer low-level facilities than either of them. The Java runtime
provides dynamic capabilities (such as reflection and runtime code modification) that are
typically not available in traditional compiled languages. As of 2019, Java was one of the most
popular programming languages in use according to GitHub, particularly for clientserver web
applications, with a reported 9 million developers. Java was originally developed by James
Gosling at Sun Microsystems (which has since been acquired by Oracle) and released in 1995
as a core component of Sun Microsystems' Java platform. The original and reference
implementation Java compilers, virtual machines, and class libraries were originally released
by Sun under proprietary licenses. As of May 2007, in compliance with the specifications of
the Java Community Process, Sun had relicensed most of its Java technologies under the GNU
General Public License. Oracle offers its own Hotspot Java Virtual Machine; however the
official reference implementation is the OpenJDK JVM which is free open source software and
used by most developers and is the default JVM for almost all Linux distributions. As of March
2021, the latest version is Java 16, with Java 11, a currently supported long-term support (LTS)
version, released on September 25, 2018. Oracle released the last zero-cost public update for
the legacy version Java 8 LTS in January 2019 for commercial use, although it will otherwise
still support Java 8 with public updates for personal use indefinitely. Other vendors have begun
to offer zero-cost builds of OpenJDK 8 and 11 that are still receiving security and other
upgrades.
Java Programming

Importance of Java
Java is designed to enable development of portable, high-performance applications for the
widest range of computing platforms possible, hence enabling the fundamental tenets of
overarching accessibility as well as cross-platform interaction. Write software on one platform
and run it on virtually any other platform.

Applications of Java
✓ Desktop GUI Applications. Desktop applications can be easily developed using Java.
✓ Mobile Applications.
✓ Enterprise Applications.
✓ Scientific Applications.
✓ Web-based Applications.
✓ Embedded Systems.
✓ Big Data Technologies.

Advantages of Java
• Java is easy to learn. Java was designed to be easy to use and is therefore easy to write,
compile, debug, and learn than other programming languages.
• Java is object-oriented. This allows you to create modular programs and reusable code.
• java is platform-independent.

A Group Chat Application Java


A group chat application using Multicast Socket (Java Platform SE 7) class is discussed. A
Multicast Socket is a (UDP) Datagram Socket, with additional capabilities for joining
“groups” of other multicast hosts on the internet.

Program For a Group Chat Application In Java


import java.net.*;

import java.io.*;

import java.util.*;
Java Programming

public class GroupChat

private static final String TERMINATE = "Exit";

static String name;

static volatile boolean finished = false;

public static void main(String[] args)

if (args.length != 2)

System.out.println("Two arguments required: <multicast-host> <port-number>");

else

try

InetAddress group = InetAddress.getByName(args[0]);

int port = Integer.parseInt(args[1]);

Scanner sc = new Scanner(System.in);

System.out.print("Enter your name: ");

name = sc.nextLine();

MulticastSocket socket = new MulticastSocket(port);

// Since we are deploying

socket.setTimeToLive(0);

//this on localhost only (For a subnet set it as 1)

socket.joinGroup(group);
Java Programming

Thread t = new Thread(new

ReadThread(socket,group,port));

// Spawn a thread for reading messages

t.start();

// sent to the current group

System.out.println("Start typing messages...\n");

while(true)

String message;

message = sc.nextLine();

if(message.equalsIgnoreCase(GroupChat.TERMINATE))

finished = true;

socket.leaveGroup(group);

socket.close();

break;

message = name + ": " + message;

byte[] buffer = message.getBytes();

DatagramPacket datagram = new

DatagramPacket(buffer,buffer.length,group,port);

socket.send(datagram);

}
Java Programming

catch(SocketException se)

System.out.println("Error creating socket");

se.printStackTrace();

catch(IOException ie)

System.out.println("Error reading/writing from/to socket");

ie.printStackTrace();

class ReadThread implements Runnable

private MulticastSocket socket;

private InetAddress group;

private int port;

private static final int MAX_LEN = 1000;

ReadThread(MulticastSocket socket,InetAddress group,int port)

this.socket = socket;

this.group = group;

this.port = port;
Java Programming

@Override

public void run()

while(!GroupChat.finished)

byte[] buffer = new byte[ReadThread.MAX_LEN];

DatagramPacket datagram = new

DatagramPacket(buffer,buffer.length,group,port);

String message;

try

socket.receive(datagram);

message = new

String(buffer,0,datagram.getLength(),"UTF-8");

if(!message.startsWith(GroupChat.name))

System.out.println(message);

catch(IOException e)

System.out.println("Socket closed!");

}
Java Programming

Implementation
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.
We have used the multicast host IP address as 239.0.0.0 and the port number as 1234 (since
the port numbers 0 through 1023 are reserved).
There are 3 members in the group: Ironman, Captain America, and Groot. Start all three
terminals first before sending the message, otherwise messages which are sent before starting
the terminal are lost (since there is no facility of buffer incorporated to store the messages.)
We need two threads in this application. One for accepting the user input (using the java.util
Scanner class) and the other for reading the messages sent from other clients. Hence I have
separated the thread which does the reading work into ReadThreadclass. For leaving the
group, any of the user can type in Exit to terminate the session

Output Of Above Program


Java Programming

Conclusion
Here by are the presented our microproject on topic Group Chat Application In Java which
inspires us all and gave us a better experience. We all distributed the work in various part and
gave them to our group members. We tried to do our best for our microproject of Java
Programming. This project is completed successfully as we all learned importance of unity
in life with teamwork, it solves the problems in seconds. At the time of completion, we have
learnt many lessons such as teamwork, tolerance, co-operation, patience and also learnt the
important of micro project because we not only gained not only theoretical knowledge but
practical knowledge also.

We are glad to present it !!

You might also like