初识网络编程
前言
1.端口表示计算机的一个程序的进程:
2.不同的进程有不同的端口号,用来区分软件
3.理论上规定了0~65536
4.TCP,UDP:65536*2 Tcp:80 udp:80,单个协议下,端口不能冲突
5.端口分类:
共有端口0~1023
HTTP:80 超文本传输协议
HTTPS:443 超文本传输安全协议,基于ssl的http协议
FTP:21 ,文件传输协议
Telent:23 远程传输协议
程序注册端口:1024~49151
Tomcat:8080
MySQL:3306
Oracle:1521
动态,私有:49152~65535
简单实现网络通信
package com.cl.lesson01; import java.net.InetAddress; import java.net.UnknownHostException; //封装IP,InetAddress public class TestInterAddress { public static void main(String[] args) { try { InetAddress interAddress1=InetAddress.getByName("127.0.0.1"); System.out.println(interAddress1); InetAddress interAddress3=InetAddress.getByName("localhost"); System.out.println(interAddress3); InetAddress interAddress4=InetAddress.getLocalHost(); System.out.println(interAddress4); //查询网站IP地址 InetAddress interAddress2=InetAddress.getByName("www.baidu.com"); System.out.println(interAddress2); //常用方法 System.out.println(interAddress2.getAddress()); System.out.println(interAddress2.getCanonicalHostName());//规范的名字 System.out.println(interAddress2.getHostAddress());//ip System.out.println(interAddress2.getHostName());//域名 }catch (UnknownHostException e){ e.printStackTrace(); } } } package com.cl.lesson01; import java.net.InetSocketAddress; //封装计算机的ip+端口号,InetSocketAddress public class TestInetSocketAddress { public static void main(String[] args) { InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",8080); InetSocketAddress socketAddress2 = new InetSocketAddress("localhost",8080); System.out.println(socketAddress2); System.out.println(socketAddress.getAddress()); System.out.println(socketAddress.getHostName());//地址 System.out.println(socketAddress.getPort());//端口 } }