Java Network Programming

IP, TCP, UDP and Socket

Protocols specify interactions between the communicating entities. A communication protocol defines rules and conventions for communication between network devices. It is a system of rules in telecommunications that allow two or more entities of a communications system to transmit information via any kind of variation of a physical quantity. It include mechanisms for devices to identify and make connections with each other, as well as formatting rules that specify how data is packaged into messages sent and received . Protocols are often described in an industry or international standard . Computers running on the Internet communicate to each other using either the Transmission Control Protocol (TCP) or the User Datagram Protocol (UDP) . When you write Java programs that communicate over the network, you are programming at the application layer . Typically, you don't need to concern yourself with the TCP and UDP layers. Instead, you can use the classes in the java.net package . These classes provide system-independent network communication.

The Internet Protocol (IP)

IP (Internet Protocol) is the method or protocol by which data is sent from one computer to another on the Internet. It uses a set of rules to send and receive messages at the Internet address level. IP has the task of delivering packets from the source host to the destination host solely based on the IP addresses in the packet headers. For this purpose, IP defines packet structures that encapsulate the data to be delivered. Internet Protocol also defines addressing methods that are used to label the datagram with source and destination information. The IP protocol standardizes the way machines over the Internet or any IP network forward or route their packets based on their IP addresses .

TCP (Transmission Control Protocol)

TCP (Transmission Control Protocol) is one of the core protocols of the Internet Protocol Suite. It is a connection-oriented reliable protocol. It is the protocol that major Internet applications such as the World Wide Web, email, remote administration and file transfer rely on. Transmission Control Protocol (TCP) is stream oriented , that is, it entities exchange streams of data. It uses a number of protocol timers that ensure reliable and synchronised communication between the two End points . It's basically responsible for taking chunks of data — which could be text, images, videos, and so on — splitting them into smaller packets of data, and then sending them to where they can be received by another TCP layer . TCP guarantees the recipient will receive the packets in order by numbering them. The recipient sends messages back to the sender saying it received the messages. If the sender doesn’t get a correct response, it will resend the packets to ensure the recipient received them. TCP packets are lost occasionally, just like IP packets. The difference is that the TCP protocol takes care of requesting retransmits to ensure that all packets reach their destination, and tracks packet sequence numbers to be sure that they are delivered in the correct order.

UDP (User Datagram Protocol)

UDP stands for User datagram protocol . It uses a simple connectionless transmission model with a minimum of protocol mechanism. UDP provides checksums for data integrity, and port numbers for addressing different functions at the source and destination of the datagram. Unlike TCP/IP , UDP does not divide each transmission into packets, which allows for a faster transmission. It uses no acknowledgment, not re-sequence the messages and sets up no flow control. It is possible that the UDP messages that are lost are duplicated, or delivered out of sequence they arrive too early to be processed upon receipt. UDP packets can arrive out of order or not at all. No packet has any knowledge of the preceding or following packet . The recipient does not acknowledge packets, so the sender does not know that the transmission was successful. UDP has no provisions for flow control-packets can be received faster than they can be used. Applications that do not require a high level security transmission, and there are many, and the management software, which require quizzes resources prefer to use UDP.

Socket

A socket is one end-point of a two-way communication link between two programs running on the network. The combination of an IP address and a port is strictly known as an endpoint and is sometimes called a socket . This usage originates with RFC793, the original TCP specification. Normally, a server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request.

A socket consists of three things:

  1. An IP address
  2. A transport protocol
  3. A port number
A server program normally listens to a specific port waiting for connection requests from a client . When a connection request arrives, the client and the server establish a dedicated connection over which they can communicate. During the connection process, the client is assigned a local port number , and binds a socket to it. Sockets are bidirectional , meaning that either side of the connection is capable of both sending and receiving data. Therefore a socket can be created theoretically at any level of the OSI model from 2 upwards.