Java Networking

Network programming refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network. package of the J2SE APIs contains classes for Network or Socket Programming.


: TCP stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP. TCP is Connection oriented Protocol. All packets are traversed thru the connection created between devices.


: UDP stands for User Datagram Protocol, a connection-less protocol that allows for packets of data to be transmitted between applications. Packets traverse thru multiple paths, between devices. There is no guarantee in the order of packets received.

IP Address

: Each device on network is identified by IP Address. There are two versions of IP Address, IPv4 and IPv6. IPv4 is 32 bit, and IPv6 is 128 bit IP Address.

Port Numbers

: A device will have multiple(thousands) of Network port numbers. Purpose of port numbers is multiple programs(on a machine) can simultaneously send/receive data.
For example you may want to use Chat Application, Email Client, Web Browser etc… simultaneously. And those Applications may need to send and receive data over network at same time. For which each Application is binded to a port number, hence multiple port numbers are required, for Applications to simultaneously read and write data to network.Hence, if multiple port numbers are not available on device, then only one Application on Device can connect to Network.

TCP Sockets
TCP Sockets

Standard Port Numbers:
80 – HTTP (Protocol used by Web Browser and WebServer) Stands or Hyper Text Transfer Protocol
443 – HTTPS(also used by Web Browser and Web Server, for secured communication)
25 – SMTP(Simple Mail Transfer Protocol)
110 – POP3(Post Office Protocol),etc…

DNS(Domain Name Server)

converts domain name to IP Address. IP Address of Machines are required to Transfer data between Machines in a Network.

java networking
java networking

How to create TCP sockets between Client and Server Machines

ServerSocket class is used by server applications to obtain a port and listen for client requests.
Socket represents the socket that both the client and server use to communicate with each other. The client obtains a Socket object by instantiating one, whereas the server obtains a Socket object from the return value of the accept() method.

Socket and ServerSocket support both versions of IP Address IPv4 and IPv6.

How to create UDP sockets between Machines

DatagramSocket: Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may be routed differently. Datagram packets may be received in different order, than sent.
DatagramPacket: Data is sent thru DatagramPacket over DatagramSocket

How to send and receive HTTP request/response, from/to Web server?

URLConnection – Base class of HttpURLConnection
HttpURLConnection – sends HTTP Request(GET or POST method) to a Web Server, and gets the response.
HttpsURLConnection – same as HttpURLConnection, but this is secured version.

You may also like to read:
RMI in Java
Java Socket Client example
ServerSocket example