本文共 1355 字,大约阅读时间需要 4 分钟。
我在将来自客户端的传入信息写入此程序时遇到问题。数据进入并且System.out每秒都有输出,但FileWriter只打印程序启动时的第一行输出。我手动停止程序,然后检查文件。我不确定什么是错的,请帮忙。
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.net.*;
import java.io.*;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Server {
public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
String msg_received;
FileWriter fw = new FileWriter("HeartData.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw);
System.out.println("Waiting for Android client to connect...");
while (true)
{
try
{
ServerSocket server = new ServerSocket(2323);
Socket s = server.accept();
server.close();
InetAddress clientAddress = s.getInetAddress();
System.out.println("Incoming connection from: " + clientAddress.getHostName() + "[" + clientAddress.getHostAddress() + "]");
DataInputStream DIS = new DataInputStream(s.getInputStream());
msg_received = DIS.readUTF();
out.println(msg_received + "," + LocalTime.now() + "," + LocalDate.now());
System.out.printf("Android says: %sat %s%n", msg_received, LocalTime.now());
}
catch (IOException e){e.printStackTrace();}
finally {
out.close();
}
}
}
}
示例输出:
等待Android客户端连接...
来自:hostname.domain [ipaddress]的传入连接
Android说:SOMETHING
在10:51:06.013
的
转载地址:https://blog.csdn.net/weixin_33826897/article/details/114191920 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!