码迷,mamicode.com
首页 > 其他好文 > 详细

【TCP传输数据-键盘录入】

时间:2019-07-31 13:32:41      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:bre   close   comm   bsp   键盘录入   inpu   author   output   serve   

package com.yjf.esupplier.common.test;

import java.io.*;
import java.net.Socket;

/**
 * @author shusheng
 * @description TCP 传输数据:键盘录入
 * @Email shusheng@yiji.com
 * @date 2019/1/15 22:57
 */
public class ClientDemo1 {

    public static void main(String[] args) throws IOException {
        // 创建发送端的Socket对象
        Socket socket = new Socket("localhost", 8888);

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        String line = null;
        while ((line = br.readLine()) != null) {
            // 键盘录入数据要自定义结束标记
            if ("886".equals(line)) {
                break;
            }
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        socket.close();
    }

}
package com.yjf.esupplier.common.test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author shusheng
 * @description TCP 传输数据:键盘录入
 * @Email shusheng@yiji.com
 * @date 2019/1/16 21:11
 */
public class ServerDemo1 {

    public static void main(String[] args) throws IOException {
        // 创建接收端的 Socket 对象
        ServerSocket ss = new ServerSocket(8888);

        Socket s = ss.accept();

        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String line = null;
        while((line=br.readLine())!=null){
            System.out.println(line);
        }

        s.close();
    }

}

 

【TCP传输数据-键盘录入】

标签:bre   close   comm   bsp   键盘录入   inpu   author   output   serve   

原文地址:https://www.cnblogs.com/zuixinxian/p/11275398.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!