该程序被认为是相对简单的。目标是拥有一台服务器,该服务器会定期向与其连接的所有客户端发送一个rgb值(255,255,255)。在下面的代码中,我将其简化为一个使用了3次的随机值。客户端可以很好地连接到服务器,并且能够接收数据。但是,数据输出不正确。我可以清楚地看到一个模式,并且我怀疑它可能与一次发送多少字节有关。但是,我认为如果有换行符,则缓冲的读取器将仅处理一行中的读取。有人可以向我解释为什么以这种方式读取数字,或者可以指出我要完成我想要的正确方法吗?
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);
}
}
}//=============================================================================================================
}
}
答案 0 :(得分:0)
一旦在+
表达式中引入了字符串,对于表达式的其余部分,您仅在进行字符串连接。
第一个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);