Day7 Java 网络编程 1
uwupu 啦啦啦啦啦

IP地址

  • 唯一定位一台网络上的计算机
  • 127.0.0.1:本机localhost
  • ip地址分为IPv4和IPv6
    • IPv4 四个字节组成;
    • IPv6:128位,8个无符号整数组成。
      • 2001:0bb2:aaaa:bbbb:cccc:dddd:eeee:2222

Java

java.net.InetAddress

这个类用来表示IP地址。

一些方法:

  1. static InetAddress getAllByName(String name),给出主机的名称,根据系统上配置的名称服务返回其IP地址数组。

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Test {
public static void main(String[] args) {
try {
//通过域名获取InetAddress对象
InetAddress address = InetAddress.getByName("www.baidu.com");
System.out.println(address);

//获取本地InetAddress对象
InetAddress address2 = InetAddress.getLocalHost();
System.out.println(address2);

//常用方法
System.out.println(address.getHostAddress());//ip地址
System.out.println(address.getHostName());//域名

} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}

端口

范围:0-65535

端口分类:

  • 公有端口 0~1023
  • 程序注册端口:1024~49151,分配用户或者程序
  • 动态、私有:49152~65535

Windows下查看进程端口

1
2
netstat -ano # 查看所有程序端口
netstat -ano|findstr "5900" # 寻找字符存在5900的行

InetSocketAddress

这个类的对象可以包含有地址+端口

1
2
3
4
5
6
7
8
9
public class Test {
public static void main(String[] args) {
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",1234);
System.out.println(socketAddress);
/*
/127.0.0.1:1234
*/
}
}

通信协议

TCP/IP协议簇

重要的协议:

  • TCP 用户传输协议
  • UDP 用户数据报协议

TCP和UDP对比

TCP:

  • 连接稳定
  • 三次握手 四次挥手

UDP:

  • 不稳定

Java上的TCP

客户端

  1. 连接服务器Socket;
  2. 发送消息。

服务端

  1. 建立服务的端口ServerSocket;
  2. 等待用户的连接Socket;
  3. 接收消息。

Client和Server示例

Client

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
public class Client {
public static void main(String[] args) {
Socket socket = null;
//IO流
OutputStream os = null;
try {
//声明地址和端口号
InetAddress server = InetAddress.getByName("127.0.0.1");
int port = 9999;
//创建一个Socket连接
socket = new Socket(server,port);
//IO流
os = socket.getOutputStream();
os.write("你好,吃黑曜石吗。".getBytes());


} catch (Exception e) {
e.printStackTrace();
}finally {
try {
os.close();
socket.close();
}catch (Exception e) {
e.printStackTrace();
}
}

}
}

Server

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
public class Server {
public static void main(String[] args) {
ServerSocket serverSocket = null;
//等待客户端连接
Socket accept = null;
//读取消息
InputStream is = null;
//管道流

ByteArrayOutputStream outputStream = null;
try {
//
serverSocket = new ServerSocket(9999);
//等待客户端连接
while (true){
accept = serverSocket.accept();
//读取消息
is = accept.getInputStream();
//管道流

outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
System.out.println(outputStream.toString());

}

} catch (Exception e) {
e.printStackTrace();
}finally {
try {
outputStream.close();
is.close();
serverSocket.close();
}catch (Exception e){
e.printStackTrace();
}

}

}
}
 评论
评论插件加载失败
正在加载评论插件
由 Hexo 驱动 & 主题 Keep
总字数 163.9k 访客数 访问量