Skip to content

Commit ada006d

Browse files
committed
updated
1 parent bf096f4 commit ada006d

10 files changed

+451
-3
lines changed

java-multithreading/Client.java

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.io.*;
2+
import java.net.*;
3+
import java.awt.*;
4+
import java.awt.event.*;
5+
import javax.swing.*;
6+
7+
public class Client extends JFrame {
8+
// Text field for receiving radius
9+
private JTextField jtf = new JTextField();
10+
11+
// Text area to display contents
12+
private JTextArea jta = new JTextArea();
13+
14+
// IO streams
15+
private DataOutputStream toServer;
16+
private DataInputStream fromServer;
17+
18+
public static void main(String[] args) {
19+
new Client();
20+
}
21+
22+
public Client() {
23+
// Panel p to hold the label and text field
24+
JPanel p = new JPanel();
25+
p.setLayout(new BorderLayout());
26+
p.add(new JLabel("Enter radius"), BorderLayout.WEST);
27+
p.add(jtf, BorderLayout.CENTER);
28+
jtf.setHorizontalAlignment(JTextField.RIGHT);
29+
30+
setLayout(new BorderLayout());
31+
add(p, BorderLayout.NORTH);
32+
add(new JScrollPane(jta), BorderLayout.CENTER);
33+
34+
jtf.addActionListener(new ButtonListener()); // Register listener
35+
36+
setTitle("Client");
37+
setSize(500, 300);
38+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
39+
setVisible(true); // It is necessary to show the frame here!
40+
41+
try {
42+
// Create a socket to connect to the server
43+
Socket socket = new Socket("localhost", 8000);
44+
// Socket socket = new Socket("130.254.204.36", 8000);
45+
// Socket socket = new Socket("drake.Armstrong.edu", 8000);
46+
47+
// Create an input stream to receive data from the server
48+
fromServer = new DataInputStream(
49+
socket.getInputStream());
50+
51+
// Create an output stream to send data to the server
52+
toServer =
53+
new DataOutputStream(socket.getOutputStream());
54+
}
55+
catch (IOException ex) {
56+
jta.append(ex.toString() + '\n');
57+
}
58+
}
59+
60+
private class ButtonListener implements ActionListener {
61+
public void actionPerformed(ActionEvent e) {
62+
try {
63+
// Get the radius from the text field
64+
double radius = Double.parseDouble(jtf.getText().trim());
65+
66+
// Send the radius to the server
67+
toServer.writeDouble(radius);
68+
toServer.flush();
69+
70+
// Get area from the server
71+
double area = fromServer.readDouble();
72+
73+
// Display to the text area
74+
jta.append("Radius is " + radius + "\n");
75+
jta.append("Area received from the server is "
76+
+ area + '\n');
77+
}
78+
catch (IOException ex) {
79+
System.err.println(ex);
80+
}
81+
}
82+
}
83+
}

