0% found this document useful (0 votes)
9 views3 pages

Multi Client

The document contains Java code for a multi-client server application that calculates the area of a circle based on user-provided radius. The server listens for client connections and spawns a new thread for each client to handle area calculations. The client sends the radius to the server and receives the computed area in response.

Uploaded by

helina3leul
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)
9 views3 pages

Multi Client

The document contains Java code for a multi-client server application that calculates the area of a circle based on user-provided radius. The server listens for client connections and spawns a new thread for each client to handle area calculations. The client sends the radius to the server and receives the computed area in response.

Uploaded by

helina3leul
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

Example 1: Handling Multi-clients

package socketprogrammingdemo;

import [Link].*;
import [Link].*;
import [Link];
public class MultiClientCircleAreaServer {
public static void main(String[] args) {
int port = 1234;
try (ServerSocket serverSocket = new ServerSocket(port)) {
[Link]("Server started. Waiting for clients...");
while (true) {
Socket clientSocket = [Link]();
[Link]("Client connected.");
new ClientHandler(clientSocket).start();
}
} catch (IOException e) {
[Link]();
}
}
}
class ClientHandler extends Thread {
private Socket socket;
ClientHandler(Socket socket) {
[Link] = socket;
}

public void run() {


try (
Scanner in = new Scanner([Link]());
PrintWriter out = new PrintWriter([Link](), true)
){
double radius = [Link]();
double area = [Link] * radius * radius;
[Link](area);
[Link]("Computed area for client: " + area);
} catch (IOException e) {
[Link]("Client disconnected.");
} finally {
try {
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
}
package socketprogrammingdemo;

import [Link].*;
import [Link].*;
import [Link];

public class CircleAreaClientMultiClientDemo {


public static void main(String[] args) {
String host = "localhost";
int port = 1234;
try (Socket socket = new Socket(host, port)) {
Scanner userInput = new Scanner([Link]);
Scanner in = new Scanner([Link]());
PrintWriter out = new PrintWriter([Link](), true);
[Link]("Enter radius of the circle: ");
double radius = [Link]();
[Link](radius);
double area = [Link]();
[Link]("The area of the circle is: %.2f\n", area);
[Link]();
} catch (IOException e) {
[Link]();
}
}
}

You might also like