gpt4 book ai didi

java - JAVA服务器/客户端程序的解决方案

转载 作者:太空宇宙 更新时间:2023-11-04 08:22:56 25 4
gpt4 key购买 nike

我的代码有问题。我用Java制作了一个等待连接的服务器。当客户端连接时,如果我们(服务器)发送命令“showinfo”,客户端会将其IP地址发送回服务器。

问题在于它是一个多线程服务器并且有许多客户端连接。因此,例如,如果有 2 个客户端连接到服务器,我们必须输入 2 次 showinfo,然后客户端才会向我们提供信息。如何让客户立即响应,为每个客户输入“showinfo”,而不是等到我为所有客户输入 showinfo。如果有 50 个客户端,那么在执行命令之前输入 50 次 showinfo 会有点累。提前谢谢您(我已经尝试了一些线程同步和一些 sleep() join() 命令,但我还没有得出结论,我希望你能帮忙一点。)

这是服务器代码

import java.io.*;
import java.net.*;
import java.security.*;

public class Server implements Runnable{
private Socket server;
private static String command,info,message;
BufferedReader infromclient =null;
InputStream infromclient2=null;
DataOutputStream outtoclient =null;
static BufferedReader infrommaster=null;
private static int port=6789;

Server( Socket server ){
try{
this.server=server;
infromclient =new BufferedReader(new InputStreamReader(server.getInputStream()));
outtoclient =new DataOutputStream(server.getOutputStream());
infrommaster=new BufferedReader(new InputStreamReader(System.in));
}catch( IOException e ){
System.out.println("Exception accessing socket streams: "+e);
}
}

// main()
// Listen for incoming connections and handle them
public static void main(String[] args) {
ThreadGroup clients = new ThreadGroup("clients");
System.out.print("Waiting for connections on port " + port + "...");
System.out.println("\nIn total there are:"+clients.activeCount()+" clients\n");
try{
ServerSocket server = new ServerSocket(port);
Socket nextsocket;

// waiting for connections
while(true){
nextsocket = server.accept();
Thread client= new Thread(clients,new Server(nextsocket),"client"+(clients.activeCount()+1));
System.out.println("Client connected");
System.out.println("There are "+clients.activeCount()+" clients connected");
client.start();
}

}catch (IOException ioe){
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}

public void run (){
try{
command=infrommaster.readLine();
outtoclient.writeBytes(command+"\n");

// showinfo
if(command.equals("showinfo")){
info=infromclient.readLine();
System.out.println("\nClient information\n\n" +info+"\n");
}

server.close();
}catch (IOException ioe){
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}
}

这是客户端的代码:

import java.io.*;
import java.net.*;
import java.lang.*;


public class Client{

public static void main(String args[]) throws Exception{
String command2;
String info;
Socket clientSocket=null;
Socket scket=null;
BufferedReader inFromUser=null;
DataOutputStream outToServer=null;
BufferedReader inFromServer=null;
BufferedWriter tosite=null;

try{
clientSocket = new Socket(InetAddress.getLocalHost().getHostName(), 6789);
inFromUser =new BufferedReader(new InputStreamReader(System.in));
outToServer =new DataOutputStream(clientSocket.getOutputStream());
inFromServer =new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}catch(UnknownHostException | IOException e ){
System.out.println("Problem "+e);
}

command2=inFromServer.readLine();
System.out.println("From Server:" +command2);

// showinfo
if (command2.equals("showinfo")){
try{
InetAddress myip=InetAddress.getLocalHost();
info =("IP: " +myip.getHostAddress()+"\n");

System.out.println("\nSome system information\n" + info);
outToServer.writeBytes(info);
}catch (Exception e){
System.out.println("Exception caught ="+e.getMessage());
}
}

outToServer.flush();
outToServer.close();
inFromUser.close();
inFromServer.close();
clientSocket.close();
}
}

** 命令 clientSocket = new Socket(InetAddress.getLocalHost().getHostName(), 6789); 意味着它测试我的电脑(端口 6789 中的我的 IP)。

最佳答案

如果一个线程在读取行上阻塞,另一个线程最终应该获得控制权。这就是多线程的全部意义。我想我知道你的问题。将读取输入命令的代码移动到服务器的 run() 方法中。那么你应该很好。我认为问题在于您的线程在轮询输入后启动。

将这些行从服务器构造函数移至服务器的 run() 方法:

 infromclient =new BufferedReader(new InputStreamReader(server.getInputStream()));
outtoclient =new DataOutputStream(server.getOutputStream());
infrommaster=new BufferedReader(new InputStreamReader(System.in));

关于java - JAVA服务器/客户端程序的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9184474/

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