gpt4 book ai didi

java - 客户端/服务器只读取一条消息?

转载 作者:行者123 更新时间:2023-12-01 13:01:09 26 4
gpt4 key购买 nike

我的客户端/服务器对于一条消息完美工作,然后无论接下来是什么,它都说它是空白的。

我相信问题可以在这里或我的命令类中解决:

package MyServer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class Main {
public static String line;
public static void main(String[] args){
while(true){
try {
//Creates a socket to receive commands from!
Socket socket = new Socket("localhost", 7586);

//Uses that socket to create a Reader to read the commands!
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

//Waits for Lines to be sent and then executes them!
while(true){
line = in.readLine();

if(line != null){
Commands.ReceiveCommand();
}else {
break;
}
}
} catch (UnknownHostException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}


}

或者在我的命令中:

package MyServer;

import javax.swing.JOptionPane;

public class Commands {

static String command = (Main.line).toString(); //<--This was the problem, just had to move it into the method below.
public static void ReceiveCommand(){
if(command.equals("test")){
JOptionPane.showMessageDialog(null,"works","command: " + command,JOptionPane.WARNING_MESSAGE);
//System.out.println("WORKEDS MOFO");
command = "";
}else{
JOptionPane.showMessageDialog(null,"not recognized","command: " + command,JOptionPane.WARNING_MESSAGE);
//System.out.println("no bueno");
//System.out.println("line is " + command);
command = "";

}

}

}

编辑:由于某种原因,在调试时,命令在使用一次后无论如何都是空白,所以它可能在我的主服务器类中:

package MyClient;

import java.util.List;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class Main {
//Sets the Port
final static int PORT = 7591;

//Creates a list of Connected Clients
static List<Socket> connectedClients = new ArrayList<Socket>();

public static void main(String[] args){
//Creates a Thread to Send Messages to connectedClients
new Thread(new messageThread()).start();

try {
//Creates the ServerSocket
ServerSocket serverSocket = new ServerSocket(PORT);

while(true){
//Waits for a Connection and Accepts it...
Socket clientSocket = serverSocket.accept();
System.out.println("A Client has connected!");
//Adds it to the List
connectedClients.add(clientSocket);
}



} catch (IOException e) {
e.printStackTrace();
}
} }

和消息线程:

package MyClient;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class messageThread implements Runnable {
public void run() {
while(true){
System.out.println(">>");
Scanner in = new Scanner(System.in);
String command = in.nextLine();

for(Socket clientToSendCommand : Main.connectedClients){
try {
PrintWriter commandWriter = new PrintWriter(clientToSendCommand.getOutputStream());
commandWriter.println(command);
commandWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}

}
}
}
}

最佳答案

这不起作用,因为该行

static String command = (Main.line).toString();
当首次引用 Commands 类时,Commands 类中的

仅执行一次。

当发送第二个命令时,该类已经被引用,因此该行不会再次执行。

要解决这个问题,请将该行放在方法中,或者更好 - 将其作为参数传递给方法。

<小时/>

P.S.:你是否混淆了包裹?具有 ServerSocket 的类应该是服务器,因此位于 MyServer 包中。 :-)

关于java - 客户端/服务器只读取一条消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23508163/

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