[go: up one dir, main page]

0% found this document useful (0 votes)
55 views41 pages

TCP Vs UDP Overview

1. Create a Server_NRP project with a Server class to listen for login requests on a port using ServerSocket. 2. Create a Client_NRP project with a login GUI and a Client class to send the username and password to the Server via Socket. 3. The Server validates the credentials by querying a database table and sends a response back to the Client. 4. The Client displays success or failure message based on the response from the Server, completing the authentication flow.

Uploaded by

tikowanaasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views41 pages

TCP Vs UDP Overview

1. Create a Server_NRP project with a Server class to listen for login requests on a port using ServerSocket. 2. Create a Client_NRP project with a login GUI and a Client class to send the username and password to the Server via Socket. 3. The Server validates the credentials by querying a database table and sends a response back to the Client. 4. The Client displays success or failure message based on the response from the Server, completing the authentication flow.

Uploaded by

tikowanaasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

04

DISTRIBUTED PROGRAMMING
UDP vs TCP
What is TCP?
TCP (Transmission Control Protocol) or connection-oriented
protocols is a continuous connection established between Client
and Server.
TCP provides reliable connection through the use of network layer
provided by IP Protocol.
Use 3 ways handshake concept.
3 ways handshake
TCP vs. UDP
TCP vs. UDP
TCP UDP
Transmission Oriented Protocol User Datagram Protocol
Connection Oriented Protocol Connectionless Protocol
Reliable than UDP Less reliable than TCP
Slower data transmission Faster data transmission
Have error checking and reporting Do not have error checking and
mechanism reporting
Guarantee the data sequence of both Do not guarantee the data sequence of
sent and received data both sent and received data

Header size: 20 bytes Header size 8 bytes


TCP vs. UDP

TCP UDP
TCP requires 3 additional packets UDP does not require any
to setup the connection (SYN- additional packet to setup the
SYNACK-SYN) connection

Commonly used in application with Commonly used in application with


characteristic: High Reliability Less characteristic: Time Sensitive less
Time Reliability
Required Class to Create TCP Connection in JAVA
(1)
1. Socket
• Socket class is used to create a socket object in order to establish a connection between
client and server.
 Methods in Socket class:

Method Description
public InputStream returns the InputStream attached with
getInputStream() this socket.
public OutputStream returns the OutputStream attached
getOutputStream() with this socket.
public synchronized void close() closes this socket
Required Class to Create TCP Connection in JAVA
(2)
2. ServerSocket
• ServerSocket class is used to create a server socket object in
order to establish a connection with client
 Methods in ServerSocket class:

Method Description
public Socket accept() returns the socket and establish a
connection between server and
client.
public synchronized void close() closes the server socket.
How to create TCP connection in JAVA?
Server Client

 ServerSocket
 Socket s
 Socket s
 DataOutputStream
 BufferedReader b
 s.getOutputStream
 s.getInputStream()
()
 DataOutputStream
 BufferedReader
 s.getOutputStream
 s.getInputStream()
()
Basic Concept of TCP
Server (fred)
Client (sid)
80 2037
ServerSocket ss.
s = ss.accept() s = new Socket
(“fred”, 80)

1583 2037
Socket s

s.getInputStream() 1583 2037


s.getInputStream()
s.getOuputStream()
s.getOuputStream()
How TCP works: Client sends data to Server
Server Client
Initialize ServerSocket and 1 4 Initialize Socket and define the
Listen port 0 - 65000 server’s IP and the target’s Port

Define Socket and accept client’s Client input String data and initialize
connection using 5 an object of BufferedReader
2
accept() method from (System.in) type
ServerSocket class
Create a variable of Initialize DataOutputStream to
BufferedReader type to store data 6 send Data to Server with
3 getOutputStream() method from
sent from Client using
getInputStream() method from Socket class
Socket class
Use writeBytes() method from
7 DataOutputStream class to send
Data received 8
the String data to Server
How TCP works: Server sends data to Client
Server Client
Initialize String data to be sent to
9
Client

Initialize DataOutputStream with Create a variable of


1
getOutputStream() method from 1 BufferedReader type to store the
0
Socket class 2 data from Server using
getInputStream() method from
Socket class
Use writeBytes() method from
DataOutputStream class to send 11 1
3 Data received
data to Client
TCP Implementation in JAVA

Case study: Simple Chat using TCP


Create a new project with name “ServerTCP”.
 Create a new form with name “FormServer”.
Create a new project with name “ClientTCP”.
 Create a new form with name “FormClient”.
The UI of the 2 forms can be seen in next slide... 
Simple Chat Using TCP
FormServer FormClient
Simple Chat Using TCP
• FormServer

To store the chat’s content, either from Server or Client


Define an object of Socket type

Initialize an object of ServerSocket type and


