Java Socket Programming Tutorial

Java provides a wonderful options for networking.This gives a simple java socket programming tutorial . Before starting our discussion with Java networking  concepts , let us look into the fundamentals of networking with TCP/IP model.

Overview to networking in Java

Let us start our tutorial with an introductory session on the famous TCP/IP  model.This model is using now a days  for communicating devices in network.

Block Diagram of TCP/IP Model

TCP/IP model is the enhanced version of OSI (Open Systems Interconnection)model.OSI model is nothing but a standard reference model for data Communication.TCP/IP  model is relevant with all platforms.Now lets start our discussions with  the layers of TCP/IP model.

1)Application Layer

This layer contains the applications .It can be a web browser , or an email client or  any other application.The applications running in application layer is communicating with the transport layer . Our application layer is receiving data from transport layer as well as giving data to transport layer.

2)Transport Layer

 This layer is dealing with end to end communication .Transport layer contains  two protocols:

1)TCP :-Transmission Control Protocol

TCP provides connection oriented , reliable communication between two end points.Java can use TCP for  networking.See this chapter for sample code.

2)UDP:-User Datagram Protocol

UDP provides connectionless , unreliable communication between two end points.Java can use UDP also for communication. See this chapter for sample code

Remember, our application layer contains the applications. An application can decide which protocol in the transport layer needs to be used for communication.A java application can use either of the two protocols for communication.The java.net   and javax.net packages contains the necessary components for for networking .We will see  communication using TCP  as well us UDP in coming chapters.

TCP vs UDP

The following table gives a comparison between TCP and UDP.

SL.No: TCP UDP
1 TCP is connection oriented Connection less
2 Reliable Un-reliable
3 TCP is slower when compared with UDP Faster
4 TCP header size is 20 bytes UDP header size is 8 bytes
5 Flow control is there No flow control

3)Internet layer

This layer contains the Internet Protocol . It does does the routing , packing and addressing of data.

4)Link Layer

This layer connects with external network.This layer sends data to external network.It receives data from external network and giving the data to the internet layer .

See also

Socket communication with UDP

Socket communication with TCP

Chat application in Java using socket programming

Transferring Java object through socket using TCP

Transferring file through socket using TCP

Transferring a directory through socket  using TCP

Transferring Java objects using UDP

Transferring file using UDP