网络编程(TCP)

网络编程(TCP)

客户端:Socket
步骤:
创建一个流套接字并将其连接到指定主机上的指定端口号
Socket(InetAddress,int port);
获取输出流
getOutputStream();
写数据
write();
关闭资源
代码:
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(8887);

        Socket socket = ss.accept();

        //获取输入流
        InputStream in = socket.getInputStream();
        //包装输入流
        BufferedReader bd = new BufferedReader(new InputStreamReader(in));
        //包装写入的文件
        BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
        //读数据
        String s;
        while ((s = bd.readLine())!=null){
            bw.write(s);
            bw.newLine();
            bw.flush();
        }
        //释放资源
        ss.close();
        bw.close();
    }

服务端:ServerSocket
步骤:
创建绑定到特定端口的服务器套间字
ServerSocker(int port)
监听客户端
accept();
获取输入流
getInputStream();
读数据
关闭资源
代码:
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(8887);

            Socket socket = ss.accept();

            //获取输入流
            InputStream in = socket.getInputStream();
            //包装输入流
            BufferedReader bd = new BufferedReader(new InputStreamReader(in));
            //包装写入的文件
            BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
            //读数据
            String s;
            while ((s = bd.readLine())!=null){
                bw.write(s);
                bw.newLine();
                bw.flush();
            }
            //释放资源
            ss.close();
            bw.close();
        }
    }

需求:
编写程序,通过TCP协议,完成多线程版本客户端上传文件到服务器端,服务器端给出反馈。

代码:
    [客户端]
    public static void main(String[] args) throws Exception {
        // 创建客户端对象
        Socket s = new Socket("192.168.0.27", 8887);
        //获取输入流
        OutputStream out = s.getOutputStream();
        InputStream in = s.getInputStream();
        //包装输出流
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));

        //读取文件
        BufferedReader br = new BufferedReader(new FileReader("a.txt"));

        String ss;
        while((ss= br.readLine())!=null){
            bw.write(ss);
            bw.newLine();
            bw.flush();
        }
        s.shutdownInput();
        //读取反馈
        byte[] b = new byte[1024];
        System.out.println(new String(b,0,b.length));
        s.close();
    }
    [服务器端]
    public static void main(String[] args) throws Exception {
        //创建服务端对象
        ServerSocket ss= new ServerSocket(8887);
        System.out.println("等待客户端连接");
        int x = 1;
        while(true){
            //监听客户端
            Socket socket = ss.accept();
            System.out.println(x++ +"个服务器进来了!");
            //启动线程
            new Thread(new TCPRunnable(socket)).start();
        }
    }
    [线程部分]
    public class TCPRunnable implements Runnable {
        Socket socket;

        public TCPRunnable(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {

            try {
                // 获取输入流
                InputStream in = socket.getInputStream();
                OutputStream out = socket.getOutputStream();
                // 包装输入流
                BufferedReader bd = new BufferedReader(new InputStreamReader(in));
                // 包装写入的文件
                BufferedWriter bw = new BufferedWriter(new FileWriter(System.currentTimeMillis() + "c.txt"));
                // 读数据
                String s;
                while ((s = bd.readLine()) != null) {
                    bw.write(s);
                    bw.newLine();
                    bw.flush();
                }
                //给客户端一个反馈
                out.write("文件上传成功!".getBytes());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

你可能感兴趣的:(网络编程,tcp)