Fundamentals of Java Network Programming
Network programming in Java involves three core components: IP addresses, port numbers, and communication protocols.
IP Address
An IP address uniquely identifies a device on a network. The loopback address 127.0.0.1 represents the local machine.
Port Number
Port numbers distinguish between different applications on a device. Valid ports range from 0 to 65535, with 0–1024 typically reserved for system use.
Communication Protocols
Protocols define rules for data exchenge. Key transport layer protocols are TCP and UDP.
TCP and UDP Protocols
TCP (Transmission Control Protocol)
- Connection-oriented: requires establishing a connection before data transfer.
- Reliable: uses a three-way handshake for connection setup and four-way handshake for termination.
- Supports large data volumes but has higher overhead due to connection management.
UDP (User Datagram Protoocl)
- Connectionless: sends data in packets without prior connection establishment.
- Unreliable but faster, with each datagram limited to 64 KB.
TCP Programming with Sockets
Sockets enable communication between applications over a network, treating the connection as a stream.
Key Socket Methods
| Method | Description |
|---|---|
void close() |
Closes the socket. |
InputStream getInputStream() |
Retrieves input stream for reading data. |
OutputStream getOutputStream() |
Retrieves output stream for writing data. |
ServerSocket Methods
| Method | Description |
|---|---|
Socket accept() |
Listens for and accepts client connections. |
void close() |
Closes the server socket. |
Client-Side Proces
- Create a
Socketobject with server IP and port. - Obtain input/output streams using
getInputStream()andgetOutputStream(). - Read from and write to the streams.
- Close the socket.
Example client code:
Socket clientSocket = new Socket("192.168.1.10", 8080);
OutputStream output = clientSocket.getOutputStream();
output.write("data".getBytes());
clientSocket.close();
Server-Side Process
- Create a
ServerSocketbound to a port. - Call
accept()to wait for and accept client connections. - Use the returned
Socketto get streams for data exchange. - Close sockets after communication.
Example server code:
ServerSocket server = new ServerSocket(8080);
Socket connection = server.accept();
InputStream input = connection.getInputStream();
byte[] buffer = new byte[1024];
int bytesRead = input.read(buffer);
String message = new String(buffer, 0, bytesRead);
connection.close();
server.close();
UDP Programming with Datagrams
UDP uses DatagramSocket and DatagramPacket for packet-based communication.
Process
- Create sender and receiver
DatagramSocketinstances. - Package data into
DatagramPacketobjects with target address and port. - Send or receive packets.
- Close the socket.
Example sender:
DatagramSocket senderSocket = new DatagramSocket();
byte[] dataBytes = "message".getBytes();
DatagramPacket packet = new DatagramPacket(dataBytes, dataBytes.length, InetAddress.getByName("localhost"), 9000);
senderSocket.send(packet);
senderSocket.close();
Example receiver:
DatagramSocket receiverSocket = new DatagramSocket(9000);
byte[] buffer = new byte[1024];
DatagramPacket receivedPacket = new DatagramPacket(buffer, buffer.length);
receiverSocket.receive(receivedPacket);
String content = new String(receivedPacket.getData(), 0, receivedPacket.getLength());
receiverSocket.close();