TCP Vs UDP Overview
TCP Vs UDP Overview
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
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
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
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
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?