首页 > 解决方案 > 如何使用 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)

标签: javasockets

解决方案


推荐阅读