Java学习笔记(18):网络编程

一:TCP通信程序练习1
客户端:

public class 客户端 {
    public static void main(String[] args) throws IOException {
        //
        Socket s = new Socket("192.168.3.12",12345);
        //

        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;
            }
           /* byte[] bys =line.getBytes();
            OutputStream os = s.getOutputStream();
            os.write(bys);
            可以这样写,但是比较麻烦
            */
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        s.close();
    }
}

服务器端:

public class 服务器端 {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(12345);

        Socket s = ss.accept();

//        while(true){
//           可以这样写,但没必要
//           byte[] bys = new byte[1024];
//           String line = new String(bys,0,is.read(bys));
//           System.out.println("发送来了:" + line);
//    }
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String line;
            while((line=br.readLine())!=null) {
                System.out.println("发送来了:" + line);
            }
            ss.close();
    }
}

二:TCP通信程序练习2
将键盘读取的数据存入文本文件
客户端:

public class 客户端 {
    public static void main(String[] args) throws IOException {
        //
        Socket s = new Socket("192.168.3.12",12345);
        //

        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;
            }
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        //接收文本文件的数据
        BufferedReader bfr = new BufferedReader(new FileReader("TEXT\\TCP存储.txt"));
        String data;
        while((data=bfr.readLine())!=null){
            System.out.println("客户端接收"+data);
        }

        s.close();
    }
}

服务器端:

public class 服务器端 {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(12345);

        //监听
        Socket s = ss.accept();

        //接收数据
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));

        //将数据写入文本文件
        BufferedWriter bw = new BufferedWriter(new FileWriter("TEXT\\TCP存储.txt"));
        String line;
        while((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        ss.close();
        bw.close();
    }
}

三:TCP通信程序练习3
上传文本文件并复制
客户端:

public class 客户端 {
    public static void main(String[] args) throws IOException {
        Socket s = new Socket("192.168.3.12",12345);

        BufferedReader br = new BufferedReader(new FileReader("TEXT\\TCP.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()));
        System.out.println("服务器的反馈:"+brClient.readLine());

        s.close();
        br.close();
    }
}

服务器端:


public class 服务器端 {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(12345);

        Socket s = ss.accept();
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new FileWriter("TEXT\\TCPcopy.java"));

        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();

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

四:TCP通信程序练习4
多线程上传文本文件
客户端:

public class 客户端 {
    public static void main(String[] args) throws IOException {
        Socket s = new Socket("192.168.3.12",12345);

        BufferedReader br = new BufferedReader(new FileReader("TEXT\\TCP.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()));
        System.out.println("服务器的反馈:"+brClient.readLine());

        s.close();
        br.close();
    }
}

服务器端

public class 服务器端 {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(12345);

        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()));
            int count = 0;
            File file = new File("TEXT\\Copy["+count+"].txt");
            while(file.exists())
            {
                count++;
                file = new File("TEXT\\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 servebw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            servebw.write("文件上传成功");
            servebw.newLine();
            servebw.flush();

            s.close();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

上一篇:java csv文件写入


下一篇:Java知识点汇总--网络编程