• Java 网络编程(二)


       以下例开始本文的内容:   

       例1,需求:上传图片。

       客户端:

    1. 服务端点。
    2. 读取客户端已有的图片数据。
    3. 通过socket输出流将数据发给服务端。
    4. 读取服务端反馈信息。
    5. 关闭。   
    class PicClient {
        public static void main(String[] args) throws IOException {
            /*
             * 一系列判断
             */
            if(args.length != 1) {
                System.out.println("请选择一个jpg格式的图片");
                return;
            }
            File file = new File(args[0]);
            if(!(file.exists() && file.isFile())) {
                System.out.println("该文件有问题,要么不存在,要么不是文件");
                return;
            }
            if(!file.getName().endsWith(".jpg")) {
                System.out.println("图片格式错误,请重新选择");
                return;
            }
            if(file.length() > 1024*1024*10) {
                System.out.println("文件过大,没安好心");
                return;
            }
            
            
            Socket s = new Socket("10.48.62.209", 10007);
            
            FileInputStream fis = new FileInputStream(file);
            
            OutputStream out = s.getOutputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            while((len = fis.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            
            //告诉服务端数据已写完
            s.shutdownOutput();
            
            InputStream in = s.getInputStream();
            byte[] bufIn = new byte[1024];
            int num = in.read(bufIn);
            System.out.println(new String(bufIn, 0, num));
            fis.close();
            s.close();
        }
    }

       服务端:

       这个服务端有个局限性,当A客户端连接上以后,被服务端获取到,服务端执行具体流程。这时B客户端连接,只有等待,因为服务端还没有处理完A客户端的请求,还没有循环回来执行下一次accpet()方法,所以暂时获取不到B客户端对象。

       那么为了可以让多个客户端同时并发访问服务端,服务端最好就是将每个客户端封装到一个单独的线程中,这样就可以同时处理多个客户端请求。

       那么如何定义线程呢?

       只要明确了每一个客户端在服务端执行的代码即可。将该代码存入run()方法中。

    class PicThread implements Runnable {
        private Socket s;
        PicThread(Socket s) {
            this.s = s;
        }
        @Override
        public void run() {
            int count = 1;
            String ip = s.getInetAddress().getHostAddress();
            try {
                
                System.out.println(ip+"......connected.");
            
                
                InputStream in = s.getInputStream();
                
                File file = new File(ip+"("+(count)+")"+".jpg");//10.48.62.209(1).jpg
                while(file.exists()) {
                    file = new File(ip+"("+(count++)+")"+".jpg");
                }
                
                
                FileOutputStream fos = new FileOutputStream(file);
                
                
                byte[] buf = new byte[1024];
                int len = 0;
                while((len = in.read(buf)) != -1) {
                    fos.write(buf, 0, len);
                }
                OutputStream out = s.getOutputStream();
                out.write("上传成功".getBytes());
                fos.close();
                s.close();
            } catch (IOException e) {
                throw new RuntimeException(ip+"上传失败");
            }
        }
        
    }
    class PicServer {
        public static void main(String[] args) throws IOException {
            ServerSocket ss = new ServerSocket(10007);
            while(true) {
                Socket s = ss.accept();
                
                new Thread(new PicThread(s)).start();
            }
            //ss.close();
        }
        
    }

       我觉得多线程是比较难理解的,所以图示以上原理:

       

       例2,客户端通过键盘录入用户名。服务端对这个用户名进行校验。如果该用户存在,在服务端显示XXX,已登录。并在客户端显示XXX,欢迎光临。如果该用户不存在,在服务端显示XXX,尝试登录。并在客户端显示XXX,该用户不存在。最多就登录3次。

       以下为代码,导包就不导了。

       客户端:

    class LoginClient {
        public static void main(String[] args) throws IOException {
            Socket s = new Socket("10.48.62.209", 10008);
            
            BufferedReader bufr = 
                    new BufferedReader(new InputStreamReader(System.in));
            
            PrintWriter out = new PrintWriter(s.getOutputStream(), true);
            
            BufferedReader bufIn = 
                    new BufferedReader(new InputStreamReader(s.getInputStream()));
            
            for(int x = 0; x < 3; x++) {
                /*
                 * 在键盘敲Ctrl+C键,即代表结束录入
                 * read()方法返回-1,readLine()方法返回null
                 */
                String line = bufr.readLine();//Ctrl+C,那么就是-1,readLine()返回null
                if(line == null) 
                    break;
                out.println(line);
                String info = bufIn.readLine();
                System.out.println("info:"+info);
                if(info.contains("欢迎"))
                    break;
            }
            bufr.close();
            s.close();//socket流中加了一个-1,相当于给流中加入一个结束标记,即-1
        }
    }

       服务端:

    class UserThread implements Runnable {
        private Socket s;
        UserThread(Socket s) {
            this.s = s;
        }
        @Override
        public void run() {
            String ip = s.getInetAddress().getHostAddress();
            System.out.println(ip+"........connected.");
            try {
                for(int x = 0; x < 3; x++) {
                    BufferedReader bufIn = new BufferedReader(
                            new InputStreamReader(s.getInputStream()));
                    /*
                     * 客户端那边敲入Ctrl+C键键时,
                     * read()返回-1,readLine()返回null。
                     * 所以name得加一个判断
                     */
                    String name = bufIn.readLine();
                    if(name == null) 
                        break;
                    
                    /*
                     * 校验
                     */
                    BufferedReader bufr = new BufferedReader(new FileReader("user.txt"));
                    
                    PrintWriter out = new PrintWriter(s.getOutputStream(), true);
                    
                    String line = null;
                    
                    boolean flag = false;//定义标记
                    while((line = bufr.readLine()) != null) {
                        if(line.equals(name)) {
                            flag = true;
                            break;
                        }
                    }
                    if(flag) {
                        System.out.println(name+",已登录");
                        out.println(name+",欢迎光临");
                        break;
                    } else {
                        System.out.println(name+"尝试登录");
                        out.println(name+",用户名不存在");
                    }
                }
                s.close();
            } catch (IOException e) {
                throw new RuntimeException(ip+"校验失败");
            }
        }
        
    }
    class LoginServer {
        public static void main(String[] args) throws IOException {
            ServerSocket ss = new ServerSocket(10008);
            while(true) {
                Socket s = ss.accept();
                new Thread(new UserThread(s)).start();
            }
        }
        
    }

       

       演示客户端和服务端

       1、

       客户端:浏览器、telnet(远程登录命令——在DOS命令行下连接网络上任意一台主机)

       形如:

    telnet 10.48.62.209 11000
    

       示例代码:

    public class ServerDemo {
    
        public static void main(String[] args) throws IOException {
            ServerSocket ss = new ServerSocket(11000);
            
            Socket s = ss.accept();
            System.out.println(s.getInetAddress().getHostAddress());
            
            InputStream in = s.getInputStream();
            byte[] buf = new byte[1024];
            int len = in.read(buf);
            System.out.println(new String(buf, 0, len));
            
            PrintWriter out = new PrintWriter(s.getOutputStream(), true);
            out.println("<font color='red' size='7'>Client, hello!!!</font>");
            s.close();
            ss.close();
        }
    
    }

       控制台输出类似诸如以下信息(头信息):

    GET / HTTP/1.1
    Host: 127.0.0.1:11000
    Connection: keep-alive
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
    Accept-Encoding: gzip, deflate, sdch
    Accept-Language: zh-CN,zh;q=0.8

       分析如下:

    http(http协议)://127.0.0.1(主机名):11000(端口号)/myWeb(资源路径)/demo.html(资源)
    Host: 127.0.0.1:11000
    Connection: keep-alive
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*(代表可接受任意类型的东东);q=0.8
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
    Accept-Encoding: gzip(告诉服务器的压缩方式), deflate, sdch
    Accept-Language: zh-CN,zh;q=0.8

       2、

       客户端:浏览器。

       服务器:Tomcat服务器。

       3、

       客户端:自定义。

       服务器:Tomcat服务器。

       示例代码:

    public class MyIE {
    
        public static void main(String[] args) throws IOException {
            Socket s = new Socket("127.0.0.1", 8888);
            
            PrintWriter out = new PrintWriter(s.getOutputStream(), true);
            
            out.println("GET /myWeb/demo.html HTTP/1.1");
            out.println("Accept: */*");
            out.println("Accept-Language: zh-CN,zh;q=0.8");
            out.println("Host: 127.0.0.1:11000");
            out.println("Connection: closed");
            /*
             * 注意:HTML头信息和主体信息之间一定要有一个空行
             * 为了保证安全,所以索性加了两行
             */
            out.println();
            out.println();
            
            BufferedReader bufr = new BufferedReader(
                    new InputStreamReader(s.getInputStream()));
            String line = null;
            while((line = bufr.readLine()) != null) {
                System.out.println(line);
            }
            s.close();
        }
    
    }

       控制台会打印类似信息:

       自定义图形界面浏览器

       代码如下(导包就不导了):

    public class MyIEByGUI {
        
        private Frame f;
        private TextField tf;
        private Button but;
        private TextArea ta;
        
        private Dialog d;
        private Label lab;
        private Button okBut; 
        
        MyIEByGUI() {
            init();
        }
        
        public void init() {
            f = new Frame("my window");
            f.setBounds(300, 100, 600, 500);
            f.setLayout(new FlowLayout());
            
            tf = new TextField(60);
            
            but = new Button("转到");
            
            ta = new TextArea(25, 70);
            
            /*
             * 对话框,也是一个窗体,最好不要加到Frame里面去
             */
            d = new Dialog(f, "提示信息-self", true);//true:对话框不处理掉,后面的窗体是无法操作的!!
            d.setBounds(400, 200, 240, 150);
            d.setLayout(new FlowLayout());
            lab = new Label();
            okBut = new Button("确定");
            
            d.add(lab);
            d.add(okBut);
            
            f.add(tf);
            f.add(but);
            f.add(ta);
            
            myEvent();
            f.setVisible(true);
            
        }
        
        private void myEvent() {
            //点击对话框中的确定按钮,对话框也不显示出来
            okBut.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    d.setVisible(false);
                }
            });
            /*
             * 关闭对话框,对话框不显示
             */
            d.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    d.setVisible(false);
                }
            });
            
            tf.addKeyListener(new KeyAdapter() {
    
                @Override
                public void keyPressed(KeyEvent e) {
                    if(e.getKeyCode() == KeyEvent.VK_ENTER) {
                        try {
                            showDir();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
                
            });
            
            /*
             * 按钮是事件源
             */
            but.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        showDir();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            });
            
            f.addWindowListener(new WindowAdapter() {
    
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
                
                
            });
        }
        
        private void showDir() throws IOException {
            ta.setText("");
            /*
             * http://127.0.0.1:8888/myWeb/demo.html
             * 以下代码只不过是拆解url字符串路径而已
             */
            String url = tf.getText();
            
            int index1 = url.indexOf("//")+2;
            
            int index2 = url.indexOf("/", index1);
            
            String str = url.substring(index1, index2);
            String[] arr = str.split(":");
            String host = arr[0];
            int port = Integer.parseInt(arr[1]);
            
            String path = url.substring(index2);
            //ta.setText(str+"....."+path);
            
            Socket s = new Socket(host, port);
            
            PrintWriter out = new PrintWriter(s.getOutputStream(), true);
            
            out.println("GET "+path+" HTTP/1.1");
            out.println("Accept: */*");
            out.println("Accept-Language: zh-CN,zh;q=0.8");
            out.println("Host: 127.0.0.1:11000");
            out.println("Connection: closed");
            out.println();
            out.println();
            
            BufferedReader bufr = new BufferedReader(
                    new InputStreamReader(s.getInputStream()));
            String line = null;
            while((line = bufr.readLine()) != null) {
                ta.append(line+"
    ");
            }
            s.close();
            
        }
        
        public static void main(String[] args) {
            new MyIEByGUI();
        }
    
    }

       运行效果图:

     

       URL 

       示例代码:

    /*
    String getFile() 
              获取此 URL 的文件名。 
    String getHost() 
              获取此 URL 的主机名(如果适用)。 
    String getPath() 
              获取此 URL 的路径部分。 
    int getPort() 
              获取此 URL 的端口号。 
    String getProtocol() 
              获取此 URL 的协议名称。 
    String getQuery() 
              获取此 URL 的查询部分。 
    */
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class URLDemo {
    
        public static void main(String[] args) throws MalformedURLException {
            URL url = new URL("http://127.0.0.1/myWeb/demo.html?name=haha&age=30");
            
            System.out.println("getProtocol():"+url.getProtocol());//http
            System.out.println("getHost():"+url.getHost());//127.0.0.1
            System.out.println("getPort():"+url.getPort());
            System.out.println("getPath():"+url.getPath());//---/myWeb/demo.html
            System.out.println("getFile():"+url.getFile());//---/myWeb/demo.html?name=haha&age=30
            System.out.println("getQuery():"+url.getQuery());//name=haha&age=30
            
            /*
            不写端口时,给一个默认的端口号80
            int port = getPort();
            if(port == -1) 
                port = 80;
            */
        }
    
    }

       URLConnection(似乎封装了Socket)

       示例代码:

    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;
    
    public class URLConnectionDemo {
    
        public static void main(String[] args) throws IOException {
            URL url = new URL("http://127.0.0.1:8888/myWeb/demo.html");
            URLConnection conn = url.openConnection();
            System.out.println(conn);
            
            InputStream in = conn.getInputStream();
            
            byte[] buf = new byte[1024];
            
            int len = in.read(buf);
            
            System.out.println(new String(buf, 0, len));
            
        }
    
    }

       所以自定义图形界面浏览器代码优化之后为:

    public class MyIEByGUI2 {
        
        private Frame f;
        private TextField tf;
        private Button but;
        private TextArea ta;
        
        private Dialog d;
        private Label lab;
        private Button okBut; 
        
        MyIEByGUI2() {
            init();
        }
        
        public void init() {
            f = new Frame("my window");
            f.setBounds(300, 100, 600, 500);
            f.setLayout(new FlowLayout());
            
            tf = new TextField(60);
            
            but = new Button("转到");
            
            ta = new TextArea(25, 70);
            
            /*
             * 对话框,也是一个窗体,最好不要加到Frame里面去
             */
            d = new Dialog(f, "提示信息-self", true);//true:对话框不处理掉,后面的窗体是无法操作的!!
            d.setBounds(400, 200, 240, 150);
            d.setLayout(new FlowLayout());
            lab = new Label();
            okBut = new Button("确定");
            
            d.add(lab);
            d.add(okBut);
            
            f.add(tf);
            f.add(but);
            f.add(ta);
            
            myEvent();
            f.setVisible(true);
            
        }
        
        private void myEvent() {
            //点击对话框中的确定按钮,对话框也不显示出来
            okBut.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    d.setVisible(false);
                }
            });
            /*
             * 关闭对话框,对话框不显示
             */
            d.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    d.setVisible(false);
                }
            });
            
            tf.addKeyListener(new KeyAdapter() {
    
                @Override
                public void keyPressed(KeyEvent e) {
                    if(e.getKeyCode() == KeyEvent.VK_ENTER) {
                        try {
                            showDir();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
                
            });
            
            /*
             * 按钮是事件源
             */
            but.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        showDir();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            });
            
            f.addWindowListener(new WindowAdapter() {
    
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
                
                
            });
        }
        
        private void showDir() throws IOException {
            ta.setText("");
            /*
             * http://127.0.0.1:8888/myWeb/demo.html
             */
            String urlPath = tf.getText();
            
            URL url = new URL(urlPath);
            URLConnection conn = url.openConnection();
            //System.out.println(conn);
            
            InputStream in = conn.getInputStream();
            
            byte[] buf = new byte[1024];
            
            int len = in.read(buf);
            
            ta.setText(new String(buf, 0, len));
            
        }
        
        public static void main(String[] args) {
            new MyIEByGUI2();
        }
    
    }

       图示原理:

       域名解析

       

  • 相关阅读:
    Cannot run program "/home/mohemi/Program/adt-bundle-linux-x86_64-20130729/sdk//tools/emulator": error=2, 没有那个文件或目录
    在Ubuntu里部署Javaweb环境脑残版
    ios 内存管理 心得
    关于Servlet的PrintWriter 中文乱码问题
    流的对拷
    什么是javaBean
    ios 页面传值4种方式(四) 之通过delegate(代理)
    ios 页面传值4种方式(一) 之全局变量
    Block
    UIView动画学习笔记
  • 原文地址:https://www.cnblogs.com/yerenyuan/p/5289137.html
Copyright © 2020-2023  润新知