gpt4 book ai didi

java - 多线程,Java 服务器/客户端项目

转载 作者:行者123 更新时间:2023-12-02 06:01:37 25 4
gpt4 key购买 nike

嗨,我目前正在使用 Java 开发服务器/客户端程序,其中客户端连接到服务器并向服务器发送一个数字,然后服务器将此数字添加到全局变量并返回全局变量。我遇到的问题是,当我有多个客户端时,一个客户端无法工作,而另一个客户端可以工作,稍后创建的客户端可以工作。我为每个客户端创建了单独的线程,并且在一定程度上起作用。

这是创建新线程的服务器类的片段

public static Integer globalSum = 0;

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

ServerSocket server = new ServerSocket(8888);
Socket s;

System.out.println("Server running. Waiting for a client to connect...");

while(true) {

s = server.accept();
ThreadHandler th=new ThreadHandler(s);
Thread t=new Thread(th);
t.start();
System.out.println("Client is now connected");

}
}

这是我的客户类

public class Client2 {

public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost", 8888);
InputStream instream = s.getInputStream();
OutputStream outstream = s.getOutputStream();
Scanner in = new Scanner(instream);
Scanner input= new Scanner(System.in);
PrintWriter out = new PrintWriter(outstream);

for(int i=0;i<5;i++){
System.out.println("Please enter a value "+(i+1));
String response=input.nextLine();
String request="SUBMIT"+" "+response+"\n";
out.print(request);
out.flush();
System.out.println(in.nextLine());
}

out.print("QUIT\n");
out.flush();

s.close();
}

}

这是我的 ThreadHandler 类

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

public class ThreadHandler implements Runnable {

private Socket s;
private Scanner in;
private static PrintWriter out;
ServerMultipleClients serverInstance;

public ThreadHandler(Socket s)
{
//this.serverInstance=serverInstance;
this.s=s;

}

public void run() {
try {
try {

in = new Scanner(s.getInputStream());
out = new PrintWriter(s.getOutputStream());

doService(); // the actual service

} finally {
s.close();
}

} catch (IOException e) {
System.err.println(e);
}
return;
}


public void doService() throws IOException{

String request;

while(true){

request=in.next();
String arrayString[] = request.split("\\s+") ;
if(request.equals("SUBMIT")){
String value2=in.next();
handle(value2);
}
else if (request.equals("QUIT")){
break;
}
else if(!request.equals("SUBMIT")||!request.equals("QUIT")){
System.out.println("ERROR in input");
break;
}

}

}


public void handle(String value1){

if(isInt(value1)==true){

int valueInt=Integer.parseInt(value1);

synchronized(serverInstance.globalSum) {

if((valueInt+serverInstance.globalSum)<Integer.MAX_VALUE){
ServerMultipleClients.globalSum=ServerMultipleClients.globalSum+valueInt;
out.println("OK");
out.flush();
System.out.println("New Value for Global Sum is: "+ServerMultipleClients.globalSum);
}
else if((valueInt+ServerMultipleClients.globalSum)>=Integer.MAX_VALUE){
out.println("ERROR");
System.out.println("Global Sum is unchanged, its value is: "+ServerMultipleClients.globalSum);
}
else{
out.println("ERROR");
System.out.println("Global Sum is unchanged, its value is: "+ServerMultipleClients.globalSum);
}

}
}
}

public boolean isInt( String input ) {
try {
Integer.parseInt( input );
return true;
}
catch( Exception e ) {
return false;
}
}

}

问题是,当我运行它时,它适用于在稍后阶段运行的每个客户端到前一个客户端,返回到前一个客户端意味着该客户端崩溃。

最佳答案

你确定这件事吗:

private static PrintWriter out;

必须是静态的吗?这意味着所有线程共享您的输出流!

关于java - 多线程,Java 服务器/客户端项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22618624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com