01 - Simple Chat System

You might also like

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

eslahi.meisam@apu.edu.

my Computer System Security (CSS)

LAB 01- Socket Programming In Java

Meisam Eslahi
eslahi.meisam@apu.edu.my

Disclaimer:

• This document is produced for the internal student and may be printed for
internal references.
• All the notes and image may be used for the internal education only.
• No part of this document may be reproduced or transmitted in any form or
by any means, electronic or mechanical, including photocopying, recording,
or by any information storage and retrieval system, without written
permission from the original publisher.

Objective:

The objective of this lab is to implement a simple chat system using client
server approach in java programming language.

Tools:
This lab requires:

1. Java Development Kit (JDK)


2. NetBeans IDE.

1
eslahi.meisam@apu.edu.my Computer System Security (CSS)

1. Introduction:

NetBeans IDE provides first-class comprehensive support for the newest


Java technologies and latest Java specification enhancements before other IDEs. It
is the first free IDE providing support for JDK 8, JDK 7, Java EE 7 including its
related HTML5 enhancements, and JavaFX 2.

www.netbeans.org

The NetBeans IDE is written in Java and can run on Windows, OS X, Linux,
Solaris and other platforms supporting a compatible JVM.

2. NetBeans and JDK Installation:

There are several NetBeans IDE available for download as shown in figure
below:

https://netbeans.org/downloads/index.html

2
eslahi.meisam@apu.edu.my Computer System Security (CSS)

However, it’s recommended to download distribution of the JDK includes


the Java SE bundle of NetBeans IDE from oracle official website.

1. Download the JDK and NetBeans from Oracle website:

http://www.oracle.com/technetwork/java/javase/downloads/jdk-7-netbeans-
download-432126.html

2. Click on installation file and wait for a while:

3. Simply click on Next in the welcome wizard.

3
eslahi.meisam@apu.edu.my Computer System Security (CSS)

4. Accept the License Agreement (No Choice) and Click on the Next.

5. Click the "Next" button to install JDK and NetBeans IDE in the default
folder which is suggested. Kindly note that you can simply change the
default installation folder by clicking on Browse button.

6. Simply click next followed by Install to install the both JDK and NetBeans.
7. Click on finish to complete the installation.

4
eslahi.meisam@apu.edu.my Computer System Security (CSS)

3. Socket Programming in Java

Different processes (programs) can communicate with each other across


networks by means of sockets. Java implements both TCP/IP sockets and datagram
sockets (UDP sockets). Very often, the two communicating processes will have a
client/server relationship. The steps required to create client/server programs via
each of these methods are very similar and are outlined in the following two sub-
sections.

3.1 TCP Sockets

A communication link created via TCP/IP sockets is a connection-orientated


link. This means that the connection between server and client remains open
throughout the duration of the dialogue between the two and is only broken (under
normal circumstances) when one end of the dialogue formally terminates the
exchanges (via an agreed protocol).

Since there are two separate types of process


involved (client and server), we shall implement
them separately.

3.1.1 Server Side:


1. Open the NetBeans and create a new project.
2. Select Java and Java Application and Click on the Next.

5
eslahi.meisam@apu.edu.my Computer System Security (CSS)

3. Input Server as your project name and set the project location. Click
finish to create your project.

6
eslahi.meisam@apu.edu.my Computer System Security (CSS)

Setting up a server process requires five steps:

a) Create a ServerSocket object:

The ServerSocket constructor requires a port number (1024-65535,


for non-reserved ones) as an argument.

ServerSocket mySocket = new ServerSocket (1234);

In the code above we defined a ServerSocket


named mySocket which is listening on port
1234. Don’t forget to Import
java.net.ServerSocket package in order to
create socket.

7
eslahi.meisam@apu.edu.my Computer System Security (CSS)

b) Put the server into a waiting state:

The server waits indefinitely for a client to connect. It does this by


calling method accept of class ServerSocket, which returns a Socket object
when a connection is made.

Socket mylink = mySocket.accept ();

We should import java.net.Socket package in


order to use Socket in the code above.

8
eslahi.meisam@apu.edu.my Computer System Security (CSS)

c) Set up input and output streams

Methods getInputStream and getOutputStream of class Socket are


used to get references to streams associated with the socket defined in
previous step. These streams will be used for communication with the client
that has just made connection.

DataInputStream input = new DataInputStream (mylink.getInputStream ());


DataOutputStream output = new DataOutputStream (mylink.getOutputStream ());

We should import java.io.DataInputStream and


java.io.DataOutputStream packages in order to
define input and output streams.

9
eslahi.meisam@apu.edu.my Computer System Security (CSS)

d) Send and receive data


1- Define two string parameters as follows:
• sndTXT: To handle the message that will be send to the client.
• rcvTXT: To receive the message from client.

2- Define a scanner to record the text data from keyboard.

Scanner scn = new Scanner (System.in);

We should import java.util.Scanner package in


order to use scanner in the code above.

Having set up our Scanner and stream objects, sending and receiving
data is very straightforward. We can simply use readUTF() to received data
and writeUTF() to send them.

rcvTXT = input.readUTF ();


System.out.println(rcvTXT);

sndTXT=scn.nextLine();
output.writeUTF (sndTXT);

3- In this example we assume that the client start the chat and end the chat by
sending “QIUT”, therefore we need do-while statement to handle it.

10
eslahi.meisam@apu.edu.my Computer System Security (CSS)

e) Close the connection

It’s important to close the connection after completion of the chat.

mylink.close ();

We just completed the server side in 5 simple


steps as follows:

1. Create a ServerSocket object


2. Put the server into a waiting state
3. Set up input and output streams
4. Send and receive data
5. Close the Connection
11
eslahi.meisam@apu.edu.my Computer System Security (CSS)

3.1.2 Client Side:


1. Create another project and name it as Client.

Setting up the corresponding client involves four steps as follows:

a) Establish a connection to the server

We should create a Socket object, supplying its constructor with the


following two arguments:

• The server's IP address (of type InetAddress);


• The appropriate port number for the service.

Socket mySocket=new Socket (InetAddress.getLocalHost (), 1234);

The port number for server and client programs must


be the same, of course!

12
eslahi.meisam@apu.edu.my Computer System Security (CSS)

b) Set up input and output streams

These are set up in exactly the same way as the server streams were set up
(by calling methods getInputStream and getOutputStream of the Socket object
that was created in step 2).

DataInputStream input = new DataInputStream (mySocket. getInputStream());


DataOutputStream output = new DataOutputStream(mySocket. getOutputStream());

13
eslahi.meisam@apu.edu.my Computer System Security (CSS)

c) Send and receive data


1- Define two string parameters as sndTXT and rcvTXT in addition to the
scanner and handle with while as same as what we did for server side.

14
eslahi.meisam@apu.edu.my Computer System Security (CSS)

d) Close the Connection

This is exactly the same as for the server process (using method close of class

Socket).

mySocket.close();

We just completed the client side in 4 simple


steps as follows:

1. Create connection to the server


2. Set up input and output streams
3. Send and receive data
4. Close the Connection

MMMM!!! Okeeeey… The program


works but I think something is wrong!!!

15

You might also like