【JavaSE】TCP通信程序练习(六个)

练习一

  • 客户端:发送数据,接受服务器反馈
  • 服务器:接收数据,给出反馈
//客户端
public class Client {
    public static void main(String[] args) throws IOException {
        Socket s = new Socket("192.168.0.103",10001);

        OutputStream os = s.getOutputStream();
        os.write("hello,tcp,我来了".getBytes());
        //以上代码,已经把数据发送过去了

        InputStream is = s.getInputStream();
        byte[] bys = new byte[1024];
        int len = is.read(bys);
        String data = new String(bys,0,len);
        System.out.println("客户端:"+data);

        s.close();

    }
}
//服务端
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10001);

        Socket s = ss.accept();

        InputStream is = s.getInputStream();
        byte[] bys = new byte[1024];
        int len = is.read(bys);
        String data = new String(bys,0,len);
        System.out.println("服务器:"+data);

        OutputStream os = s.getOutputStream();
        os.write("数据已经收到".getBytes());

        ss.close();

    }
}

练习二

  • 客户端:数据来自于键盘录入,知道输入的数据是886,发送数据结束
  • 服务端:接收到的数据在控制台输出
//客户端
public class Client {
    public static void main(String[] args) throws IOException {
        Socket s = new Socket("192.168.0.103",10001);

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        String line;
        while ((line=br.readLine())!=null) {
            if("886".equals(line)) {
                break;
            }
//            OutputStream os  = s.getOutputStream();
//            os.write(line.getBytes());
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        s.close();
    }
}
//服务端
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10001);
        Socket s  = ss.accept();
//        InputStream is = s.getInputStream();
//        InputStreamReader isr = new InputStreamReader(is);
//        BufferedReader br = new BufferedReader(isr);
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String line;
        while ((line = br.readLine())!= null) {
            System.out.println(line);
        }
        ss.close();
    }
}

练习三

  • 客户端:数据来自于键盘录入,知道输入的数据是886,发送数据结束(跟练习二一样)
  • 服务端:接收到的数据写入文本文件
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10001);

        Socket s = ss.accept();

        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\s.txt"));

        String  line;
        while((line= br.readLine())!=null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        ss.close();

    }
}

练习四

  • 客户端:数据来自于文本文件
  • 服务端:接收到的数据写入文本文件(练习三)
//客户端
public class Client {
    public static void main(String[] args) throws IOException {
            Socket s = new Socket("192.168.0.103",10001);

            //封装文本文件的数据
            BufferedReader br = new BufferedReader(new FileReader("D:\\s.txt"));
            //封装输出流写数据
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

            String line;
        while ((line=br.readLine())!=null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        br.close();
        s.close();
    }
}

练习五

  • 客户端:数据来自于文本文件,结束服务器反馈
  • 服务端:接收到的数据写入文本文件,给出反馈
//客户端
public class Client {
    public static void main(String[] args) throws IOException {
            Socket s = new Socket("192.168.0.103",10001);

            //封装文本文件的数据
            BufferedReader br = new BufferedReader(new FileReader("D:\\s.txt"));
            //封装输出流写数据
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

            String line;
        while ((line=br.readLine())!=null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        s.shutdownOutput();//发送数据结束标记
        //接受反馈
        BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String data = brClient.readLine();
        System.out.println("服务器的反馈:"+data);

        br.close();
        s.close();
    }
}
//服务端
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10001);

        Socket s = ss.accept();

        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\ss.txt"));

        String  line;
        while((line= br.readLine())!=null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        //给出反馈
        BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        bwServer.write("文件上传成功");
        bwServer.newLine();
        bwServer.flush();

        bw.close();
        ss.close();

    }
}

出现的问题:程序一直等待
原因:读数据的方法是阻塞式的
解决办法:自定义结束标记;使用shutdownOutput()方法(推荐)

练习六

  • 客户端:数据来自于文本文件,结束服务器反馈
  • 服务端:接收到的数据写入文本文件,给出反馈,代码用线程进行封装,为每一个客户端开启一个线程
//服务器端
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10001);
        while (true) {
            Socket s = ss.accept();
            new Thread(new ServerThread(s)).start();
        }
    }

    }
//服务器端的线程类
public class ServerThread implements Runnable {
    private Socket s;

    public ServerThread(Socket s) {
        this.s=s;

    }

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
           // BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\sss.txt"));
            //解决名称冲突问题
            int count = 0;
            File file = new File("D:\\Copy["+count+"].txt");
            while(file.exists())
            {
                count++;
                file = new File("D:\\Copy["+count+"].txt");
            }

            BufferedWriter bw = new BufferedWriter(new FileWriter(file));

            String line;
            while((line= br.readLine())!=null) {
                bw.write(line);
                bw.newLine();
                bw.flush();
            }

            BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bwServer.write("文件上传成功");
            bwServer.newLine();
            bwServer.flush();

            s.close();


        } catch (IOException e ){
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值