- 软件结构
- 网络通信协议
- 应用层 http ftp 等
- 传输层 tcp udp
- tcp:连接的,可靠的传输(安全性高于udp) tcp三次握手,严格区分客户端和服务端
- udp:无连接的,不可靠的传输(传输效率高于tcp)
- 网络层 ip icmp igmp
- 数据链路层 设备驱动程序及接口 相关协议
- IP地址和端口号
- IP标识网络中唯一一台计算机,端口唯一标识一台机算计中的某个应用程序
- IP地址用32位标识 192.168.8.127 理论上地址范围0~2的32次方
- 端口号用2个字节16位组成 范围0~65535,但是0-1024端口是被系统软件用了的,无法分配,需要注意。
- 常见端口号
- 网络端口http服务器:80
- tomcat:8080
- mysql:3306
- oracle:1521
- ftp:21
- ssh:22
- TCP、Socket、ServerSocket
- TCPClient
package com.zengqifeng.rocketmqconsumer.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 8888);
OutputStream os = socket.getOutputStream();
os.write("你好服务器".getBytes());
InputStream is = socket.getInputStream();
byte[] bytes = new byte[1024];
is.read(bytes);
System.out.println(new String(bytes));
socket.close();
}
}
package com.zengqifeng.rocketmqconsumer.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
inputStream.read(bytes);
System.out.println(new String(bytes));
OutputStream os = socket.getOutputStream();
os.write("收到了,谢谢".getBytes());
socket.close();
serverSocket.close();
}
}