java - 如何使用 readline() 获取和存储每 2 秒生成的字符串
问题描述
如何获取和存储每 2 秒生成的字符串readline()
?套接字编程。
#接收者
try {
while (true) {
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
//payload = br.readLine();
// payload = String.parseString(br.read(char getdata[],0, 65535));//valueOf(char)
//br.reset();
int payload_length = payload.length();
System.out.println("Message sent from client: " + payload + ":: Length ::" + payload_length);
if (payload_length == stringLength) {
// for(int k=0;k>=s;k++){
System.out.println("String length matching");
String Query1 = "INSERT INTO RpiBuffer (string) values(?)"; //,(payload);
pst1 = conn.prepareStatement(Query1);
// pst1.setInt(1, id);
pst1.setString(1, payload);
//pst1.setInt(3,isSend);
pst1.executeUpdate();
System.out.println("Query 1 Executed::" + pst1);
// }
} else {
System.out.println("String length not matching");
}
/* ---------------------------------------------------------------------*/
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
// bw.write(returnedMessage);
bw.newLine();
// System.out.println("Message replied to client: "+returnedMessage);
bw.flush();
check_internet();
}
} catch (IOException e) {
System.out.println("Exception in receive_data())::" + e);
}
#发件人
import java.io.*;
import java.net.*;
import java.util.concurrent.TimeUnit;
public class EchoClient {
public static void main(String[] args) throws InterruptedException {
String hostName = "127.0.0.1";
int portNumber = 3002;
int StringLength = 22;
int i=0;
while(true){ //for(i=0;i<99;i++){
try {
Socket echoSocket = new Socket(hostName, portNumber);
// PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader( new InputStreamReader(echoSocket.getInputStream()));
BufferedWriter out= new BufferedWriter( new OutputStreamWriter(echoSocket.getOutputStream()));
String s1="1231231231231231231234",s2="0001110001110001110001",s3="0101010101",s4="1111000011110000111100";
out.write(s1);System.out.println("send data1 successfully????? ");TimeUnit.SECONDS.sleep(2);
out.write(s2);System.out.println("send data2 successfully????? ");TimeUnit.SECONDS.sleep(2);
out.write(s3);System.out.println("send data3 successfully????? ");TimeUnit.SECONDS.sleep(2);
out.write(s4);System.out.println("send data4 successfully????? ");TimeUnit.SECONDS.sleep(2);
/* String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}*/
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
// System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
//System.exit(1);
}
}
}
}
跑:
initializing program.....rpibuffer.RpiBuffer@7852e922
Server start at port 3002.
BUILD STOPPED (total time: 46 seconds)
解决方案
推荐阅读
- c# - UseHsts 不适用于 NetCore 2.1 网站
- node.js - 为什么我可以在 MongoDB 中更新一个字段而不能更新另一个字段?(平均堆栈)
- c# - 将模式窗口中的内容保存为 SQL Server 表中的 varbinary
- tensorflow - cudnnLSTM 不会恢复为 cudnnCompatibleLSTM
- javascript - 遍历复杂的 JSON 以查找匹配并返回关联的数据
- swift - 区分记录更改和 Firebase 中删除的数据
- kubernetes - Kubernetes 的 ConfigMap 是可写的吗?
- c# - routes.MapRoute 语法
- c++ - 如何滚动自定义 QGraphicsWidgets 的所有内容?
- pandas - 使用 pandas 过滤功能后返回一个列表