Java 网络编程 - 总结概述
2024-10-19 14:11:57
IP地址
IP地址IntAddress
- 唯一定位一台网络上的计算机
- 127.0.0.1:本地localhost
- IP地址的分类
- ipV4/ipV6
- ipV4:127.0.0.1,4个字节组成;0~255,42亿~;30亿都在北美,亚洲4亿;2011年就用完了
- ipV6:128位。8个无符号整数
- 公网(互联网)-私网(局域网)
- ABCD类地址
- 192.168 .xx.xx,专门给组织内部使用的
- ipV4/ipV6
- 域名:方面记忆,免去了记录IP的问题
1 //测试IP
2 public class TestInetAddress {
3 public static void main(String[] args) {
4 try {
5 //查询本机地址
6 InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
7 System.out.println(inetAddress);
8 InetAddress localhost = InetAddress.getByName("localhost");
9 System.out.println(localhost);
10 InetAddress localHost = InetAddress.getLocalHost();
11 System.out.println(localHost);
12
13 //查询网站ip地址
14 InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");
15 System.out.println(inetAddress1);
16
17 //常用方法
18 System.out.println(inetAddress1.getHostAddress());//ip
19 System.out.println(inetAddress1.getHostName());//域名,或者自己的名字
20 } catch (UnknownHostException e) {
21 e.printStackTrace();
22 }
23 }
24 }
端口
ip相当于省/市/区/街/楼,端口就是门牌号;端口表示计算机上的一个程序的进程
- 不同的进程有不同的端口号!用来区分软件!
- 被规定0~65535
- TCP,UDP:65535*2;tcp:80;udp:80
- 端口分类
- 公有端口0~1023
- HTTP:80
- HTTPS:443
- FTP:21
- Telent:23
- 程序注册端口:1024~49151,分配用户或者程序
- Tomcat:8080
- MySQL:3306
- Orcal:1521
- 动态、私有:49152~65535
- 公有端口0~1023
//CMD
netstat -ano #查看所有的端口
netstat -ano|findstr "5900" #查看指定的端口
tasklist|findstr "8696" #查看指定端口的进程
1 //端口
2 public class TestInetSocketAddress {
3 public static void main(String[] args) {
4 InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080);
5 System.out.println(socketAddress);
6
7 System.out.println(socketAddress.getAddress());
8 System.out.println(socketAddress.getHostName());//地址
9 System.out.println(socketAddress.getPort());//端口
10 }
11 }
通信协议
协议:约定,共同遵守,都能理解
网络通信协议:速率,传输码率,代码结构,传输控制....
TCP/IP协议簇:实际上是一组协议
重要
- TCP:用户传输协议
- UDP:用户数据报协议
TCP UDP对比
TCP:打电话
- 连接,稳定
- 三次握手,四次挥手
- 客户端、服务端
- 传输完成,释放连接,效率低
UDP:发短信
- 不连接,不稳定
- 客户端、服务端:没有明确的界限
- 不管有没有准备好,都可以发给你
TCP实现聊天
1 //服务端
2 public class TcpServerDemo01 {
3 public static void main(String[] args) {
4 ServerSocket serverSocket = null;
5 Socket accept=null;
6 InputStream is=null;
7 ByteArrayOutputStream baos=null;
8 try {
9 //1.得有一个地址
10 serverSocket = new ServerSocket(9999);
11
12 while (true){
13 //2.等待客户端连接过来
14 accept = serverSocket.accept();
15 //3.读取客户端得消息
16 is = accept.getInputStream();
17
18 //管道流
19 baos = new ByteArrayOutputStream();
20 byte[] bytes = new byte[1024];
21 int len;
22 while ((len=is.read(bytes))!=-1){
23 baos.write(bytes,0,len);
24 }
25 System.out.println(baos.toString());
26 }
27
28 } catch (IOException e) {
29 e.printStackTrace();
30 }finally {
31 //关闭流
32 try {
33 baos.close();
34 } catch (IOException e) {
35 e.printStackTrace();
36 }
37 try {
38 is.close();
39 } catch (IOException e) {
40 e.printStackTrace();
41 }
42 try {
43 accept.close();
44 } catch (IOException e) {
45 e.printStackTrace();
46 }
47 try {
48 serverSocket.close();
49 } catch (IOException e) {
50 e.printStackTrace();
51 }
52
53 }
54 }
55 }
1 //客户端
2 public class TcpClientDemo01 {
3 public static void main(String[] args) {
4 Socket socket=null;
5 OutputStream os=null;
6
7 try {
8 //1.要直到服务器得地址
9 InetAddress serverIP= InetAddress.getByName("127.0.0.1");
10 int port=9999;
11 //2.创建一个socker连接
12 try {
13 socket = new Socket(serverIP,port);
14 //3.发送消息 IO流
15 os = socket.getOutputStream();
16 os.write("Hello".getBytes());
17 } catch (IOException e) {
18 e.printStackTrace();
19 }
20
21
22 } catch (UnknownHostException e) {
23 e.printStackTrace();
24 }finally {
25 try {
26 os.close();
27 } catch (IOException e) {
28 e.printStackTrace();
29 }
30 try {
31 socket.close();
32 } catch (IOException e) {
33 e.printStackTrace();
34 }
35 }
36 }
37 }
TCP文件上传
1 //服务端
2 public class TcpServerDemo02 {
3 public static void main(String[] args) throws Exception{
4 //1.创建服务
5 ServerSocket serverSocket = new ServerSocket(9000);
6 //2.监听客户端得连接
7 Socket accept = serverSocket.accept();//阻塞式监听,会一直等待客户端得连接
8 //3.获取输入流
9 InputStream is = accept.getInputStream();
10
11 //4.文件输出
12 FileOutputStream fos = new FileOutputStream("receive.jpg");
13 byte[] by = new byte[1024];
14 int len;
15 while ((len=is.read(by))!=-1){
16 fos.write(by,0,len);
17 }
18
19 //通知客户端我接收完毕了
20 OutputStream os = accept.getOutputStream();
21 os.write("接收完毕".getBytes());
22
23 os.close();
24 fos.close();
25 is.close();
26 accept.close();
27 serverSocket.close();
28 }
29 }
1 //客户端
2 public class TcpClientDemo02 {
3 public static void main(String[] args) throws Exception{
4 //1.创建一个socket连接
5 Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
6 //2.创建一个输出流
7 OutputStream os = socket.getOutputStream();
8
9 //3.读取文件
10 FileInputStream fis = new FileInputStream("D:\\WorkSpace\\JavaSE\\基础语法\\111.png");
11 //4.写出文件
12 byte[] by = new byte[1024];
13 int len;
14 while ((len=fis.read(by))!=-1){
15 os.write(by,0,len);
16 }
17
18 //通知服务器,我已经传输结束了
19 socket.shutdownOutput();
20
21 //确认服务器接收完毕,才能断开连接
22 InputStream is = socket.getInputStream();
23 ByteArrayOutputStream baos = new ByteArrayOutputStream();
24
25 byte[] bytes = new byte[1024];
26 int leng;
27 while ((leng=is.read(bytes))!=-1){
28 baos.write(bytes,0,leng);
29 }
30 System.out.println(baos.toString());
31
32 baos.close();
33 is.close();
34 os.close();
35 fis.close();
36 socket.close();
37 }
38 }
UDP消息发送
1 //发送方
2 public class UdpClientDemo01 {
3 public static void main(String[] args) throws Exception{
4 //1.建立一个Socket
5 DatagramSocket datagramSocket = new DatagramSocket();
6
7 //2.建个包
8 String msg="你好啊,服务器!";
9 InetAddress localhost = InetAddress.getByName("localhost");
10 int port = 9090;
11
12 //数据、数据的长度起始、要发给谁
13 DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
14
15 //发送包
16 datagramSocket.send(datagramPacket);
17
18 //4.关闭流
19 datagramSocket.close();
20 }
21 }
1 //接收方
2 public class UdpServerDemo01 {
3 public static void main(String[] args) throws Exception{
4 //开放端口
5 DatagramSocket datagramSocket = new DatagramSocket(9090);
6 //接收数据
7 byte[] bytes = new byte[1024];
8 DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length);
9
10 datagramSocket.receive(datagramPacket);//阻塞接收
11
12 System.out.println(datagramPacket.getAddress());
13 System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));
14 }
15 }
UDP聊天实现
1 //发送方
2 public class UdpSenderDemo01 {
3 public static void main(String[] args) throws Exception{
4
5 DatagramSocket datagramSocket = new DatagramSocket(8888);
6
7 //准备数据:控制台读取System.in
8 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
9
10 while (true){
11 String data=reader.readLine();
12 byte[] bytes = data.getBytes();
13 DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress("localhost",6666));
14 datagramSocket.send(datagramPacket);
15 if(bytes.equals("byebye")){
16 break;
17 }
18 }
19 datagramSocket.close();
20 }
21 }
1 //接收方
2 public class UdpReceiveDemo01 {
3 public static void main(String[] args) throws Exception{
4 DatagramSocket datagramSocket = new DatagramSocket(6666);
5
6 while (true){
7 //准备接收包裹
8 byte[] bytes = new byte[1024];
9 DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length);
10
11 //断开连接 byebye
12 byte[] data = datagramPacket.getData();
13 String string = new String(data, 0, data.length);
14 System.out.println(string);
15 if(string.equals("byebye")){
16 break;
17 }
18 }
19
20 datagramSocket.close();
21
22 }
23 }
最新文章
- WEB 基础知识(一)
- ajax 的返回值类型
- ASP.NET 5探险(3):使用UMEditor并实现图片上传
- Javascript基础--类与对象(五)
- NetBeansRCP-添加/修改NetBeans的JVM启动参数
- BZOJ 1045: [HAOI2008] 糖果传递 数学
- Android(工具・集成)
- select count(1) from table where ..这句sql语句的作用
- POJ 1469 ZOJ1140 二分匹配裸题
- MySQL server has gone away报错
- Android studio 中国的垃圾问题解决
- Spring-AOP用法总结
- 实现简易版的moment.js
- zk日常运维管理
- PAT1002:A+B for Polynomials
- NIO(生活篇)
- 学习STM32F769DK-OTA例程之百度云平台建立MQTT服务器
- sql 日志文件截断收缩
- Guava Cache源码详解
- Keepalived详解(五):Keepalived集群中MASTER和BACKUP角色选举策略【转】