7 Network Programming With Sockets

Advanced Java Programming · Unit 7

Network Programming with Sockets

Exam-focused notes for Network Programming with Sockets (Advanced Java Programming, BIT401): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.

Network Programming with Sockets in short

Network programming lets two programs on different computers exchange data. Java does this with sockets, using the TCP and UDP protocols.

Network Programming with Sockets: notes

Client-server communication

In the client-server model a server program waits for requests and a client program sends them. The server runs first and listens. The client connects, sends a request, and reads the reply. Each computer on a network has an IP address, and each program on it uses a port number.

Port usage and significance

A port is a 16-bit number from 0 to 65535 that names one program on a computer. The IP address finds the machine, and the port finds the right program on it. Without ports, a computer could run only one network program at a time.

  • 0 to 1023: well-known ports such as HTTP (80), HTTPS (443) and SSH (22).
  • 1024 to 49151: registered ports used by applications.
  • 49152 to 65535: dynamic ports given to clients for short use.

A server picks a fixed port so that clients know where to connect. Our own servers should use a port above 1024.

TCP socket programming steps

TCP is connection-oriented. It sets up a link first, delivers data in order, and resends lost data. A socket is one end of the link.

Server steps:

  • Create a ServerSocket on a port.
  • Call accept(). It waits until a client connects and returns a Socket.
  • Get input and output streams from that socket.
  • Read the request, process it, write the reply.
  • Close the streams and the socket.

Client steps:

  • Create a Socket with the server address and port.
  • Get the input and output streams.
  • Send the request and read the reply.
  • Close the socket.

Socket creation and connection

new ServerSocket(5000) opens the server port. new Socket("localhost", 5000) connects to it. If the server is not running, the client gets a ConnectException. The accept() call blocks, so servers often run it in a loop and give each client its own thread.

What this unit covers

  • TCP socket programming steps
  • UDP socket programming steps
  • Client-server communication
  • Port usage and significance
  • Socket creation and connection
  • Data transmission over sockets

TCP socket programming steps

2082 · 5 marks

Mention the steps to write the socket program using TCP. [5]

Show solved answer

Model Answer: Steps to Write a Socket Program Using TCP

Since reference notes are not provided, this answer follows standard TCP socket programming methodology taught in BSc CSIT curricula:

Server-Side Steps:

  1. Create a Socket

    • Use socket() system call to create a socket with domain (AF_INET), type (SOCK_STREAM), and protocol (0 or IPPROTO_TCP)
  2. Bind the Socket

    • Use bind() to associate the socket with a specific port number and IP address
    • Define server address structure with port and address family
  3. Listen for Connections

    • Use listen() to mark the socket as passive and specify the backlog queue size for pending connections
  4. Accept Client Connection

    • Use accept() to accept incoming client connections
    • Creates a new socket for communication with the connected client
  5. Send/Receive Data

    • Use send() or write() to transmit data to the client
    • Use recv() or read() to receive data from the client
  6. Close the Socket

    • Use close() to terminate the connection and release resources

Client-Side Steps:

  1. Create a Socket

    • Use socket() with same parameters as server
  2. Connect to Server

    • Use connect() to establish connection to server's IP address and port
  3. Send/Receive Data

    • Use send() or write() to send data to server
    • Use recv() or read() to receive data from server
  4. Close the Socket

    • Use close() to terminate connection

Key Point: TCP ensures reliable, ordered, connection-oriented communication between client and server.

Open this answer on its own page →

2080 · 5 marks

Write a socket program using TCP where client sends an integer to server and server response whether it is positive or negative. [5]

A TCP client-server program where: - Client: Connects to server and sends an integer - Server: Receives integer, determines if positive/negative, sends response back --- --- --- 1. Compile both programs: 2. Run server (Terminal 1): 3. Run client (Terminal 2): 4. Sample Output: - Client: Enter an ...

Open this answer on its own page →

0 · 5 marks

Write down a java program where client sends a number to server and server calculates its factorial and sends it back to client using TCP. [5]

Here is a complete Java program demonstrating client-server communication using TCP sockets for factorial calculation: 1. ServerSocket: Listens on port 5000 for incoming client connections 2. Socket: Establishes connection between client and server 3. PrintWriter: Sends data to the other end (wit...

Open this answer on its own page →

UDP socket programming steps

2081 · 5 marks

Write the steps of writing socket program using UDP. [5]

UDP (User Datagram Protocol) is a connectionless, unreliable transport layer protocol. The following are the steps to write a UDP socket program: 1. Create a socket - Use socket() system call with parameters: AFINET (address family), SOCKDGRAM (socket type for UDP), and IPPROTOUDP (protocol) - Ex...

Open this answer on its own page →

Port usage and significance

2081 · 5 marks

Why do we need ports in socket programming? What types of instructions are written in try block? [5]

Ports serve the following critical purposes: 1. Process Identification: Ports uniquely identify specific processes or services running on a host machine. Since a single computer can run multiple network applications simultaneously, ports distinguish between them. 2. Multiplexing Communication: Po...

Open this answer on its own page →