java-multithreading/ClientAB.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.io.*;
2+
import java.net.*;
3+
import java.util.Scanner;
4+
5+
6+
public class ClientAB
7+
{
8+
private DataOutputStream toServer;
9+
private DataInputStream fromServer;
10+
public static void main(String[] args)
11+
{
12+
new ClientAB();
13+
}
14+
public ClientAB()
15+
{
16+
try
17+
{
18+
// Create a socket to connect to the server
19+
Socket socket = new Socket("localhost", 8000);
20+
21+
Scanner sc=new Scanner(System.in);
22+
System.out.println("Please Enter radius");
23+
Double radius=sc.nextDouble();
24+
while(radius !=0)
25+
{
26+
// Create an input stream to receive data from the server
27+
fromServer = new DataInputStream(
28+
socket.getInputStream());
29+
30+
// Create an output stream to send data to the server
31+
toServer =new DataOutputStream(socket.getOutputStream());
32+
toServer.writeDouble(radius);
33+
toServer.flush();
34+
double area = fromServer.readDouble();
35+
36+
System.out.println("Please area of cirlce" + area);
37+
System.out.println("Please Enter radius");
38+
radius=sc.nextDouble();
39+
}
40+
41+
}
42+
catch (IOException ex)
43+
{
44+
System.out.println(ex.toString() + '\n');
45+
}
46+
}
47+
48+
49+
}
+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import java.io.*;
2+
import java.net.*;
3+
import java.util.*;
4+
import java.awt.*;
5+
import javax.swing.*;
6+
7+
public class MultiThreadServer extends JFrame {
8+
// Text area for displaying contents
9+
private JTextArea jta = new JTextArea();
10+
11+
public static void main(String[] args) {
12+
new MultiThreadServer();
13+
}
14+
15+
public MultiThreadServer() {
16+
// Place text area on the frame
17+
setLayout(new BorderLayout());
18+
add(new JScrollPane(jta), BorderLayout.CENTER);
19+
20+
setTitle("MultiThreadServer");
21+
setSize(500, 300);
22+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23+
setVisible(true); // It is necessary to show the frame here!
24+
25+
try {
26+
// Create a server socket
27+
ServerSocket serverSocket = new ServerSocket(8000);
28+
jta.append("MultiThreadServer started at " + new Date() + '\n');
29+
30+
// Number a client
31+
int clientNo = 1;
32+
33+
while (true) {
34+
// Listen for a new connection request
35+
Socket socket = serverSocket.accept();
36+
37+
// Display the client number
38+
jta.append("Starting thread for client " + clientNo +
39+
" at " + new Date() + '\n');
40+
41+
// Find the client's host name, and IP address
42+
InetAddress inetAddress = socket.getInetAddress();
43+
jta.append("Client " + clientNo + "'s host name is "
44+
+ inetAddress.getHostName() + "\n");
45+
jta.append("Client " + clientNo + "'s IP Address is "
46+
+ inetAddress.getHostAddress() + "\n");
47+
48+
// Create a new thread for the connection
49+
HandleAClient task = new HandleAClient(socket);
50+
51+
// Start the new thread
52+
new Thread(task).start();
53+
54+
// Increment clientNo
55+
clientNo++;
56+
}
57+
}
58+
catch(IOException ex) {
59+
System.err.println(ex);
60+
}
61+
}
62+
63+
// Inner class
64+
// Define the thread class for handling new connection
65+
class HandleAClient implements Runnable {
66+
private Socket socket; // A connected socket
67+
68+
/** Construct a thread */
69+
public HandleAClient(Socket socket) {
70+
this.socket = socket;
71+
}
72+
73+
/** Run a thread */
74+
public void run() {
75+
try {
76+
// Create data input and output streams
77+
DataInputStream inputFromClient = new DataInputStream(
78+
socket.getInputStream());
79+
DataOutputStream outputToClient = new DataOutputStream(
80+
socket.getOutputStream());
81+
82+
// Continuously serve the client
83+
while (true) {
84+
// Receive radius from the client
85+
double radius = inputFromClient.readDouble();
86+
87+
// Compute area
88+
double area = radius * radius * Math.PI;
89+
90+
// Send area back to the client
91+
outputToClient.writeDouble(area);
92+
93+
jta.append("radius received from client: " +
94+
radius + '\n');
95+
jta.append("Area found: " + area + '\n');
96+
}
97+
}
98+
catch(IOException e) {
99+
System.err.println(e);
100+
}
101+
}
102+
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import java.io.*;
2+
import java.net.*;
3+
import java.util.*;
4+
5+
6+
public class MultiThreadServerAB
7+
{
8+
9+
10+
public static void main(String[] args) {
11+
new MultiThreadServerAB();
12+
}
13+
14+
public MultiThreadServerAB() {
15+
16+
17+
18+
try {
19+
// Create a server socket
20+
ServerSocket serverSocket = new ServerSocket(8000);
21+
System.out.println("MultiThreadServer started at " + new Date() + '\n');
22+
23+
// Number a client
24+
int clientNo = 1;
25+
26+
while (true) {
27+
// Listen for a new connection request
28+
Socket socket = serverSocket.accept();
29+
30+
// Display the client number
31+
System.out.println("Starting thread for client " + clientNo +
32+
" at " + new Date() + '\n');
33+
34+
// Find the client's host name, and IP address
35+
InetAddress inetAddress = socket.getInetAddress();
36+
System.out.println("Client " + clientNo + "'s host name is "
37+
+ inetAddress.getHostName() + "\n");
38+
System.out.println("Client " + clientNo + "'s IP Address is "
39+
+ inetAddress.getHostAddress() + "\n");
40+
41+
// Create a new thread for the connection
42+
HandleAClient task = new HandleAClient(socket);
43+
Thread t1 = new Thread(task);
44+
// Start the new thread
45+
t1.start();
46+
47+
// Increment clientNo
48+
clientNo++;
49+
}
50+
}
51+
catch(IOException ex) {
52+
System.err.println(ex);
53+
}
54+
}
55+
56+
// Inner class
57+
// Define the thread class for handling new connection
58+
class HandleAClient implements Runnable {
59+
private Socket socket; // A connected socket
60+
61+
/** Construct a thread */
62+
public HandleAClient(Socket socket) {
63+
this.socket = socket;
64+
}
65+
66+
/** Run a thread */
67+
public void run() {
68+
try {
69+
// Create data input and output streams
70+
DataInputStream inputFromClient = new DataInputStream(
71+
socket.getInputStream());
72+
DataOutputStream outputToClient = new DataOutputStream(
73+
socket.getOutputStream());
74+
75+
// Continuously serve the client
76+
while (true) {
77+
// Receive radius from the client
78+
double radius = inputFromClient.readDouble();
79+
80+
// Compute area
81+
double area = radius * radius * Math.PI;
82+
83+
// Send area back to the client
84+
outputToClient.writeDouble(area);
85+
86+
System.out.println("radius received from client: " +
87+
radius + '\n');
88+
System.out.println("Area found: " + area + '\n');
89+
}
90+
}
91+
catch(IOException e) {
92+
System.err.println(e);
93+
}
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)