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

23 Practical: 16: Name:Ashwin Pawar Roll No.

This document discusses Java socket programming and creating a chat application and prime number checking program using client-server architecture. The document provides code for the server and client for both programs.

Uploaded by

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

23 Practical: 16: Name:Ashwin Pawar Roll No.

This document discusses Java socket programming and creating a chat application and prime number checking program using client-server architecture. The document provides code for the server and client for both programs.

Uploaded by

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

Advanced Java Programming

Name :ASHWIN PAWAR CO5I Roll No. : 23

Practical : 16

Q. Write a program using Socket and ServerSocket to create Chat Application?


CODE:
Server code:
import java.io.*;
import java.net.*;

public class practical_16 {

// server program
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(1254);
System.out.println("Server Started, waiting for client connection");
Socket sl = s.accept();
OutputStream out = sl.getOutputStream();

DataOutputStream dos = new DataOutputStream(out);


dos.writeUTF("Hey there i am pranjal Shahane");

dos.close();
out.close();
sl.close();

s.close();
}
}
Client side code:
import java.io.*;
import java.net.*;
Advanced Java Programming

public class practical16A {


// client side

public static void main (String[] args) throws IOException {


Socket s1 = new Socket("localhost", 1254);

InputStream In = s1.getInputStream();
DataInputStream dis = new DataInputStream(In);

String st = new String(dis.readUTF());


System.out.println(st);
dis.close();
In.close();
s1.close();
}
}
OUTPUT:

Q. Write a program to develop prime number Server (Client will send any number to
server, Sever will send the response the number is prime or not)?

SERVER CODE :
import java.io.*;
import java.net.*;

public class practical_16_2_server {


public static void main(String[] args) throws IOException {
Advanced Java Programming

ServerSocket serverSocket = null;


Socket clientSocket = null;
try {
serverSocket = new ServerSocket(8002);
System.out.println("Server socket is created and waiting for a client");
clientSocket = serverSocket.accept();
System.out.println("Client connected from: " + clientSocket.getInetAddress());
DataInputStream inputStream = new
DataInputStream(clientSocket.getInputStream());
DataOutputStream outputStream = new
DataOutputStream(clientSocket.getOutputStream());
while (true) {
int number = inputStream.readInt();
System.out.println("Client sent number: " + number);
boolean isPrime = isPrime(number);
outputStream.writeBoolean(isPrime);
System.out.println("Response sent to client: " + isPrime);
}
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
} finally {
if (clientSocket != null) {
clientSocket.close();
}
if (serverSocket != null) {
serverSocket.close();
}
}
}
private static boolean isPrime(int num) {
if (num <= 1) {
Advanced Java Programming

return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
OUTPUT:

Client CODE:
import java.io.*;
import java.net.*;

public class practical_16_2_client {


public static void main(String[] args) throws IOException {
Socket clientSocket = null;
try {
clientSocket = new Socket(InetAddress.getLocalHost(), 8002);
System.out.println("Client socket is created and waiting for the server");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
DataInputStream inputStream = new
DataInputStream(clientSocket.getInputStream());
DataOutputStream outputStream = new
DataOutputStream(clientSocket.getOutputStream());
while (true) {
System.out.print("Enter a number to check for prime (or type 'exit' to quit): ");
Advanced Java Programming

String input = br.readLine();


if (input.equalsIgnoreCase("exit")) {
break;
}
try {
int number = Integer.parseInt(input);
outputStream.writeInt(number);
boolean isPrime = inputStream.readBoolean();
System.out.println(number + " is prime: " + isPrime);
} catch (NumberFormatException e) {
System.err.println("Invalid input. Please enter a valid number.");
}
}
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
} finally {
if (clientSocket != null) {
clientSocket.close();
}
}
}
}
OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy