概述
1.1什么是网络
1.2网络通信的要素
主要问题
1、如何准确定位到网络上的主机
2、找到主机之后如何通信
如何实现网络的通信
1、通信的双方地址:
IP地址
端口号
2、规则:网络通信协议
TCP/IP参考模型
OSI参考模型
IP地址
import java.net.InetAddress;import java.net.UnknownHostException;//测试IPpublic class TestInetAddress {public static void main(String[] args) {try {//创建对应IP的对象//静态方法,通过名字返回对象InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");System.out.println(inetAddress1);//查询网站IP地址InetAddress inetAddress2 = InetAddress.getByName("www.baodu.com");System.out.println(inetAddress2);InetAddress inetAddress3 = InetAddress.getByName("localhost");System.out.println(inetAddress3);//获取本机地址InetAddress inetAddress4 = InetAddress.getLocalHost();System.out.println(inetAddress4);//常用方法System.out.println(inetAddress2.getCanonicalHostName());//获取规范域名System.out.println(inetAddress2.getHostAddress());//获取IP地址System.out.println(inetAddress2.getHostName());//域名,或者自己电脑名} catch (UnknownHostException e) {e.printStackTrace();}}}
端口
端口表示计算机上的一个程序的进程
- 不同的进程有不同的端口号,用来区分软件
- 被规定:0~65536
- TCP,UDP,每一种协议都有0~65536个端口,不同的协议端口相同不会产生影响
端口分类
- 公有端口:0~ 1023
- HTTP:80
- HTTPS:443
- FTP:21
- Telent:23
- 程序注册端口:2014~49151,分配给用户或者程序
- tomcat :8080
- MYSQL:3306
- Orcal:1521
动态、私有:49152~65535
netstat -ano #查看所有的端口netstat -ano|findstr "5900" #查询5900端口tasklist|finder "8696" #查看指定端口的进程
import java.net.InetSocketAddress;public class TestSockAddress {public static void main(String[] args) {//创建端口和IP对象InetSocketAddress socketAddress1 = new InetSocketAddress("127.0.0.1", 8080);InetSocketAddress socketAddress2 = new InetSocketAddress("localhost", 8080);System.out.println(socketAddress1);System.out.println(socketAddress2);System.out.println(socketAddress1.getAddress());System.out.println(socketAddress1.getHostName());System.out.println(socketAddress1.getPort());}}
通信协议
TCP/IP协议簇
TCP:用户传输协议
TDP:
IP:网络互联协议
TCP
编写客户端和服务器 ```java import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket;
- 公有端口:0~ 1023
public class TcpClientDemo1 { public static void main(String[] args) throws IOException { OutputStream os = null; Socket socket=null; try { //创建一个ip地址对象 InetAddress serverIP = InetAddress.getByName(“127.0.0.1”); //端口号 int port = 9999; //创建一个socket连接 socket = new Socket(serverIP, port); //发送消息IO流 os=socket.getOutputStream(); os.write(“你好,这是一个测试信息”.getBytes()); } catch (Exception e) { e.printStackTrace(); }finally { if (os != null) os.close(); if (socket != null)socket.close(); }
}
}
```javaimport java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;public class TcpServerDemo1 {public static void main(String[] args) throws IOException {ServerSocket serverSocket=null;InputStream is = null;Socket socket=null;ByteArrayOutputStream baos = null;try {//创建一个地址对象serverSocket = new ServerSocket(9999);//等待用户连接.accept方法会把线程阻塞,知道接收客户端发送的信息socket=serverSocket.accept();//读取用户传递的信息is = socket.getInputStream();//管道流baos = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len;while ((len = is.read(buffer)) != -1) {baos.write(buffer, 0, len);}System.out.println(baos.toString());} catch (IOException e) {e.printStackTrace();}finally {//关闭资源if (baos != null)baos.close();if (is != null)is.close();if (socket != null)socket.close();if (serverSocket != null)serverSocket.close();}}}
