0% found this document useful (0 votes)
4 views

Program 6- TCP and IP

The document describes a client-server program using TCP/IP sockets where the client requests a file by name and the server responds with the file's contents if it exists. The server listens on a specified port, accepts client connections, and reads the requested file, sending its contents back to the client. The client connects to the server, sends the file name, and displays the received file contents or an error message if the file is not found.

Uploaded by

sreekantha.aiml
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Program 6- TCP and IP

The document describes a client-server program using TCP/IP sockets where the client requests a file by name and the server responds with the file's contents if it exists. The server listens on a specified port, accepts client connections, and reads the requested file, sending its contents back to the client. The client connects to the server, sends the file name, and displays the received file contents or an error message if the file is not found.

Uploaded by

sreekantha.aiml
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Program 6: Using TCP/IP sockets, write a client – server program to make the client send the

file name and to make the server send back the contents of the requested file if present.

Server Program (FileServer.java)

This server program listens on a specified port and waits for the client to send the name of the
file. If the file exists on the server, it sends the file contents back to the client.

Program:-

import java.io.*;

import java.net.*;

public class FileServer

public static void main(String[] args)

int port = 12345; // Port number for the server

try (ServerSocket serverSocket = new ServerSocket(port))

System.out.println("Server is listening on port " + port);

// Accept client connection

try (Socket socket = serverSocket.accept())

System.out.println("Client connected.");

// Create input and output streams

BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);

// Read the file name sent by the client

String fileName = reader.readLine();


System.out.println("Requested file: " + fileName);

File file = new File(fileName);

if (file.exists() && file.isFile()) {

// If file exists, send its contents

try (BufferedReader fileReader = new BufferedReader(new FileReader(file)))

String line;

while ((line = fileReader.readLine()) != null)

writer.println(line);

writer.println("EOF"); // End of file marker

} catch (IOException e) {

writer.println("Error reading file.");

else

writer.println("File not found.");

catch (IOException e)

System.err.println("Error on server: " + e.getMessage());

}
}

Client Program (FileClient.java)

The client connects to the server, sends the file name, and then reads the response from the
server (which is the contents of the requested file, or an error message if the file isn't found).

Program:-

import java.io.*;

import java.net.*;

public class FileClient

public static void main(String[] args)

String serverAddress = "localhost"; // Server address (localhost for local machine)

int serverPort = 12345; // Port number used by the server

try (Socket socket = new Socket(serverAddress, serverPort))

// Create input and output streams

BufferedReader userInputReader = new BufferedReader(new InputStreamReader(System.in));

PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);

BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));


// Prompt user for the file name to request from the server

System.out.print("Enter the file name to request: ");

String fileName = userInputReader.readLine();

// Send the file name to the server

writer.println(fileName);

// Read and display the server's response

String line;

while ((line = reader.readLine()) != null) {

if (line.equals("EOF")) {

break; // End of file

System.out.println(line); // Print each line of the file

} catch (IOException e) {

System.err.println("Error on client: " + e.getMessage());

Output:-
************** END******************************************

You might also like