java服务器(简单示例)仅显示发送的第一个字符串
您好,我想通过在一个非常简单的 java 服务器上测试我的客户端应用程序是否正常工作。我的客户几乎每秒都会发送一个字符串。我的想法是,服务器一旦收到该字符串,就会在 eclipse 控制台中显示该字符串。但问题是简单的java服务器只打印出第一个字符串。下面是我从 site 获得的简单 java 服务器
package serverjava;
/* ChatServer.java */
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class serverjava {
private static int port = 1234; /* port the server listens on */
public static void main (String[] args) throws IOException {
ServerSocket server = null;
try {
server = new ServerSocket(port); /* start listening on the port */
} catch (IOException e) {
System.err.println("Could not listen on port: " + port);
System.err.println(e);
System.exit(1);
}
Socket client = null;
try {
client = server.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.err.println(e);
System.exit(1);
}
/* obtain an input stream to the client */
BufferedReader in = new BufferedReader(new InputStreamReader(
client.getInputStream()));
String msg;
/* loop reading lines from the client and display them */
while ((msg = in.readLine()) != null) {
System.out.println("Client says: " + msg);
}
}
}
java 服务器是否能够每次启动仅显示字符串的一个实例?如果是,我怎样才能让它在控制台中打印或显示每秒发送的所有字符串?
Hi i want to test if my client app is working by testing it on a very simple java server. My client will send a string almost every second. My idea is that the server will display the string in the eclipse console once it receives the string. But the problem is that the simple java server only prints out the first string. Below is the simple java server i got from the site
package serverjava;
/* ChatServer.java */
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class serverjava {
private static int port = 1234; /* port the server listens on */
public static void main (String[] args) throws IOException {
ServerSocket server = null;
try {
server = new ServerSocket(port); /* start listening on the port */
} catch (IOException e) {
System.err.println("Could not listen on port: " + port);
System.err.println(e);
System.exit(1);
}
Socket client = null;
try {
client = server.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.err.println(e);
System.exit(1);
}
/* obtain an input stream to the client */
BufferedReader in = new BufferedReader(new InputStreamReader(
client.getInputStream()));
String msg;
/* loop reading lines from the client and display them */
while ((msg = in.readLine()) != null) {
System.out.println("Client says: " + msg);
}
}
}
Is the java server capable of displaying only an instance of a string per launch? If yes, how can I make it to print or show in the console all the strings that are being sent every second?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到的问题是您只读取缓冲区一次。服务器等待客户端行,当它读取它们时,它会完成执行,因为“msg”为空。您必须有一个每秒、每分钟或每小时读取一次的进程......这意味着使用线程。
http://www.isr.umd.edu/~austin/ence489c .d/threads.html。
http://www.javaworld.com/javaworld/jw -04-1996/jw-04-threads.html
小心!不要使用类似的 while(true) 结构,因为机器将超载。
The problem you have is that you read the buffer only one time. The server waits for client lines and when it reads them, then it finish the execution because 'msg' is null. You must have a process that reads every second or minute, or hour... That means using THREADS.
http://www.isr.umd.edu/~austin/ence489c.d/threads.html.
http://www.javaworld.com/javaworld/jw-04-1996/jw-04-threads.html
Be careful! Don't use a while(true) structure of something like that, because the machine will be overloaded.