TCP和UDP都是运输层的网络协议
UPD和TCP的区别
?
区别 | UDP | TCP | 是否连接 | 无连接,不连接就可以直接发送数据,所以不可靠 | 经过三次握手,才建立连接,面向连接 | 是否可靠 | 不可靠传输,不使用流量控制和拥塞控制 | 可靠传输,使用流量控制和拥塞控制 | 连接对象个数 | 支持一对一,一对多,多对一和多对多交互通信 | 只能是一对一通信 | 传输方式 | 面向报文 | 面向字节流 | 首部开销 | 首部开销小,仅8字节 | 首部最小20字节,最大60字节 | 适用场景 | 适用于实时应用(IP电话、视频会议、直播等) | 适用于要求可靠传输的应用,例如文件传输 |
使用TCP实现聊天
服务器端
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class tcpserver {
public static void main(String[] args) throws IOException {
//有一个地址
ServerSocket serverSocket = new ServerSocket(999);
//等待客户端连接
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
//管道
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
while((len = inputStream.read(bytes))!=-1){
byteArrayOutputStream.write(bytes,0,len);
}
System.out.println(byteArrayOutputStream.toString());
}
}
客户端
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class tcpclient {
public static void main(String[] args) throws IOException {
//知道服务器端地址
InetAddress name = InetAddress.getByName("127.0.0.1");
int port = 999;
//创建socket连接
Socket socket = new Socket(name,port);
//发送消息
OutputStream outputStream = socket.getOutputStream();
outputStream.write("您好 我在访问".getBytes());
}
}
TCP实现文件上传
服务器端:
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class tcpFileServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(999);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("receive.png");
byte[] bytes = new byte[1024];
int len;
while((len = inputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
}
}
客户端:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class tcpFile {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),999);
OutputStream outputStream = socket.getOutputStream();
FileInputStream file = new FileInputStream(new File("/Users/mac/Desktop/springboot/miaokill/Net/liuji.png"));
byte[] bytes = new byte[1024];
int len;
while((len = file.read(bytes))!=-1){
outputStream.write(bytes);
}
}
}
UDP实现聊天
发送端
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
public class UdpChatSend {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(0000);
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(System.in));
while (true) {
String data = bufferedReader.readLine();
DatagramPacket packet = new DatagramPacket(data.getBytes(), data.getBytes().length
, new InetSocketAddress("127.0.0.1", 9898));
socket.send(packet);
if(data.equals("bye")){
break;
}
}
socket.close();
}
}
接收端
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;
public class UdpChatRecive {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(9898);
while (true) {
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0,bytes.length
);
socket.receive(packet);
byte[] data = packet.getData();
String receive = new String(data,0,data.length);
System.out.println(receive);
if (receive.equals("bye")) {
break;
}
}
socket.close();
}
}
但是这样只能实现一个人发,一个人收,如何实现双方互相发送,就为两者开辟两条线程。
两人互相发送
接收端:
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;
public class ThreadUdpReceive implements Runnable {
DatagramSocket socket = null;
private int port;
private String fromMsg;
public ThreadUdpReceive(int port, String fromMsg) throws SocketException {
this.fromMsg = fromMsg;
this.port = port;
socket = new DatagramSocket(port);
}
@Override
public void run() {
while (true) {
try {
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length
);
socket.receive(packet);
byte[] data = packet.getData();
String receive = new String(data, 0, data.length);
System.out.println(fromMsg + ":" + receive);
if (receive.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
发送端:
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;
public class ThreadUdpSend implements Runnable {
DatagramSocket socket = null;
BufferedReader bufferedReader = null;
private int fromport;
private int port;
private String myip;
public ThreadUdpSend(int fromport, int port, String myip) throws SocketException {
this.fromport = fromport;
this.port = port;
this.myip = myip;
socket = new DatagramSocket();
bufferedReader =
new BufferedReader(new InputStreamReader(System.in));
}
@Override
public void run() {
while (true) {
try {
String data = bufferedReader.readLine();
DatagramPacket packet = new DatagramPacket(data.getBytes(), data.getBytes().length
, new InetSocketAddress(this.myip, this.port));
socket.send(packet);
if (data.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
测试:
import java.net.SocketException;
public class StudentSend {
public static void main(String[] args) throws SocketException {
new Thread(
new ThreadUdpSend(7777,9999,"localhost")).start();
new Thread(new ThreadUdpReceive(8888, "老师")).start();
}
}
import java.net.SocketException;
public class TeacherSend {
public static void main(String[] args) throws SocketException {
new Thread(new ThreadUdpSend(1231, 8888, "localhost")).start();
new Thread(
new ThreadUdpReceive(9999, "学生")
).start();
}
}
|