Java中级_网络编程

OSI与TCP/IP体系模型

OSI体系结构

OSI体系结构
7.应用层
6.表示层
5.会话层
4.传输层
3.网络层
2.数据链路层
1.物理层

TCP/IP体系结构

TCP/IP体系结构
4.应用层(协议:Telnet,FTP,SMTP…)
3.传输层(TCP/UDP)
2.网际层IP
1.网络接口层

TCP

Tranfer Control Protocol 的简称;

是一种面向连接的保证可靠传输的协议.

UDP

User Datagram Protocol 的简称

是一种无连接的协议.

TCP/UDP比较:

UDP:

  1. 每个数据报中都给出了完整的地址信息,因此无需要建立发送方和接收方的连接。
  2. UDP传输数据时是有大小限制的,每个被传输的数据报必须限定在64KB之内。
  3. UDP是一个不可靠的协议,发送方所发送的数据报并不一定以相同的次序到达接收方。

TCP:

  1. 面向连接的协议,在socket之间进行数据传输之前必然要建立连接,所以在TCP中需要连接时间。
  2. TCP传输数据大小没有限制,一旦连接建立起来,双方的socket就可以按统一的格式传输大的数据。
  3. TCP是一个可靠的协议,它确保接收方完全正确地获取发送方所发送的全部数据。

TCP/UDP应用:

TCP:

远程连接,文件传输…

TCP传输没有UDP快

UDP:

音视频传输…

Socket

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
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 9999);
//打开字节流输出流
OutputStream os = s.getOutputStream();

//封装字节流-数据流
DataOutputStream dos = new DataOutputStream(os);

//读取控制台数据
Scanner scanner = new Scanner(System.in);
String data = scanner.next();

//发送数据到服务端
dos.writeUTF(data);
os.close();
s.close();

// // 启动发送信息线程
// new SendThread(s).start();
// // 启动接收信息线程
// new RecieveThread(s).start();

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

ServerSocket

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
public static void main(String[] args) {
try {
// 服务器端打开端口9999
ServerSocket ss = new ServerSocket(9999);

// 在9999端口监听
System.out.println("正在监听9999");
Socket s = ss.accept();

System.out.println("请求连接:" + s);

// 打开字节流输入流
InputStream is = s.getInputStream();

//封装字节流-数据流
DataInputStream dis = new DataInputStream(is);

// 读取客户端发送过来的数据
String readUTF = dis.readUTF();
System.out.println("客户端:"+readUTF);

is.close();
s.close();
ss.close();

// // 启动发送信息线程
// new SendThread(s).start();
// // 启动接收信息线程
// new RecieveThread(s).start();

} 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
/**
*
* @ClassName: SendThread
* @Description: 发送信息线程
* @author zhouhongwei
*
*/
public class SendThread extends Thread {
private Socket s;

public SendThread(Socket s) {
this.s = s;
}

public void run() {
try {
// 打开字节流输出流
OutputStream os = s.getOutputStream();
// 封装字节流-数据流
DataOutputStream dos = new DataOutputStream(os);

while (true) {
// 读取控制台数据
Scanner scanner = new Scanner(System.in);
String data = scanner.next();

// 发送数据到服务端
dos.writeUTF(data);
}
} catch (IOException e) {
// TODO Auto-generated catch block
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
/**
*
* @ClassName: RecieveThread
* @Description: 接收信息线程
* @author zhouhongwei
*
*/
public class RecieveThread extends Thread {

private Socket s;

public RecieveThread(Socket s) {
this.s = s;
}

public void run() {
try {
// 打开字节流输入流
InputStream is = s.getInputStream();
// 封装字节流-数据流
DataInputStream dis = new DataInputStream(is);

while (true) {
// 读取客户端发送过来的数据
String readUTF = dis.readUTF();
System.out.println(readUTF);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

服务器端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main(String[] args) {
try {
// 服务器端打开端口9999
ServerSocket ss = new ServerSocket(9999);

// 在9999端口监听
System.out.println("正在监听9999");
Socket s = ss.accept();

// 启动发送信息线程
new SendThread(s).start();
// 启动接收信息线程
new RecieveThread(s).start();

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

客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 9999);
// 启动发送信息线程
new SendThread(s).start();
// 启动接收信息线程
new RecieveThread(s).start();

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

说一下,网络编程,有 NIO,也有Netty框架

小唠嗑:

本章到这里就结束了,谢谢耐心看到这里的各位Boss,如果觉得有哪里说的不好的地方,还请高抬贵手多多原谅,不惜指教。

最后,谢谢!

---本文结束感谢您的阅读!---