Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

IP

ip地址:InetAddress(java类)

  • 唯一定位一台网络上计算机
  • 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,专门给组织内部使用

端口

端口表示计算机上的一个进程;

  • 不同的进程有不同的端口号,用来区分软件

  • 被规定0~65535

  • TCP,UDP:65530*2,单个协议下端口号不能冲突

  • 端口分类

    • 共有端口0~1023

      • HTTP:80
      • HTTPS:443
      • FTP:21
      • Telent:23
    • 程序注册端口:1024~49151,分配用户或者程序

      • TomCat:8080
      • MySql:3306
      • Oracle:1521
    • 动态、私有端口:49152~65535

      1
      2
      3
      netstat -ano//查看所有端口
      netstat -ano|findstr//查看指定端口
      tasklist|findstr //查看指定端口的进程

通信协议

网络通信协议:速率,传输码率,代码结构,传输控制……

TCP/IP协议簇:实际上是一组协议

重要:

  • TCP:用户传输协议
  • UDP:用户数据包协议

出名的歇息:

  • TCP:
  • IP:网络互连协议

TCP于UDP对比

TCP:打电话

  • 连接,稳定
  • 三次握手,四次挥手
    • 三次挥手:A:你愁啥!B:瞅你咋滴 A:打一架
    • 四次挥手:A:分开吧 B:等我收拾收拾东西 B:我收拾完了,走了 A:好 拜拜
  • 客户端、服务端
  • 传输完成,释放连接,效率低

UDP:发短信

  • 不连接,不稳定
  • 客户端服务端:没有明确的界限
  • 不管有没有准备好,都可以发给你

TCP

客户端

  1. 连接服务器通过socket

  2. 发送消息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    package com.bestrookie.lesson02;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    /**
    * @author : bestrookie
    * @date : 19:57 2021/1/14
    * 客户端
    */
    public class TcpClientDemo {
    public static void main(String[] args) throws IOException {
    InetAddress serverIp = InetAddress.getByName("127.0.0.1");
    int port = 9999;
    Socket socket = new Socket(serverIp,port);
    OutputStream os = socket.getOutputStream();
    os.write("这就是网络".getBytes());
    os.close();
    socket.close();

    }
    }

服务器

  1. 简历服务的端口 serverSocket

  2. 等待连接 accept

  3. 接受用的消息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    package com.bestrookie.lesson02;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    /**
    * @author : bestrookie
    * @date : 19:57 2021/1/14
    * 服务端
    */
    public class TcpServerDemo {
    public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = new ServerSocket(9999);
    Socket socket;
    InputStream is;
    ByteArrayOutputStream baos;
    while (true){
    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());
    }
    }
    }

UDP

发短信:不用连接,需要知道对方的地址

发送消息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.bestrookie.lesson03;
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;

/**
* @author : bestrookie
* @date : 13:35 2021/1/16
*/
public class UdpClientDemo {
public static void main(String[] args) throws IOException {
//1.建立一个Socket
DatagramSocket socket = new DatagramSocket();
//2.建个包
String msg = "udp test";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(), 0, msg.length(),localhost,port);
socket.send(datagramPacket);
socket.close();
}
}

接受消息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.bestrookie.lesson03;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
* @author : bestrookie
* @date : 13:40 2021/1/16
*/
public class UdpServerDemo {
public static void main(String[] args) throws IOException {
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接受数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length );
socket.receive(packet);
System.out.println(packet.getSocketAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
}
}

循环发送消息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.bestrookie.chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;

/**
* @author : bestrookie
* @date : 16:31 2021/1/16
*/
public class UdpSenderDemo {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(8888);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true){
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",6666));
socket.send(packet);
if ("bye".equals(data)){
break;
}
}
socket.close();
}
}

循环接受消息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.bestrookie.chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
* @author : bestrookie
* @date : 16:36 2021/1/16
*/
public class UdpReceiveDemo {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(6666);
while (true){
//准备接受包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length );
socket.receive(packet);//阻塞式接受包裹
//断开连接
byte[] data = packet.getData();
String receiveData = new String(data,0,packet.getLength());
System.out.println(receiveData);
if ("bye".equals(receiveData)){
break;
}

}
socket.close();
}
}

在线聊天小例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.bestrookie.chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

/**
* @author : bestrookie
* @date : 16:56 2021/1/16
*/
public class TalkSend implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIp;
private int toPort;

public TalkSend(int fromPort, String toIp, int toPort) throws SocketException {
this.fromPort = fromPort;
this.toIp = toIp;
this.toPort = toPort;
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
}

@Override
public void run() {
while (true){
String data;
try {
data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIp,this.toPort));
socket.send(packet);
if ("bye".equals(data)){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.bestrookie.chat;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
* @author : bestrookie
* @date : 17:04 2021/1/16
*/
public class TalkReceive implements Runnable{
DatagramSocket socket ;
private int port;
private String msg;

public TalkReceive(int port,String msg) throws SocketException {
this.port = port;
this.msg = msg;
socket = new DatagramSocket(port);
}

@Override
public void run() {
while (true){
//准备接受包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length );
try {
socket.receive(packet);//阻塞式接受包裹
} catch (IOException e) {
e.printStackTrace();
}
//断开连接
byte[] data = packet.getData();
String receiveData = new String(data,0,packet.getLength());
System.out.println(msg + receiveData);
if ("bye".equals(receiveData)){
break;
}

}
socket.close();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.bestrookie.chat;

import java.net.SocketException;

/**
* @author : bestrookie
* @date : 17:34 2021/1/16
*/
public class TalkStudent {
public static void main(String[] args) throws SocketException {
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"学生")).start();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.bestrookie.chat;

import java.net.SocketException;

/**
* @author : bestrookie
* @date : 18:09 2021/1/16
*/
public class TalkTeacher {
public static void main(String[] args) throws SocketException {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}

Url

从下载地址,解析下载文件小demo,类似于爬虫

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.bestrookie.lesson04;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;

/**
* @author : bestrookie
* @date : 20:48 2021/1/16
*/
public class UrlDemo {
public static void main(String[] args) throws IOException {
//下载地址
URL url = new URL("https://www.baidu.com");
//连接到这个资源
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
File file;
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("文件名称");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();//断开连接
}
}

评论