Escolar Documentos
Profissional Documentos
Cultura Documentos
Objectives
How to program sockets in Java using TCP/IP
2. Requiretments
In this project, Students will learn the important skill of
distributed programming using socket programming. Socket
programming is very cool and the basis of the vast majority of Internet.
Student will write a TCP or UDP client and a TCP or UDP server.
Implement a client that retrieves temperature, humidity and light data
from a remote server. The client should connect to the server, stay
connected, and retrieve a number of readings. The client can request
readings from the server as many times as you want.
The server is responsible for accepting client connections and
sending out the right data according to the commands sent by clients.
Data returned by the server are like:
TEMPERATURE = 60 HUMIDITY = 0 LIGHT = 1.
The background to this programming idea is that a client can
interacts with a sensor and gets real-time sensor readings, such as
temperature, humidity, and light. Here the sensor is the server
software
3. System Design
3.1
CLIENT
SERVER
Syncronize
ALgorithm
Syncronize Acknowledgement
Syncronize
Data Temperature
Data Light
Data Humidity
2 | Page
Flowchart
Start
Serversocket = 127.0.0.1:8765;
ClientSocket = 127.0.0.1:8765;
Temperature = temperature.dat
Humidity = humidity.dat
Light = light.dat
ServerSocket
=
ClientSocket ?
Y
READ
Input_command;
InputCommand
=
temperature ?
Y
PRINT
Temperature
InputCommand
=
humidity ?
Y
PRINT
Humidity
InputCommand
=
Light ?
Y
PRINT
Light
End
3 | Page
3.2
Source Code
SERVER
We must start the server first, because the server will create a new
socket with a new port at 8765. After the server create a new socket,
then the server will wait until the client connected and of course the
server will accept this client connection.
Package server;
import
import
import
import
import
import
import
import
import
import
java.io.BufferedReader;
java.io.File;
java.io.FileInputStream;
java.io.FileNotFoundException;
java.io.FileReader;
java.io.InputStreamReader;
java.io.PrintStream;
java.net.ServerSocket;
java.net.Socket;
java.util.Scanner;
try
{
//initial server socket
ServerSocket serverSocket = new ServerSocket(8765);
//wait connection and accept the client
Socket sock = serverSocket.accept();
//ps getoutputstream
PrintStream ps = new PrintStream(sock.getOutputStream());
ps.println("Type Client Name: ");
BufferedReader br = new BufferedReader
(new InputStreamReader(sock.getInputStream()));
String user = br.readLine();
System.out.println("Client Name : " + user);
ps.println("Hello Client, " + user + "!");
String end = "";
while (!end.equals("end"))
{
BufferedReader pesan = new BufferedReader
(new InputStreamReader(sock.getInputStream()));
String nama_file = pesan.readLine();
4 | Page
switch (nama_file)
{
case "temperature":
try{
BufferedReader filebufferReader = new
BufferedReader(new FileReader("C:\\Users\\
syntax\\Documents\\NetBeansProjects\\chat_ku\\
socket programming\\data\\temperature.dat"));
StringBuffer stringBuffer = new StringBuffer();
String line = null;
while((line = filebufferReader.readLine())!=null){
stringBuffer.append(line).append(" ");
}
ps.println("TEMPERATURE = "+stringBuffer);
System.out.print("\n \tStatus: Successfully sent "
+nama_file +".dat");
}catch(FileNotFoundException e){
ps.println("not found");
}
break;
case "light":
try{
BufferedReader filebufferReader = new
BufferedReader(new FileReader("C:\\Users\\
syntax\\Documents\\NetBeansProjects\\chat_ku\\
socket programming\\data\\light.dat"));
StringBuffer stringBuffer = new StringBuffer();
String line = null;
while((line = filebufferReader.readLine())!=null){
stringBuffer.append(line).append(" ");
}
ps.println("LIGHT = "+stringBuffer);
System.out.print("\n \tStatus: Successfully sent "
+nama_file +".dat");
}catch(FileNotFoundException e){
ps.println("not found");
}
break;
5 | Page
case "humidity":
try{
BufferedReader filebufferReader = new
BufferedReader(new FileReader("C:\\Users
\\syntax\\Documents\\NetBeansProjects\\chat_ku\\
socket programming\\data\\humidity.dat"));
StringBuffer stringBuffer = new StringBuffer();
String line = null;
while((line = filebufferReader.readLine())!=null){
stringBuffer.append(line).append(" ");
}
ps.println("HUMIDITY = "+stringBuffer);
System.out.print("\n \tStatus: Successfully sent "
+nama_file +".dat");
}catch(FileNotFoundException e){
ps.println("not found");
}
break;
default:
ps.println("Wrong Commands !!!");
System.out.println("\n \tStatus: Can't send '"
+ nama_file +"'.");
break;
}
}
}
catch (Exception ex) { }
System.out.println("Oppss... Program Stopped.");
}
CLIENT
The client program must be run after the server program is executed. This
client program will connect to the same port with server used before and
running in the localhost mode because both of the server and client is
running in the one or same computer.
package client;
import
import
import
import
java.io.BufferedReader;
java.io.InputStreamReader;
java.io.PrintStream;
java.net.Socket;
6 | Page
{
public static void main(String[] args)
{
try
{
System.out.println("######################################################");
System.out.println("# Title
: Project1 - Socket Programming
#");
System.out.println("# Group Name
: Syaiful, Rachmat, Ersa, Agung
#");
System.out.println("# Input Commands :
#");
System.out.println("#
- Temperature
#");
System.out.println("#
- Humidity
#");
System.out.println("#
- Light
#");
System.out.println("######################################################");
//
}
}
catch (Exception ex) { }
}
}
7 | Page
3.3
Results
8 | Page