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

Java Practical No 25

The document contains Java code for a simple client-server application using sockets. It includes a basic server and client that exchange messages, a user authentication system, and a chat application where both server and client can send messages until one disconnects. Each section provides the server and client code along with brief descriptions of their functionalities.

Uploaded by

dipya2121
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Java Practical No 25

The document contains Java code for a simple client-server application using sockets. It includes a basic server and client that exchange messages, a user authentication system, and a chat application where both server and client can send messages until one disconnects. Each section provides the server and client code along with brief descriptions of their functionalities.

Uploaded by

dipya2121
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Practical No 25

Title code:
Code for server:
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(5000);
System.out.println("Server is waiting for a client...");
Socket socket = serverSocket.accept();
System.out.println("Client connected.");
BufferedReader input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
String message = input.readLine();
System.out.println("Client: " + message);
output.println("Hello from Server!");
input.close();
output.close();
socket.close();
serverSocket.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

Code for client:


import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 5000);
System.out.println("Connected to server.");
BufferedReader input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
output.println("Hello from Client!");
String message = input.readLine();
System.out.println("Server: " + message);
input.close();
output.close();
socket.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

O/P:
Server terminal:
Client terminal:

4. 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())
Ans:
Server code
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(5000);
System.out.println("Server is waiting for a client...");
Socket socket = serverSocket.accept();
System.out.println("Client connected.");
BufferedReader input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
String userId = input.readLine();
String password = input.readLine();
if (userId.equals("admin") && password.equals("password123")) {
output.println("Login Successful");
} else {
output.println("Invalid Credentials");
}
input.close();
output.close();
socket.close();
serverSocket.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

Client code:
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 5000);
System.out.println("Connected to server.");
BufferedReader input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
BufferedReader userInput = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter User ID: ");
String userId = userInput.readLine();
System.out.print("Enter Password: ");
String password = userInput.readLine();
output.println(userId);
output.println(password);
String response = input.readLine();
System.out.println("Server: " + response);
input.close();
output.close();
socket.close();
userInput.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

O/P:
Server terminal

Client terminal
5. Write a program using Socket and ServerSocket to
create Chat Application
Ans:
ChatServer:
import java.io.*;
import java.net.*;
public class ChatServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(5000);
System.out.println("Server is waiting for a client...");
Socket socket = serverSocket.accept();
System.out.println("Client connected.");
BufferedReader input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
BufferedReader userInput = new BufferedReader(new
InputStreamReader(System.in));
String message;
while (true) {
message = input.readLine();
if (message.equalsIgnoreCase("exit")) {
System.out.println("Client disconnected.");
break;
}
System.out.println("Client: " + message);
System.out.print("Server: ");
message = userInput.readLine();
output.println(message);
}
input.close();
output.close();
socket.close();
serverSocket.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

ChatClient:
import java.io.*;
import java.net.*;
public class ChatClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 5000);
System.out.println("Connected to server.");
BufferedReader input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
BufferedReader userInput = new BufferedReader(new
InputStreamReader(System.in));
String message;
while (true) {
System.out.print("Client: ");
message = userInput.readLine();
output.println(message);
if (message.equalsIgnoreCase("exit")) {
System.out.println("Disconnected from server.");
break;
}
message = input.readLine();
System.out.println("Server: " + message);
}
input.close();
output.close();
socket.close();
userInput.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

ChatServer Terminal:

ChatClient Server:

You might also like