Reputation: 187
I searched and couldn't find relevant solution for my problem. I'm trying to send integer to server to calculate its factorial with multithreading. But I get "connection refused" error. Here is my code :
package ohw3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class MathClient extends JFrame {
static JTextArea jta = new JTextArea(5, 1);
public static void main(String[] args) {
new MathClient();
}
public MathClient() {
setTitle("Factorial Calculator");
setSize(400, 300);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JScrollPane(jta), BorderLayout.NORTH);
JPanel panel1 = new JPanel();
panel1.setBorder(new TitledBorder("Factorial Calculator"));
panel1.setFocusable(true);
JLabel necess = new JLabel("Please Enter the Integer:");
panel1.add(necess, BorderLayout.SOUTH);
final JTextField integer = new JTextField(15);
panel1.add(integer);
JButton send = new JButton("Calculate");
panel1.add(send, BorderLayout.NORTH);
add(panel1);
setVisible(true);
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(1080);
serverSocket.setSoTimeout(3000);
jta.append("MultiThreadServer started at '' " + new Date() + " ''\n");
// Number a client
int clientNo = 1;
while (true) {
// Listen for a new connection request
final Socket socket = new Socket("localhost", 1080);
// Display the client number
jta.append("Starting thread for client " + clientNo
+ " at '' " + new Date() + " ''\n");
// Find the client's host name, and IP address
InetAddress inetAddress = socket.getInetAddress();
jta.append("Client " + clientNo + "'s host name is "
+ inetAddress.getHostName() + "\n");
jta.append("Client " + clientNo + "'s IP Address is "
+ inetAddress.getHostAddress() + "\n");
send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int value = Integer.parseInt(integer.getText());
// Create a new thread for the connection
MultiThreadedMathServer task = new MultiThreadedMathServer(socket, value);
// Start the new thread
new Thread(task).start();
}
});
// Increment clientNo
serverSocket.close();
clientNo++;
}
} catch (IOException ex) {
System.err.println(ex);
}
}
class MultiThreadedMathServer implements Runnable {
private final Socket socket; // A connected socket
private final int numb; // A connected socket
/**
* Construct a thread
*/
public MultiThreadedMathServer(Socket socket, int numb) {
this.socket = socket;
this.numb = numb;
}
/**
* Run a thread
*/
@Override
public void run() {
try {
// Create data input and output streams
DataInputStream inputFromClient = new DataInputStream(
socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(
socket.getOutputStream());
// Continuously serve the client
while (true) {
// Receive radius from the client
int integer = inputFromClient.readInt();
// Compute area
int fact = 1;
for (int i = integer; i > 0; i--) {
fact = fact * i;
}
// Send area back to the client
outputToClient.writeDouble(fact);
}
} catch (IOException e) {
System.err.println(e);
}
}
}
}
Upvotes: 0
Views: 87
Reputation: 32963
Your server has no provisions for accepting a connection, so it doesn't know what to do with the incoming connection.
Socket clientSocket = socket.accept();
// and then you can use the socket for reading and writing.
But I suggest you review a good tutorial on Java networking, like the official tutorial.
Upvotes: 1
Reputation: 5341
As fvu says, your server is not accepting sockets. Also, MultiThreadedMathServer
thread initialization looks misplaced. You should create it on every new accepted socket connection received on server. Something like:
while(true){
MultiThreadedMathServer task = new MultiThreadedMathServer(serverSocket.accept(), value);
new Thread(task).start(); //this thread will deal with this client
}
Because, serverSocket.accept()
will hang until new connection arrived, upon arrival a new thread will deal with that socket and server will come back to accept()
new sockets again.
Upvotes: 1