What is socket? what are the differences between TCP socket and UDP socket.
A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent.
An endpoint is a combination of an IP address and a port number. Every TCP connection can be uniquely identified by its two endpoints. That way you can have multiple connections between your host and the server.
The java.net package in the Java platform provides a class, Socket, that implements one side of a two way connection between your Java program and another program on the network. The Socket class sits on top of a platform-dependent implementation, hiding the details of any particular system from your Java program. By using the java.net.Socket class instead of relying on native code, your Java programs can communicate over the network in a platform-independent fashion.
Also View: What is servlet?
Differences between TCP socket and UDP socket
The following are the Differences between TCP socket and UDP socket
Feature | TCP (Transmission Control Protocol) | UDP (User Datagram Protocol) |
---|---|---|
Full Name | Transfer Control Protocol | User Datagram Protocol |
Type | Connection-oriented protocol | Connection-less protocol |
Used By | HTTP, HTTPS, FTP, SMTP, Telnet | DNS, DHCP |
Speed | Slower | Faster |
Reliability | Guaranteed delivery of data | No guarantee of delivery |
Data Transmission | Stream-based; no clear separation between packets | Packet-based (Datagrams); sent independently |
Order of Data | Ensures data is received in order | No guarantee of order |
Header Size | 20 bytes | 8 bytes (typically) |
Weight | Heavyweight | Lightweight |
Acknowledgment | Has acknowledgment segments | No acknowledgment |
Handshake | Uses handshake (e.g., SYN, SYN-ACK, ACK) | No handshake |