Java网络编程

来源:互联网 发布:淘宝假冒商品处罚买家 编辑:程序博客网 时间:2024/06/05 18:49

一、URL

    1、URL类

        1)创建URL类的对象

            public URL(String str)
            public URL(String protocol, String host, String file)
            public URL(String protocol, String host, String port, String file)
            public URL(URL content, String str)

        2)URL类的常用方法
            Int getPort()
            String getProtocol()
            String getHost()
            String getFile()
            Boolean equals(Object obj)
            String toString()

    2、URLConnection类

        1)创建URLConnect类的对象
            URL url = new URL(“www.baidu.com”);
            URLConnection connect = url.openConnection();

        2)建立输入/输出数据流

三、Socket通讯

    1、Socket通讯机制
        1)建立连接
        2)数据通讯
        3)拆除连接

    2、Socket类和ServerSocket类
        ServerSocket(int port)
        ServerSocket(int port, int count)
        Socket(InetAddress address, int port)
        Socket(InetAddress address, int port, Boolean Strean)
        Socket(String host, int port)
        Socket(String host, int port, Boolean strean))

    3、网络编程的一般过程

        1)创建ServerSocket类对象和Socket类对象
            (1)创建ServerSocket对象
            ServerSocket listen = new ServerSocket(4321);
            Socket line = Listen.accept();
            (2)创建Socket对象
            Socket service = new Socket(“address”, 4321);

        2)发送和接收流式数据
            OutputStream translate= server.getOutputStream();
            InputStream receive = service.getInputStream();
            translate.write(receive.read);

        3)拆除连接
            对于客服端:socket.close();
            对于服务器端:server.close();</

0 0
原创粉丝点击