客户端-服务器聊天中的客户端 CPU 负载 30-50%
Client in Client-Server Chat CPU load 30-50%
客户端连接到服务器,服务器向客户端发送1条消息,客户端必须等待另一条消息。当客户端在 while(true) 循环中等待时,它将 CPU 加载到 50%。我尝试尽可能简单地做到这一点,以了解它是如何工作的。
P.S。所有 catch() 都已隐藏,以最小化此处的代码。
客户:
public class SocketClient
{
String host;
int port;
static Socket connection;
BufferedReader bfr;
public SocketClient(String host, int port)
{
this.port = port;
this.host = host;
}
public void connect() throws UnknownHostException, IOException, Exception
{
connection = new Socket(new String(host), port);
System.out.println("Client is ready.");
bfr = new BufferedReader(new InputStreamReader(connection.getInputStream()));
}
public void readInput() throws IOException
{
String input;
if(input = bfr.readLine()) != null)
{
System.out.println(input);
}
}
public static void main(String[] args) throws UnknownHostException, IOException, Exception
{
SocketClient socketClient = new SocketClient("localhost", 19999);
try {
socketClient.connect();
try
{
while(true)
{
socketClient.readInput();
}
}
}
}
}
服务器:
public class MultipleSocketServer implements Runnable {
private Socket connection;
private String TimeStamp;
private int ID;
static PrintWriter writer;
private static File file;
public static void main(String[] args)
throws FileNotFoundException, UnsupportedEncodingException
{
int port = 19999;
int count = 0;
file = new File("E:/test.txt");
writer = new PrintWriter(file, "UTF-8");
try
{
ServerSocket socket = new ServerSocket(port);
System.out.println("MultipleSocketServer Initialized");
while (true)
{
Socket connection = socket.accept();
Runnable runnable = new MultipleSocketServer(connection, ++count);
Thread thread = new Thread(runnable);
thread.start();
}
}
}
MultipleSocketServer(Socket s, int i) {
this.connection = s;
this.ID = i;
}
public void run() {
try {
System.out.println("Connected: " + connection.getLocalSocketAddress() + " at port " + connection.getPort());
writer.println(ID + ": " + connection.getLocalSocketAddress() + " at port " + connection.getPort());
writer.flush();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
writer.write("MultipleSocketServer repsonded at " + new java.util.Date().toString());
writer.write("\n");
writer.flush();
}
finally {
try {
connection.close();
}
}
}
}
分析
好像服务器端发送数据后关闭了连接。
客户端有以下死循环:
while (true)
{
socketClient.readInput();
}
循环会导致CPU消耗:方法bfr.readLine()
方法调用会returnnull
立即连接已关闭。
解决方案
请考虑将客户端的循环改为读到"the end-of-connection":
String input;
while ((input = bfr.readLine()) != null) {
System.out.println(input);
}
客户端连接到服务器,服务器向客户端发送1条消息,客户端必须等待另一条消息。当客户端在 while(true) 循环中等待时,它将 CPU 加载到 50%。我尝试尽可能简单地做到这一点,以了解它是如何工作的。
P.S。所有 catch() 都已隐藏,以最小化此处的代码。
客户:
public class SocketClient
{
String host;
int port;
static Socket connection;
BufferedReader bfr;
public SocketClient(String host, int port)
{
this.port = port;
this.host = host;
}
public void connect() throws UnknownHostException, IOException, Exception
{
connection = new Socket(new String(host), port);
System.out.println("Client is ready.");
bfr = new BufferedReader(new InputStreamReader(connection.getInputStream()));
}
public void readInput() throws IOException
{
String input;
if(input = bfr.readLine()) != null)
{
System.out.println(input);
}
}
public static void main(String[] args) throws UnknownHostException, IOException, Exception
{
SocketClient socketClient = new SocketClient("localhost", 19999);
try {
socketClient.connect();
try
{
while(true)
{
socketClient.readInput();
}
}
}
}
}
服务器:
public class MultipleSocketServer implements Runnable {
private Socket connection;
private String TimeStamp;
private int ID;
static PrintWriter writer;
private static File file;
public static void main(String[] args)
throws FileNotFoundException, UnsupportedEncodingException
{
int port = 19999;
int count = 0;
file = new File("E:/test.txt");
writer = new PrintWriter(file, "UTF-8");
try
{
ServerSocket socket = new ServerSocket(port);
System.out.println("MultipleSocketServer Initialized");
while (true)
{
Socket connection = socket.accept();
Runnable runnable = new MultipleSocketServer(connection, ++count);
Thread thread = new Thread(runnable);
thread.start();
}
}
}
MultipleSocketServer(Socket s, int i) {
this.connection = s;
this.ID = i;
}
public void run() {
try {
System.out.println("Connected: " + connection.getLocalSocketAddress() + " at port " + connection.getPort());
writer.println(ID + ": " + connection.getLocalSocketAddress() + " at port " + connection.getPort());
writer.flush();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
writer.write("MultipleSocketServer repsonded at " + new java.util.Date().toString());
writer.write("\n");
writer.flush();
}
finally {
try {
connection.close();
}
}
}
}
分析
好像服务器端发送数据后关闭了连接。
客户端有以下死循环:
while (true)
{
socketClient.readInput();
}
循环会导致CPU消耗:方法bfr.readLine()
方法调用会returnnull
立即连接已关闭。
解决方案
请考虑将客户端的循环改为读到"the end-of-connection":
String input;
while ((input = bfr.readLine()) != null) {
System.out.println(input);
}