2. Connection-oriented vs. Connectionless 1
TCP UDP
TCP is a connection-orientedprotocol.
Connection-orientationmeans that the
communicating devices should establisha
connectionbefore transmitting data and
should close the connectionafter
transmitting the data.
UDP is the Datagramorientedprotocol.
This is because there is no overhead for
opening a connection, maintaining a
connection, And terminating a connection.
UDP is efficientFor broadcast and multicast
type of networktransmission.
3. Reliability 2
TCP UDP
TCP provides the delivery guarantee,
whichmeans a message sent using TCP
protocol is guaranteedto be delivered to
the client. If a message is lost in transits
thenits recovered using resending, which
is handled by TCP protocol itself
UDP is unreliable, it doesn't provide any
deliveryguarantee. A datagrampackage
may be lost in transits. That's why UDP is
not suitable for programs which require
guaranteed delivery.
4. Error Checking 3
TCP UDP
TCP provides extensive error checking
mechanisms. It is becauseit provides flow
control and acknowledgment of data.
UDP has only the basic error checking
mechanismusing checksums.
5. Ordering 4
TCP UDP
TCP guarantees the order of message. The
message will be delivered to the client in
the same order, the server has sent, though
it's possible they may reach out of order to
the other end of the network. TCP protocol
will do all sequencing and ordering for
you
UDP doesn't provide any ordering or
sequencing guarantee.
6. Speed 5
TCP UDP
TCP is slow.
TCP does has to createa connection,
ensure guaranteedand ordereddelivery, it
does a lot more than UDP.
UPDis fast.
UDP is more suitable where speed is a
concern, for example, online video
streaming, telecast or online multiplayer
games
8. Heavyweight vs. Lightweight 7
TCP UDP
TCP is heavyweight.
Because of the overheadmentionedabove
UPDis lightweight.
UDP is deliver a message without bearing
any overhead of creating connection and
guaranteeing deliveryor order guarantee
keeps it light weight.
9. Header Size 8
TCP UDP
TCP has bigger header thanUDP.
TCP header size is 20 bytes.
UPDhas smallest header than TCP.
UDP Header size is 8 bytes.
10. Congestion or Flow Control 9
TCP UDP
TCP does Flow Control. It requires three
packets to set up a socket connection
before any userdata can be sent.
TCP handles reliabilityand congestion
control.
UDP does not havean option for flow
control.
11. Using 10
TCP UDP
TCP is usedby HTTP, HTTPs, FTP, SMTP
and Telnet
UDP is used by DNS, DHCP, TFTP, SNMP,
RIP, and VoIP.
13. Server Side Programming… 12
Establisha Socket Connection
- To write a server application two sockets are needed.
- A ServerSocket which waits for the client requests (when a client makes a new Socket())
- Socket socket to use for communication withthe client.
ServerSocket serverSocket = new ServerSocket(PORT);
1st argument: TCPport
14. Server Side Programming 13
Communication
- getOutputStream() method is usedto send the output through the socket.
- getInputStream() method is used to receive the input through the socket.
Close the Connection
- After finishing, it is important to close theconnectionby closing thesocket as well as input/output
streams.
15. Client Side Programming 14
Establisha Socket Connection :
- To connect to other machine we needa socket connection.
- A socket connectionmeans the two machines have informationabout eachother’s networklocation
- (IP Address) and TCP port
- The java.net.Socket classrepresents a Socket
Socket socket = new Socket(“127.0.0.1”, 5000)
- 1st argument : IP address of Server (127.0.0.1 is the IP address of local host)
- 2nd argument : TCP Port (Just a number representing which application to run on a
server, e.g. : HTTP runs on port 80, Port number can be from0 to 65535)
16. Client Side Programming… 15
- Communication :
To communicateover a socket connection, streams are used to bothinputand output the data.
- Closing the connection:
The socket connection is closed explicitly oncethe message to server is sent
22. 20Server Side…
public static void main(String[] args){
Server server = new Server();
server.startConnection();
server.Connecting();
server.stopConnection();
}