TCP及UDP协议
TCP/IP协议族
七层网络模型及其对应的TCP/IP协议
TCP与UDP对比
TCP(Transmission Control Protocol)和UDP(User Datagram Protocol)协议属于传输层协议。其中TCP提供IP环境下的数据可靠传输,提供可靠的字节流服务 ,它提供的服务包括**数据流传送、可靠性、**有效流控、全双工操作和多路复用。通过面向连接、端到端和可靠的数据包发送;而UDP则不为IP提供可靠性、流控或差错恢复功能。
TCP对应的是可靠性要求高的应用,而UDP对应的则是可靠性要求低、传输经济的应用。
TCP类似打电话 ,双方需要建立稳定的连接;UDP则是发短信、发邮件,不需要对方和自己建立稳定连接。
Java类实现
Java关于网络的类都封装在java.net中。
Java实现TCP协议
java服务器端:
-
建立服务连接地址 -
读取客户端消息
- 阻塞式监听
- 获取客户端输入
- 使用管道流(或普通输出流)写入客户端消息
-
关闭资源 -
代码示例
public static void tcpMsg() {
try {
ServerSocket socket = new ServerSocket(8888);
Socket accept = socket.accept();
InputStream in = accept.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[124];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
String s = new String(buffer, 0, buffer.length);
System.out.println(s);
System.out.println(out.toString());
}
out.close();
in.close();
accept.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void tcpFile() {
try {
ServerSocket socket = new ServerSocket(8989);
Socket accept = socket.accept();
FileOutputStream out = new FileOutputStream("cc.md");
InputStream input = accept.getInputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
OutputStream output = accept.getOutputStream();
output.write("文件接收完毕".getBytes(StandardCharsets.UTF_8));
output.close();
input.close();
out.close();
accept.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
java客户端:
- 获取服务器连接地址
- 发送消息
- 关闭资源
- 代码示例
public static void tcpMsg() {
try {
InetAddress ip = InetAddress.getByName("127.0.0.1");
int port = 8888;
Socket socket = new Socket(ip, port);
OutputStream out = socket.getOutputStream();
out.write("欢迎来到世界".getBytes());
out.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void tcpFile() {
try {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 8989);
FileInputStream input = new FileInputStream("D:\\javablog\\Java反射.md");
OutputStream out = socket.getOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) != -1) {
out.write(buffer, 0, buffer.length);
}
socket.shutdownOutput();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
InputStream in = socket.getInputStream();
byte[] buffer1 = new byte[1024];
while ((len=in.read(buffer1))!=-1){
outputStream.write(buffer1,0,buffer1.length);
System.out.println(outputStream.toString());
}
in.close();
outputStream.close();
out.close();
input.close();
socket.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Java实现UDP协议
-
创建socket连接 -
创建包 -
发送包 -
关闭资源 -
代码示例 public static void udpSendmsg() {
try {
DatagramSocket socket = new DatagramSocket();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = null;
while (true) {
s = reader.readLine();
byte[] msg = s.getBytes(StandardCharsets.UTF_8);
InetAddress ip = InetAddress.getByName("127.0.0.1");
int port = 8888;
DatagramPacket packet = new DatagramPacket(msg, 0, msg.length, ip, port);
socket.send(packet);
if (s.equals("bye bye")) break;
}
reader.close();
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
-
创建socket连接 -
接收包 -
关闭资源 -
代码示例 public static void udpRecieveMsg() {
try {
int port = 8888;
DatagramSocket socket = new DatagramSocket(port);
while (true) {
byte[] bufer = new byte[1024];
DatagramPacket packet = new DatagramPacket(bufer, 0, bufer.length);
socket.receive(packet);
String data = new String(packet.getData(), 0, packet.getLength());
System.out.println(data);
if (data.equals("bye bye")) {
break;
}
}
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
|