【问题标题】:How to save data recieved from tcp/ip server into textfile in java如何将从 tcp/ip 服务器接收到的数据保存到 java 中的文本文件中
【发布时间】:2017-02-20 20:51:27
【问题描述】:

我在 Java 的 Socket Network 中编写了一个小程序,它使用 IP 地址和端口号与服务器通信。现在将连续接收来自服务器的数据。我希望从服务器接收到的所有这些数据都存储在一个文本文件中,但不知道如何在不挂起应用程序的情况下做到这一点。

这是我的代码:

public class Client
{

    public Client()
    {
        try
        {
            //ceating the socket to connect tso server running on same machine binded on port no 3000
            Socket client=new Socket("localhost",3000);
            System.out.println("Client connected ");
            //getting the o/p stream of that connection
            PrintStream out=new PrintStream(client.getOutputStream());
            //sending the message to server
            out.print("Hello from client\n");
            out.flush();
            //reading the response using input stream
            BufferedReader in= new BufferedReader(new InputStreamReader(client.getInputStream()));
            System.out.println(in.readLine());
            //closing the streams
            in.close();
            out.close();

        }
        catch(Exception err)
        {
            System.err.println("* err"+err);
        }

    }

    public static void main(String a[])
    {
        new Client();
    }
}   

提前致谢。

【问题讨论】:

    标签: java sockets networking tcpclient


    【解决方案1】:

    我有一个经过测试的代码。试试这个。

      StringBuffer instr = new StringBuffer();
    
      /** Establish a socket connetion with the server*/
      Socket connection = new Socket(address, port);
    
      /** Instantiate a BufferedOutputStream object */
      BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());
    
      /** Instantiate an OutputStreamWriter object with the optional character
       * encoding. Sending some message to server
       */
      OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII"); 
      String process = "SAMPLE COMMAND SENT TO SERVER"+(char)13;
    
      /** Write across the socket connection and flush the buffer */
      osw.write(process);
      osw.flush();
    
      // NOW READING THE RESPONSE FROM THE SERVER
      // I HAVE ADDED THE CHAR(13) as the delimiter here
      /** Instantiate a BufferedInputStream object for reading
      /** Instantiate a BufferedInputStream object for reading
       * incoming socket streams.
       */
    
      BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
      /**Instantiate an InputStreamReader with the optional
       * character encoding.
       */
    
      InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");
    
      /**Read the socket's InputStream and append to a StringBuffer */
      int c;
      while((c=isr.read())!=13)
        instr.append((char)c);
    
      /** Close the socket connection. */
      connection.close();
    

    【讨论】:

    • 谢谢您的回复,因为我是套接字编程的新手,请您指导我如何将实时字符串缓冲区数据保存到文本文件中
    • 这很简单,从这个网站学习文件写入:mkyong.com/java/…
    猜你喜欢
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多