0% found this document useful (0 votes)
7 views5 pages

JP Manual Answers Experiment 25

The document contains two programming exercises involving socket communication in Java. The first exercise demonstrates user authentication via a server-client model, where the server checks credentials against stored values. The second exercise illustrates a file transfer application, allowing a client to send a file to a server using sockets.

Uploaded by

Riya Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

JP Manual Answers Experiment 25

The document contains two programming exercises involving socket communication in Java. The first exercise demonstrates user authentication via a server-client model, where the server checks credentials against stored values. The second exercise illustrates a file transfer application, allowing a client to send a file to a server using sockets.

Uploaded by

Riya Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No.

: 25

Program Statement 1: Write a program to check credentials of users(Client will send user id
and password to server and server will authenticate the client using equals()).

Source Code:

//Server.java

import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Map;

public class Server {

private static final Map<String, String> USER_CREDENTIALS = new


HashMap<>();

static {
USER_CREDENTIALS.put("user1", "pass123");
USER_CREDENTIALS.put("user2", "securePass");
USER_CREDENTIALS.put("admin", "adminPass");
}

public static void main(String[] args) {


try (ServerSocket serverSocket = new ServerSocket(12345)) {
System.out.println("Server started. Listening on port 12345...");

while (true) {
try (Socket clientSocket = serverSocket.accept();
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true)) {

System.out.println("Client connected: " + clientSocket.getInetAddress());

String userId = in.readLine();


String password = in.readLine();

if (authenticate(userId, password)) {
out.println("Authentication successful.");
} else {
out.println("Authentication failed.");
}
} catch (IOException e) {
System.err.println("Error handling client: " + e.getMessage());
}
}
} catch (IOException e) {
System.err.println("Server error: " + e.getMessage());
}
}

private static boolean authenticate(String userId, String password) {


String storedPassword = USER_CREDENTIALS.get(userId);
return storedPassword != null && storedPassword.equals(password);
}
}

//Client.java

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client {

public static void main(String[] args) {


try (Socket socket = new Socket("localhost", 12345);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
Scanner scanner = new Scanner(System.in)) {

System.out.print("Enter user ID: ");


String userId = scanner.nextLine();
out.println(userId);

System.out.print("Enter password: ");


String password = scanner.nextLine();
out.println(password);

String response = in.readLine();


System.out.println("Server response: " + response);

} catch (IOException e) {
System.err.println("Client error: " + e.getMessage());
}
}
}
Output:

On server’s side:

On client’s side:

Program Statement 2: WAP using Socket and ServerSocket to create Chat Application.

Source Code:

File_to_send.txt:

This is the file to be sent..........

// Serverex.java

import java.io.*;
import java.net.*;

public class Serverex {


public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(12345);
System.out.println("Server started on port 12345");

Socket socket = serverSocket.accept();


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

DataInputStream in = new DataInputStream(socket.getInputStream());


FileOutputStream out = new FileOutputStream("received_file.txt");

byte[] buffer = new byte[1024]; // Corrected: Declared the buffer


int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}

out.close();
in.close();
socket.close();
serverSocket.close();
System.out.println("File received successfully.");
}
}

// Clientex.java

import java.io.*;
import java.net.*;

public class Clientex {


public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 12345);
System.out.println("Connected to server");

FileInputStream in = new FileInputStream("file_to_send.txt");


DataOutputStream out = new DataOutputStream(socket.getOutputStream());

byte[] buffer = new byte[1024]; // Corrected: Declared the buffer


int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}

out.close();
in.close();
socket.close();
System.out.println("File sent successfully.");
}
}
Output:

On server’s side:

On client’s side:

You might also like