Java第十三章网络编程
网络编程:
IP地址:InetAddress
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。》唯一的标识
端口号:标识正在计算机运行的进程
0~65535其中0~1023被预先定义
端口号与IP地址的组合得出一个网络套接字。
/** * InetAddress:位于java.net包下 * 1.InetAddress用来代表IP地址,一个InetAddress的对象就代表着一个IP地址 * 2.如何创建InetAddress的对象,getByName(String host) * 3.getHostName():获取IP地址对应的域名 * 4.getHostAddress():获取IP地址 */ public class NetADDress { public static void main(String[] args) throws UnknownHostException { //创建一个InetAddress对象,getByName() InetAddress inetAddress = InetAddress.getByName("www.baidu.com");//域名或IP地址 System.out.println(inetAddress); //两个方法 System.out.println(inetAddress.getHostName()); System.out.println(inetAddress.getHostAddress()); //获得本机的IP:getLocalHost() InetAddress inetAddress1 = InetAddress.getLocalHost(); System.out.println(inetAddress1); System.out.println(inetAddress1.getHostName()); System.out.println(inetAddress1.getHostAddress()); } }
TCP和UDP
TCP协议:
》使用TCP协议前,须先建立TCP连接,形成传输数据通道
》传输前,采用“三次握手”方式,是可靠的
》TCP协议进行通信的两个应用进程:客户端、服务器
》传输完毕,须释放已建立的连接,效率低
UDP协议:
》将数据、源、目的封装成数据包,不需要建立连接
》每个数据包的大小限制在64K内
》因无需连接,故是不可靠的
》发送数据结束时无需释放资源,速度快

更多精彩