Java Networking

Sockets and URL Illustration . . How to build client socket . . Client Socket code example . . Server Socket class . . Building server socket code . . Server Socket Code Example . . URL classes . . URL Code Example

What are sockets ?

Sockets are services at the end point of communication links. We can exchange data between processes by transferring data through the socket.

Two types of sockets are implemented in Java:

STREAM SOCKETS

called TCP/IP sockets providing access to the Transport Communication Protocol running over the Internet Protocol (TCP/IP). TCP/IP sockets present major data communications on the Internet. The basic features of TCP/IP sockets: - They are connection oriented services transferring data without record boundaries. - The process will be informed when connection is broken. Main Internet applications use TCP/IP services: FTP - File Transfer Protocol SMTP - Simple Mail Transfer Protocol TELNET - the remote terminal connection TCP/IP sockets implemented in Java with set of classes: Socket, ServerSocket, SocketImpl and with SocketImplFactory interface. Stream sockets can be connected to Input/Output streams. Once socket connection is established, data can be transferred through these sockets as a byte stream.

DATAGRAM SOCKETS

called UDP sockets provide access to Unreliable Datagram Protocol (UDP). UDP is a broadcast protocol, that does not guarantee the reception of its messages. - Datagram sockets are light weight sockets. - They don't provide error checking, so they are faster than TCP/IP sockets. In the case when you send the same information repeatedly you can save resources by choosing UDP sockets. UDP sockets implemented in Java with: DatagramSocket and DatagramPacket classes.

Back To Java School