01 - Simple Chat System
01 - Simple Chat System
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
eslahi.meisam@apu.edu.my Computer System Security (CSS)
1. Introduction:
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.
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)
http://www.oracle.com/technetwork/java/javase/downloads/jdk-7-netbeans-
download-432126.html
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)
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)
7
eslahi.meisam@apu.edu.my Computer System Security (CSS)
8
eslahi.meisam@apu.edu.my Computer System Security (CSS)
9
eslahi.meisam@apu.edu.my Computer System Security (CSS)
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.
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)
mylink.close ();
12
eslahi.meisam@apu.edu.my Computer System Security (CSS)
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).
13
eslahi.meisam@apu.edu.my Computer System Security (CSS)
14
eslahi.meisam@apu.edu.my Computer System Security (CSS)
This is exactly the same as for the server process (using method close of class
Socket).
mySocket.close();
15