所以我试图在这种情况下创建一个多线程服务器(尝试解释尽可能简单):
目前,我的客户端无法连接到服务器。我对java很新,所以请耐心等待。我如何将计算结果发送回服务器?
MultiThreadServer:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Random;
public class MultiThreadServer {
public static double SendNumber;
public static int Delay = 500;
public static String ReceivedSum;
public static final int PORT = 2000;
public static void main(String[] args) throws IOException, InterruptedException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Server socket: " + s);
System.out.println("Server listening...");
try { while(true) {
Socket socket = s.accept();
// Blocks until a connection occurs:
System.out.println("Connection accepted.");
System.out.println("The new socket: " + socket);
DataInputStream din = new DataInputStream(socket.getInputStream());
DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Client t = new Client(socket);
System.out.println("New thread started.");
System.out.println("The new thread: " + t);
Random r = new Random();
for(int i=0;i<10;i++) {
SendNumber = r.nextDouble();
dout.writeDouble(SendNumber);
}
Thread.sleep(Delay);
System.out.println("Client Sum is: " + din.read());
}
} //catch, while, try
finally { s.close(); }
} //main
} //MultiServer
客户端:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Random;
import javafx.application.Platform;
class Client extends Thread{
private PrintWriter out;
private BufferedReader in;
private Socket socket;
public static ArrayList<Double> Numbers = new ArrayList<Double>();
public static ArrayList<Client> allClients = new ArrayList<Client>();
public static double FinalClientSum;
public Client(Socket s) throws IOException {
socket = s;
in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
String line;
while((line = in.readLine()) != null) {
Numbers.add((Double.parseDouble(line)));
}
synchronized(this) {
allClients.add(this);
}
start();
}
public void run(){
try {
DataInputStream din = new DataInputStream(socket.getInputStream());
DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i = 0; i<Numbers.size();i++) {
FinalClientSum += Numbers.get(i); //calc sum
}
//send back the information to server.
dout.writeDouble(FinalClientSum);
}catch(Exception e) {
}finally {
try { socket.close(); }
catch(IOException e) {
System.out.println("Client thread " + ": Socket not closed!");
}
synchronized(this) {
allClients.remove(allClients.indexOf(this));
}
}
}
}
答案 0 :(得分:-1)
你的Socket socket = s.accept();
应该进入while循环。