set to Listen to Port 5000-9000
getChat() Method is called when the Server is “waiting” incoming chat from Client
To receive data Call ServerSocket’s accept() method to receive
from Client any incoming connection from Client

Chat data chat is received from Client


Append the latest chat into TextArea
Simple Chat Using TCP
• FormServer (The implementation in Send button)

Obtain chat from TextField


Object of Append the latest chat into TextArea
DataOutputStream
type
to send chat from
Server to Client Send chat data to Client
Recall the getChat() method to “wait” the reply from Client
Simple Chat Using TCP
• FormServer

Add the required libraries


Simple Chat Using TCP
• FormClient (The implementation in Send button)

To store the chat’s content, either from Server or Client


Obtain chat from TextField
Append the latest chat into TextArea
Object of
Initialize a new socket and set the Server’s
DataOutputSt
IP address and Port
ream type
to send chat Send chat data to Server
from Client to
Server Received chat data from Server

To obtain data from Server


Simple Chat Using TCP
• FormClient

Add the required libraries


Simple Chat Using TCP
• First, execute the FormServer. The FormServer does not show up since the
Server is still “waiting” for any incoming connection from Client
• Then, execute the FormClient. Try to send some message.

FormServer
shows up after
Client sends a
message
Simple Chat Using TCP

SendButton is “frozen” or cannot be used since it must wait for a reply from Client and vice versa.
After client has sent a chat to Server, then the SendButton will be “frozen” because
it waits for a reply from Server.
Try to do the Simple Chat
project Tutorial within the
next 15-30 minutes
Simple Chat Using TCP
• How to solve the “frozen” SendButton on the
Client/Server form such that the Client/Server could send
the chat seamlessly?

Wait until we learn about multithreading!!!


Additional References
• https://www.tutorialspoint.com/java/java_networking.htm

• Implementation using PrintStream:


https://www.youtube.com/watch?reload=9&v=vCDrGJW
qR8w
Self Practice
Case Study: Authentication in a Platform
• Create a Login Authentication feature in JAVA platform
• Create a new console-based Project “Server_NRP” to process the Login
mechanism using TCP/UDP.
• Create a new GUI-based Project “Client_NRP” to let the user enters
his/her username & password through the client platform

• Create a `pengguna` class:


– ID (PK, Autoincrement)  INT
– username (varchar(200))  STRING
– password (varchar(200))  STRING
– no_telp (varchar(20))  STRING
Create an object of `Pengguna` class
• Create 10 data for object of `Pengguna` class and store them in an
array.
• Put the object of `Pengguna` class inside your “Server”, since the
data is only stored in the Server
• Client only request the data
Class
Data Object of `Pengguna` Class
Define the Communication Syntax Between the
Client and Server
• We will use a communication model that is similar like command prompt:
[COMMAND]~[VALUE]
• In this case study, we will adopt the following syntax: [COMMAND]~[VALUE]
• COMMAND = fill with command string which is agreed by both the Client
and Server
• VALUE = fill with data string to be processed
• Delimiter: ~
• Example:
– LOGIN~admin~admin  LOGIN command to process 2 data: “admin admin”
– REGISTER~dosen~dosen  REGISTER command to process 2 data: “dosen
dosen”
Workflow of Server
1. Create a class that represents the Model
2. Fill the Model class with
a) Attributes and the Properties (setter and getter)
b) Constructor (default & parameterized)
c) Boolean checkLogin(String username, String Password)
3. Use MainClass to establish the TCP connectivity
4. Integrate the communication corresponding to the communication
protocol that has been explained in the previous slides
Snip Coding: Model
• Methods in model to support the program
• The core methods: CheckLogin and GetAccess
Snip Coding: Initial TCP Connectivity
• In the code below, the main class directly creates an object of
ServerSocket type when it is launched. This is done in order to let
the Server directly gives TCP service to any Client
Snip Coding: Initial Data Retrieval
Snip Code: Process after data retrieval. Redirect the code
corresponds to the given COMMAND
Snip Code: Action GetAccess
Workflow of Client
1. Create a Form (User Interface Login username password and Login button)
2. Create an ActionPerformed script when the Login button is pressed
3. When user clicked on the Login button:
a) Create a TCP connection to the Server
b) Create the pre-determined Login command. For example: LOGIN~[textuser]~[textpassword]
c) Send the Login command to the Server using TCP connection
d) Receive the response message given by the Server
e) Display the Server’s response message
UI Declare a variable s, msgFromServer and msgToServer in the
form of global variable / data attribute in class Form
SNIP Code: Code Activity in TCP Communication
Disclaimer:
• The previous slides is only a guideline to help your thinking
process
• You can use your creativity to develop the Login form as long as it
supports the system’s authentication process.

You might also like