Today, many of the processes running on a computer are required to obtain or send information to other processes that are located on a different computer. To achieve this communication, protocols TCP and UDP are used.
TCP (Transmission Control Protocol) provides a conduit for point to point communication between two computers, ie, when the transmission requires a flow of data between two computers, TCP sets an exclusive conduit between the equipment by which the data will be transmitted and this will last until the transmission is completed, thanks to this TCP guarantees that data sent from one end of the connection and reach the other end in the same order they were sent. The features that have made the protocol TCP is known as a connection-oriented protocol.
Meanwhile there is also orientated protocol called UDP connection. UDP is not connection-oriented because the way to transmit data in the first place does not guarantee arrival at the destination, even if it came to the final destination, does not guarantee the integrity of data. UPD for the transmission of data without establishing a communication conduit exclusively as does TCP, also uses datagrams, which contain an information portion and which are sent to the network waiting to be captured by the target device. When the destination capture datagrams must rebuild information for this should sort the information it receives as information transmitted does not come with a specific order, you must also be aware that not all information will come. The UDP protocol operation can be compared with the postal service.
Sockets
Sockets are a form of communication that are on different machines on a network, the sockets provide a point of communication by which you can send or receive data between processes.
The sockets have a life cycle depending if they are server sockets, waiting for a client to establish a communication or are socket client looking for a server socket for communication.
Deployment of Sockets in Java
The Java programming language includes Java.net library that provides both TCP and UDP sockets:
Classes for TCP sockets
Class |
Description |
Socket |
This class implements client sockets A socket is one end in communication between two machines. |
ServerSocket |
This class implements the server socket. A server socket waits for a request coming from the network performs certain operations based on the request received, and then possibly returns a result to the applicant. |
Classes for UDP sockets
Class |
Description |
DatagramPacket |
This class represents a datagram packet. Datagram packets are used to implement the package delivery service offline. Each message is routed from one machine to another based on the information contained within the packet, only. When multiple packets are sent from one machine to another, they can follow different routes and can arrive in any order. Packet delivery is not guaranteed. |
DatagramSocket |
This class represents a socket for sending and receiving datagram packets. |
Examples of TCP Sockets
Below are two code in java that use TCP sockets, the code is an application that acts as a client and another as a server, which sends a message string and receive a response string.
TCP Server Program
TCP Client Program
The results of both the server and client will look like this:
TCP Server
TCP Client
comments powered by Disqus