Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Fundamentals of Java Network Programming

Tech May 12 3

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

  1. Create a Socket object with server IP and port.
  2. Obtain input/output streams using getInputStream() and getOutputStream().
  3. Read from and write to the streams.
  4. 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

  1. Create a ServerSocket bound to a port.
  2. Call accept() to wait for and accept client connections.
  3. Use the returned Socket to get streams for data exchange.
  4. 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 DatagramSocket instances.
  • Package data into DatagramPacket objects 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();

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.