Network Application
Network Application
Network Application
Network Application
Network application exchange data between physically separated machines. For this to
occur the machines must be connected by a transmission media. There are many different types
of communication links and new ones continue to be developed. Coaxial cables, phone lines,
digital phone lines, fiber optic cable, satellite beam, and infrared waves are all used as
exchanged between them. A local are network on LAN is a network of computers in close
physical proximity, usually a single building, but can be a group of adjacent buildings. Over the
last decades LANs have become an important component of the computer workplace.
Protocol Stacks
Very Early in the history of computer network development the concept of separating the
problem into multiple levels was adapted. With a multilevel architecture each layer can handle a
different aspect of networking and provide that functionality to the above layer. TCP/IP is a
specific implementation of a multi level network architecture. In both, the first and second
chapter, we are always repeating the same sentence, which is TCP/IP protocol. It is now the time
TCP
TCP (the Transmission Control Protocol ) has the responsibility for breaking up the message
into datagrams, reassembling them at the other end, resending anything that gets lost, and putting
things back in the right order. It may seem that TCP is doing all the work. And in small network it
is true. With TCP, there is no maximum message length. When a message is passed to the TCP
protocol, if it is too large to be sent in one peace, the message is broken up into chunks or packets
and sent one at a time to the destination address. The TCP packet contains the addressing
information. The TCP message also contains a packet number and total number of packets.
Because of the nature of the TCP/IP protocol, the packet may travel different paths and may arrive
in a different order than sent. TCP reassemble the packets in the proper order and requests the
retransmission of any missing or corrupted packets. TCP enables you to create and maintain a
connection to a remote computer. By using the connection, both computers can stream data
IP
give remote computers the capability to recognize other remote computers; thus the IP addressing
method was born. Therefore, simply an IP address uniquely identifies any computer connected to
a network. This address is made up of 32 bits divided into 4 four bytes. But since the number of
connected computers is too large and since it is difficult to remember all their IP addresses, the
Domain Name Service (DNS) was designed. It has the job of transforming the unique computer
names (host name) into an IP address. Therefor, whenever in our project we run the client
application and enter the host name, this means that we are writing the IP address of the remote
Service Port
Till now, we have seen that TCP/IP forms the backbone for communication between
computers, but do you know how these computers speak to each other?
The answer is Ports. A port is a special location in the computers memory that exists when two
computers are communicating via TCP/IP. Application uses a port number to communicate and
the sending and receiving computers use this same port to exchange data. To make the job of
communication easier, some port numbers have been standardized, ex, (www Port 80, Ftp Port
Sockets
communicating with back end servers of various types. Hardware and software technology
advances are pushing PCs into the role of every where communications devices. For software
technology, they require an Application Programming Interface (API) which provides a simple
and uniform access to this technology. WinSock has been this interface for TCP/IP on windows
systems for the last 3 years. It is now set to become the definitive applications interface for all
windows-based communication-capable applications.
Client-Server Application
When the program wishes to use TCP to exchange data, one of the programs should take
the role of a client while the other must take the role of a server. The client application initiates
what is called active open. It creates a socket and actively attempts to connect to server program.
On the other hand, the server application creates a socket and passively listens for incoming
connections from client, performing what is called passive open. When the client wants to
connect a server, it sends a connection request. The server is notified that some process is trying
to connect with it. By accepting the connection, the server completes what is called a virtual
complete a connection.
1. Create a socket
2. Specify the address and service port of the server program
3. Establish the connection with the server
4. Send and receive information
The java swing package lets you make GUI components for your java applications and is
platform independent.
The Swing library is built on top of the Java Abstract Widget Toolkit (AWT), an older, platform
dependent GUI toolkit.
You can use the Java GUI components like button , textbox etc from the library and do not have
to create the components from scratch.
The class JFrame is an extended version of java.awt.Frame that adds support for the JFC/Swing
component architecture.
Container classes are classes that can have other components on it. So for creating a GUI, we
need at least one Container object Three types of containers
1. Panel : It is a pure container and is not a window in itself. The sole purpose of a Panel is
to organize the components on to a window.
2. Frame : It is a fully functioning window with its own title and icons.
3. Dialog : It can be thought of as a pop-up window that pops out when message has to be
displayed. It is not a fully functioning window like the Frame.
Diagrammatic Representation
LANGUAGE CODED
JAVA
JAVA SWING
IMPLEMENTATION & SOFTWARE USED:
NETBEANS IDE
SOURCE CODE:
Database.java
package com.socket;
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
/**
*@Chetali
* @author Akash
*/
this.filePath = filePath;
try{
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
doc.getDocumentElement().normalize();
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
if(getTagValue("username", eElement).equals(username)){
return true;
return false;
catch(Exception ex){
return false;
try{
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
doc.getDocumentElement().normalize();
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
return true;
System.out.println("Hippie");
return false;
catch(Exception ex){
return false;
}
public void addUser(String username, String password){
try {
DocumentBuilderFactory docFactory =
DocumentBuilderFactory.newInstance();
newuser.appendChild(newusername);
newuser.appendChild(newpassword); data.appendChild(newuser);
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
transformer.transform(source, result);
catch(Exception ex){
System.out.println("Exceptionmodify xml");
NodeList nlList =
eElement.getElementsByTagName(sTag).item(0).getChildNodes();
return nValue.getNodeValue();
Message.java
package com.socket;
import java.io.Serializable;
/**
*@Chetali
* @author Akash
*/
@Override
SocketServer.java
package com.socket;
import java.io.*;
import java.net.*;
/**
*@Chetali
* @author Akash
*/
super();
server = _server;
socket = _socket;
ID = socket.getPort();
ui = _server.ui;
try {
streamOut.writeObject(msg);
streamOut.flush();
return ID;
@SuppressWarnings("deprecation")
while (true){
try{
server.handle(ID, msg);
catch(Exception ioe){
server.remove(ID);
stop();
streamOut.flush();
ui = frame;
db = new Database(ui.filePath);
try{
port = server.getLocalPort();
start();
}
catch(IOException ioe){
ui.RetryStart(0);
ui = frame;
port = Port;
db = new Database(ui.filePath);
try{
port = server.getLocalPort();
start();
catch(IOException ioe){
}
}
try{
addThread(server.accept());
catch(Exception ioe){
ui.RetryStart(0);
if (thread == null){
thread.start();
@SuppressWarnings("deprecation")
public void stop(){
if (thread != null){
thread.stop();
thread = null;
if (clients[i].getID() == ID){
return i;
return -1;
if (msg.content.equals(".bye")){
remove(ID);
else{
if(msg.type.equals("login")){
if(findUserThread(msg.sender) == null){
if(db.checkLogin(msg.sender, msg.content)){
clients[findClient(ID)].username = msg.sender;
SendUserList(msg.sender);
else{
else{
else if(msg.type.equals("message")){
if(msg.recipient.equals("All")){
else{
findUserThread(msg.recipient).send(new Message(msg.type,
msg.sender, msg.content, msg.recipient));
else if(msg.type.equals("test")){
else if(msg.type.equals("signup")){
if(findUserThread(msg.sender) == null){
if(!db.userExists(msg.sender)){
db.addUser(msg.sender, msg.content);
clients[findClient(ID)].username = msg.sender;
clients[findClient(ID)].send(new Message("signup",
"SERVER", "TRUE", msg.sender));
else{
clients[findClient(ID)].send(new Message("signup",
"SERVER", "FALSE", msg.sender));
else{
else if(msg.type.equals("upload_req")){
if(msg.recipient.equals("All")){
else{
findUserThread(msg.recipient).send(new Message("upload_req",
msg.sender, msg.content, msg.recipient));
}
else if(msg.type.equals("upload_res")){
if(!msg.content.equals("NO")){
String IP =
findUserThread(msg.sender).socket.getInetAddress().getHostAddress();
findUserThread(msg.recipient).send(new Message("upload_res",
IP, msg.content, msg.recipient));
else{
findUserThread(msg.recipient).send(new Message("upload_res",
msg.sender, msg.content, msg.recipient));
} }
clients[i].send(msg);
if(clients[i].username.equals(usr)){
return clients[i];
return null;
@SuppressWarnings("deprecation")
clients[i-1] = clients[i];
}
clientCount--;
try{
toTerminate.close();
catch(IOException ioe){
toTerminate.stop();
try{
clients[clientCount].open();
clients[clientCount].start();
clientCount++;
}
catch(IOException ioe){
else{
ServerFrame.java
package com.socket;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
/**
*@Chetali
* @author Akash
*/
public ServerFrame() {
initComponents();
jTextField3.setEditable(false);
jTextField3.setBackground(Color.WHITE);
jTextArea1.setEditable(false); }
return System.getProperty("os.name").startsWith("Windows"); }
@SuppressWarnings("unchecked")
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Server Box");
Startserverr.setText("Start Server");
Startserverr.setEnabled(false);
Startserverr.addActionListener(new java.awt.event.ActionListener() {
StartserverrActionPerformed(evt); }
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
Browsee.setText("Browse...");
Browsee.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BrowseeActionPerformed(evt);
});
jLabel1.setText(" A.P.");
getContentPane().setLayout(layout);
layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.Gro
upLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
LEADING)
.addComponent(jScrollPane1)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField3,
javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Browsee,
javax.swing.GroupLayout.PREFERRED_SIZE, 91,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Startserverr)))
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
layout.setVerticalGroup( layout.createParallelGroup(javax.swing.Group
Layout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
BASELINE)
.addComponent(jTextField3,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(Browsee)
.addComponent(Startserverr))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1))
);
pack();
}// </editor-fold>
Startserverr.setEnabled(false); Browsee.setEnabled(false);
fileChooser.showDialog(this, "Select");
if(file != null){
filePath = file.getPath();
Startserverr.setEnabled(true);
try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelCl
assName());
catch(Exception ex){
java.awt.EventQueue.invokeLater(new Runnable() {
new ServerFrame().setVisible(true);
});
Message.java
package com.socket;
import java.io.Serializable;
/**
*@Chetali
* @author Akash
*/
@Override
SocketClient.java
package com.socket;
import com.ui.ChatFrame;
import java.io.*;
import java.net.*;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*@Chetali
* @author Akash
*/
Out.flush();
In = new ObjectInputStream(socket.getInputStream());
@Override
while(keepRunning){
try {
System.out.println("Incoming : "+msg.toString());
if(msg.type.equals("message")){
if(msg.recipient.equals(ui.username)){
else{
}
if(!msg.content.equals(".bye") && !
msg.sender.equals(ui.username)){
else if(msg.type.equals("login")){
if(msg.content.equals("TRUE")){
ui.loginn.setEnabled(false); ui.signupp.setEnabled(false);
ui.mesgsend.setEnabled(true);
ui.Akashh.setEnabled(false); ui.passwordd.setEnabled(false);
}
else{
else if(msg.type.equals("test")){
ui.connectt.setEnabled(false);
ui.loginn.setEnabled(true); ui.signupp.setEnabled(true);
ui.Akashh.setEnabled(true); ui.passwordd.setEnabled(true);
ui.localhostt.setEditable(false); ui.localhost2.setEditable(false);
}
else if(msg.type.equals("newuser")){
if(!msg.content.equals(ui.username)){
if(ui.model.getElementAt(i).equals(msg.content)){
if(!exists){ ui.model.addElement(msg.content); }
else if(msg.type.equals("signup")){
if(msg.content.equals("TRUE")){
ui.loginn.setEnabled(false); ui.signupp.setEnabled(false);
else{
}
}
else if(msg.type.equals("signout")){
if(msg.content.equals(ui.username)){
ui.connectt.setEnabled(true); ui.mesgsend.setEnabled(false);
ui.localhostt.setEditable(true); ui.localhost2.setEditable(true);
ui.model.removeElementAt(i);
ui.clientThread.stop();
else{
ui.model.removeElement(msg.content);
else if(msg.type.equals("upload_req"))
{
if(JOptionPane.showConfirmDialog(ui, ("Accept
'"+msg.content+"' from "+msg.sender+" ?")) == 0)
jf.setSelectedFile(new File(msg.content));
else{
else if(msg.type.equals("upload_res")){
if(!msg.content.equals("NO")){
else{
}
}
else{
catch(Exception ex) {
keepRunning = false;
ui.connectt.setEnabled(true);
ui.localhostt.setEditable(true);
ui.localhost2.setEditable(true);
ui.model.removeElementAt(i);
ui.clientThread.stop();
ex.printStackTrace();
}
}
try {
Out.writeObject(msg);
Out.flush();
System.out.println("Outgoing : "+msg.toString());
t = null;
}
//com.socket
ChatFrame.java
package com.ui;
import com.socket.Message;
import com.socket.SocketClient;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.UIManager;
import oracle.jrockit.jfr.JFR;
/**
*@Chetali
* @author Akash
*/
public ChatFrame() {
initComponents();
this.setTitle("Chat Box");
model.addElement("All");
jList1.setSelectedIndex(0);
this.addWindowListener(new WindowListener() {
@Override public void windowOpened(WindowEvent e) {}
});
return System.getProperty("os.name").startsWith("Windows");
@SuppressWarnings("unchecked")
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
localhostt.setText("localhost");
localhost2.addActionListener(new java.awt.event.ActionListener() {
localhost2ActionPerformed(evt);
});
connectt.setText("Connect");
connectt.addActionListener(new java.awt.event.ActionListener() {
connecttActionPerformed(evt);
});
Akashh.setText("Akash");
Akashh.setEnabled(false);
Akashh.addActionListener(new java.awt.event.ActionListener() {
AkashhActionPerformed(evt);
});
jLabel3.setText("Password :");
jLabel4.setText("Username :");
signupp.setText("SignUp");
signupp.setEnabled(false);
signupp.addActionListener(new java.awt.event.ActionListener() {
signuppActionPerformed(evt);
});
passwordd.setText("chetali");
passwordd.setEnabled(false);
passwordd.addActionListener(new java.awt.event.ActionListener() {
passworddActionPerformed(evt);
});
mesgarea.setColumns(20);
mesgarea.setRows(5);
jScrollPane1.setViewportView(mesgarea);
jLabel5.setText("Message : ");
mesgsend.setEnabled(false);
mesgsend.addActionListener(new java.awt.event.ActionListener() {
mesgsendActionPerformed(evt);
});
loginn.setText("Login");
loginn.setEnabled(false);
loginn.addActionListener(new java.awt.event.ActionListener() {
loginnActionPerformed(evt);
});
jLabel6.setText(" A.P.");
getContentPane().setLayout(layout);
layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.Gr
oupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
LEADING)
.addComponent(jSeparator1,
javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
TRAILING)
.addComponent(jLabel1)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
LEADING)
.addComponent(Akashh)
.addComponent(localhostt,
javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))
.
addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.T
RAILING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELAT
ED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
LEADING)
.addComponent(localhost2)
.addComponent(passwordd,
javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
LEADING, false)
.addComponent(connectt,
javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(loginn, javax.swing.GroupLayout.PREFERRED_SIZE,
70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(signupp,
javax.swing.GroupLayout.PREFERRED_SIZE, 81,
javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jScrollPane1)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(mesgtext)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELAT
ED)
.addComponent(mesgsend)
.addGap(8, 8, 8)))
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel6)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
BASELINE)
.addComponent(jLabel1)
.addComponent(localhostt,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(localhost2,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(connectt))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELAT
ED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
BASELINE)
.addComponent(Akashh,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(signupp)
.addComponent(passwordd,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(loginn))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELAT
ED)
.addComponent(jSeparator1,
javax.swing.GroupLayout.PREFERRED_SIZE, 10,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
LEADING)
.addComponent(jScrollPane1)
.addComponent(jScrollPane2,
javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE))
.addComponent(mesgtext,
javax.swing.GroupLayout.PREFERRED_SIZE, 53,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5)
.addComponent(mesgsend,
javax.swing.GroupLayout.PREFERRED_SIZE, 39,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel6)
.addContainerGap())
);
pack();
}// </editor-fold>
try{
clientThread.start();
catch(Exception ex){
} } }
username = Akashh.getText();
password = passwordd.getText();
mesgtext.setText("");
} }
username = Akashh.getText();
password = passwordd.getText();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
catch(Exception ex){
java.awt.EventQueue.invokeLater(new Runnable() {
new ChatFrame().setVisible(true);
});
}
SCREENSHOTS:
ChatFrame.java (jFrame)
ServerFrame.java (jFrame)
All Chat Boxes at start up
There is always a room for improvements in any software package, however good and
efficient it may be done. But the most important thing should be flexible to accept
further modification. Right now we are just dealing with text communication. In
future this software may be extended to include features such as:
File transfer: this will enable the user to send files of different formats to
others via the chat application.
Voice chat: this will enhance the application to a higher level where
communication will be possible via voice calling as in telephone.
Video chat: this will further enhance the feature of calling into video communication.