网络编程

在java 中支持网络通讯程序的开发,主要提供了两种通讯协议:TCP协议、UDP协议。

1)TCP协议:可靠的连接传输,使用三方握手的方式完成通信。

2)UDP协议:不可靠的连接传输,传输的时候接收方未必能接收的到。

三方握手
  在可靠的传输层操作中,一台想要传送数据的设备通过创建一个会话,与远程设备建立起面向连接的通信。发送方设备首先与其对端系统建立起面向连接的会话,这称为“三方握手”。
  三方握手包括三个步骤:
    ·第一个“同意连接”数据段用来请求同步。
    ·第二个和第三个数据段用来确认请求并在主机之间建立连接参数(即规则)。在这里,接收方的排序也要求进行同步,以便建立双向连接。
    ·最后一个数据段也用来进行确认。它通知目的主机已同意建立连接,并且已经建立了实际连接。然后就可以开始传输数据了。当数据传输完成后,就会产生呼叫终止,以断开虚电路。

在java中的所有网络程序的开发类都存放在java.net包中

  IP地址=网络地址+主机地址

  网络号:用于识别主机所在的网络;

  主机号:用于识别该网络中的主机;

IP地址中存在掩码的功能主机是为了区分网络号和主机号;

InetAddress

InetAddress类主要表示IP地址,这个类有两个子类:Inet4Address、Inet6Address,一个用于表示IPV4,另一个表示IPV6协议。

InetAddress类的常用方法

public static InetAddress getByName(String host) throw UnkownHostException      通过主机名称得到InetAddress对象。

public static InetAddress getLocalHost() throws UnknownHostException  通过本机得到InetAddress对象

public String getHostName()  得到IP地址

public boolean isReachable(int timeout) throws IOException  是否可达,可设置超时时间

import java.net.InetAddress;


public class InetAddressDemo {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        InetAddress locAdd = null;
        InetAddress remAdd = null;
        remAdd = InetAddress.getByName("www.mldnjava.cn");
        locAdd = InetAddress.getLocalHost();
        System.out.println("本地的IP地址:"+locAdd.getHostAddress());
        System.out.println("MLDNJAVA的IP地址:"+remAdd.getHostAddress());
        System.out.println("本机是否可达:"+ locAdd.isReachable(5000));
        System.out.println("主机名:" + locAdd.getHostName());
    }

}

URL与URLConnection

URL统一资源定位符,可以直接使用此类找到互联网上的资源。

public URL(String protocol,String host,int port,String file) throws MalformedURLException

使用以上的构造方法完成读取的功能。

public final InputStream openStream() throws IOException

通过以上方法可以取得整个页面的输入流

import java.net.URL;
import java.io.InputStream;
import java.util.Scanner;
public class URLDemo {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        URL url = new URL("http","www.mldnjava.cn",80,"/curriculum.htm");
        InputStream input = url.openStream();
        Scanner scan = new Scanner(input);
        scan.useDelimiter("
");
        while(scan.hasNext()){
            System.out.println(scan.next());
        }
    }

}

URLConnection

URLConnection是封装访问远程网络资源一般方法的类,通过它可以建立远程服务器的连接,检查远程资源的一些属性。

public int getContentLength()  获取内容长度

public String getContentType()  取得内容的类型

public InputStream getInputStream() throws IOException  取得连接的输入流

import java.net.URL;
import java.net.URLConnection;
import java.io.InputStream;
import java.util.Scanner;

public class URLConnectionDemo  {
    public static void main(String args[])throws Exception{
        URL url = new URL("http://www.baidu.com");
        URLConnection ulrCon = url.openConnection();
        System.out.println("内容大小:" +ulrCon.getContentLength());
        System.out.println("内容类型:" +ulrCon.getContentType());
    }
}

 URLEncoder与URLDecoder

使用Encoder进程编码,Decoder进行解码操作,一般在进行网络程序开发时,传递中文的时候往往会需要进行编码和解码的操作。

public static String encode(String s,String enc) throws UnsupportedEncodingException  使用指定的编码机制将字符串转换为application/x-www-form-urlencoded格式

public static String decode(String s,String enc) throws UnsupportedEncodingException  使用指定的编码机制对application/x-www-form-urlencoded字符串解码

import java.net.URLEncoder;
import java.net.URLDecoder;
public class CodeDemo {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        String str = "测试";
        String encod = URLEncoder.encode(str, "UTF-8");
        System.out.println("编码之后的内容:"+encod);
        String decod = URLDecoder.decode(encod, "UTF-8");
        System.out.println("解码之后的内容:"+decod);
    }

}
原文地址:https://www.cnblogs.com/zhuangjb/p/3579372.html