Você está na página 1de 8

1.

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

Algorithm & Flowchart


We use a socket component in this java project, and here is the
main phase of socket

Before a socket been built :


- The server just waits, listening to the socket for a client to
make a connection
reauest.
- To make a connection request, the clients tries to
renderzvous with the server on the servers machine and
port.

CLIENT
SERVER

Syncronize

After a socket been built :


- The client can use the socket to communicate with the
server.
- The client and server can now communicate by writing to
or reading from their
socket.

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

PRINT Wrong Command !!

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;

public class Server


{
String num;
public static void main(String[] args)
{

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

System.out.print("\n\n-\tClient "+user+" Request: "


+nama_file);

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;

public class Client

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("######################################################");

Socket sock = new Socket("localhost", 8765);


//ps getoutputstream, kirim
PrintStream ps = new PrintStream(sock.getOutputStream());
BufferedReader mess1 = new BufferedReader(new
InputStreamReader(sock.getInputStream()));
System.out.print(mess1.readLine());
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br1 = new BufferedReader(isr);
String user = br1.readLine();
ps.println(user);
BufferedReader mess2 = new BufferedReader(new
InputStreamReader(sock.getInputStream()));
System.out.println(mess2.readLine());
String end = "";
while (!end.equals("end"))
{
System.out.print("[from client]"+user + ": ");
System.out.print(" <"+user +">"+ " :\t");

//

InputStreamReader input = new InputStreamReader(System.in);


BufferedReader buffer = new BufferedReader(input);
end = buffer.readLine();
ps.println(end);
BufferedReader pesan = new BufferedReader(new
InputStreamReader(sock.getInputStream()));
String show_file = pesan.readLine();
//System.out.println("[from client] Server: "+emot);
System.out.println(" <Server> :\t"+show_file);

}
}
catch (Exception ex) { }
}
}

7 | Page

3.3

Results

8 | Page

Você também pode gostar