gpt4 book ai didi

Java客户端从服务器读取错误的整数

转载 作者:行者123 更新时间:2023-12-01 18:42:04 24 4
gpt4 key购买 nike

该程序应该相对简单。目标是让服务器定期向所有连接到它的客户端发送 RGB 值 (255,255,255)。在下面的代码中,我将其简化为使用三次的一个随机值。客户端与服务器的连接正常并且能够接收数据。但是,数据显示不正确。我可以清楚地看到一种模式,我怀疑它可能与一次发送的字节数有关。但是,我认为缓冲读取器只会在有新行字符的情况下处理行中的读取。有人可以向我解释为什么以这种方式读取数字,并可能指出我实现我想要的目标的正确方法吗?

输出示例:

10 91 9

38 371 37

33 321 32

235 2341 234

201 2001 200

65 641 64

225 2241 224

68 671 67

116 1151 115

6 51 5

服务器

public class Server {

HashMap<Integer, ClientThread> clients = new HashMap<>(); // A list of clients so that they can be referenced later.

private ServerSocket server = null; // The socket for the server
boolean running = false; // If the server is running at all
int responseDelta = 0; // The time between updates. Will help calculate when the server should write
// out all of its messages;
int clientConnectionCount = 0; // The number of clients Connected
int updateCounter = 1; // This is just for testing purposes

Queue<String> incomingMessages = new LinkedBlockingQueue<>(100);
Queue<String> outgoingMessages = new LinkedBlockingQueue<>(100);

public Server(int port) throws IOException {
server = new ServerSocket(port);
}// =================================================================================================================

public void writeMessage(String message) {
System.out.println(message);
}

public void readMessages() {
while (!incomingMessages.isEmpty()) {
System.out.println(incomingMessages.poll());
}
}// =================================================================================================================

public void sendMessages() {
while (!outgoingMessages.isEmpty()) {
String message = outgoingMessages.poll();
for (int i = 0; i < clients.size(); i++) {
clients.get(i + 1).sendMessage(message);
;
}
}
}// =================================================================================================================

// Starts the Server
public void startServer() {
System.out.println("Server was started");
running = true;
ConnectionThread connectionThread = new ConnectionThread();
connectionThread.start();
ServerThread serverThread = new ServerThread();
serverThread.start();
}

/**
* Reads and writes to a client connected through a socket
*
* @author Sam
*
*/
private class ClientThread extends Thread {
private Socket socket = null;
private InputStream clientIn;
private PrintWriter clientOut;
BufferedReader bufferedReader;
boolean connected;

private ClientThread(Socket socket) throws IOException {
this.socket = socket;
connected = true;
clientIn = socket.getInputStream();
clientOut = new PrintWriter(socket.getOutputStream());
bufferedReader = new BufferedReader(new InputStreamReader(clientIn));
}// =============================================================================================================

public void sendMessage(String message) {
clientOut.write((message + "\n"));
clientOut.flush();
}

@Override
public void run() {
try {
while (connected && running) {
clientIn = socket.getInputStream();
String line = null;
line = bufferedReader.readLine();
if (line != null) {
incomingMessages.add(line);
}
}
socket.close(); // close the socket because we should be done.
} catch (IOException e) {
System.out.println("Turminating socket");
connected = false;
e.printStackTrace();
}
}
}

//Creates a thread that will listen to incoming connections
private class ConnectionThread extends Thread {
@Override
public void run() {
while (running) {
Socket socket = null;
try {
socket = server.accept(); // A client has just connected
ClientThread clientThread = new ClientThread(socket); // Create a new thread to read and write to
// the socket. If we didn't it would just
// get dropped after scope.
clientThread.start();
clients.put(++clientConnectionCount, clientThread); // Put it in the hash map so that it can be
// referenced later.
System.out.println("A new client connected");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

// Creates a thread that will write and read
private class ServerThread extends Thread {
@Override
public void run() {
Random rnd = new Random();
while (running) {
readMessages();
int color = rnd.nextInt(254)+1;
outgoingMessages.add(color + 1 + "\t" + color + 1 + "\t" + color);
sendMessages();
}
}// =============================================================================================================
}

}// #####################################################################################################################

客户端

public class Client {


boolean running = true;
int port;
String address;
OutputStream clientOut;
BufferedReader clientIn;




public void connect(String address, int port) throws IOException {
ServerConnectionThread sct = new ServerConnectionThread(new Socket("127.0.0.1", port));
sct.start();

}

public void writeToServer(String data) {
try {
clientOut.write((data +"\n").getBytes());
clientOut.flush();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}



private class ServerConnectionThread extends Thread{

Socket connection;

public ServerConnectionThread(Socket socket) throws UnknownHostException, IOException {
connection = socket;
clientIn = new BufferedReader(new InputStreamReader(connection.getInputStream()));
clientOut = connection.getOutputStream();
}

@Override
public void run() {
while(running){
String line = null;
try {
line = clientIn.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if( line != null){
System.out.println(line);
}
}

}//=============================================================================================================
}
}

最佳答案

将字符串引入 + 表达式后,您只需对表达式的其余部分进行字符串连接。

第一个 color + 1 被解释为数学加法,因为此时表达式中没有遇到任何字符串。

这个:

outgoingMessages.add(color + 1 + "\t" + color + 1 + "\t" + color);

实际上与此相同:

outgoingMessages.add(
new StringBuilder(String.valueOf(color + 1)) // mathematical addition
.append("\t")
.append(String.valueOf(color))
.append(String.valueOf(1))
.append("\t")
.append(String.valueOf(color))
.toString());

解决方案是隔离数学表达式,因此它会被解释为数学:

outgoingMessages.add(color + 1 + "\t" + (color + 1) + "\t" + color);

为了清楚起见,我个人会将两个数学表达式括起来:

outgoingMessages.add((color + 1) + "\t" + (color + 1) + "\t" + color);

当然,color 是一个变量,因此您要打印三个数字,它们都基于单个值。变量不以任何方式“附加”到它可能添加的任何先前的赋值;仅在程序中遇到赋值时才设置变量。

如果你想要三个随机数,你必须进行三个随机数运算和三个赋值:

int color1 = rnd.nextInt(254) + 1;
int color2 = rnd.nextInt(254) + 1;
int color3 = rnd.nextInt(254) + 1;
outgoingMessages.add((color1 + 1) + "\t" + (color2 + 1) + "\t" + color3);

关于Java客户端从服务器读取错误的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59910248/

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