TCP与UDP协议
1、UDP
UDP是无连接通信协议,即在数据传输时,数据的发送端和接收端不建立逻辑连接。
由于使用UDP协议消耗资源小,通信效率高,所以通常都会用于音频、视频和普通数据的传输例如视频会议都使用UDP
协议,因为这种情况即使偶尔丢失一两个数据包,也不会对接收结果产生太大影响。但是在使用UDP协议传送数据时,
由于UDP的面向无连接性,不能保证数据的完整性,因此在传输重要数据时不建议使用UDP协议。
1.1 、UDP程序交互的流程
*发送端
1、创建DatagramSocket对象
2、创建DatagramPacket对象,并封装数据
3、发送数据
4、释放流资源
*接收端
1、创建DatagramSocket对象
2、创建DatagramPacket对象
3、接收数据存储到DatagramPacket对象中
4、获取DatagramPacket对象的内容
5、释放流资源
1.2、简单示例
package com.protocol;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpProtocol {
public static void main(String[] args) {
byte[] bytes = "suhua nice".getBytes();
int port = 10086;
int length = bytes.length;
String host = "127.0.0.1";
try {
SendUdpProtocol(bytes , length , port , host);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void SendUdpProtocol(byte buf[] , int length, int port , String host) throws Exception {
DatagramSocket datagramSocket = new DatagramSocket();
DatagramPacket datagramPacket = new DatagramPacket(buf, length, InetAddress.getByName(host),port);
datagramSocket.send(datagramPacket);
datagramSocket.close();
}
static class ReceiveUdp{
public static void main(String[] args) {
byte[] bytes = new byte[1024];
int port = 10086;
int length = bytes.length;
try {
ReceiveUdpProtocol(bytes ,length , port);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void ReceiveUdpProtocol(byte buf[] ,int length , int port) throws Exception{
DatagramSocket datagramSocket=new DatagramSocket(port);
DatagramPacket datagramPacket=new DatagramPacket(buf, length);
datagramSocket.receive(datagramPacket);
String ip=datagramPacket.getAddress().getHostAddress();
String data=new String(datagramPacket.getData(),0,datagramPacket.getLength());
int count = datagramPacket.getLength();
System.out.println("IP地址:"+ip
+"\n发来了多少数据:"+count
+"\n发送过来的数据:"+data);
datagramSocket.close();
}
}
}
2、TCP
TCP协议是面向连接的通信协议,即在传输数据前先在发送端和接收端建立逻辑连接,
然后再传输数据,它提供了两台计算机之间可靠无差错的数据传输。
每次连接的创建都需要经过“三次握手”。
*第一次握手,客户端向服务器端发出连接请求,等待服务器确认,
*第二次握手,服务器端向客户端回送一个响应,通知客户端收到了连接请求,
*第三次握手,客户端再次向服务器端发送确认信息,确认连接。
2.1、TCP程序交互的流程
*客户端
1、创建客户端的Socket对象
2、获取Socket的输出流对象
3、写数据给服务器
4、获取Socket的输入流对象
5、使用输入流,读反馈信息
6、关闭流资源
*服务端
1、创建服务器端ServerSocket对象,指定服务器端端口号
2、开启服务器,等待着客户端Socket对象的连接,如有客户端连接,返回客户端的Socket对象
3、通过客户端的Socket对象,获取客户端的输入流,为了实现获取客户端发来的数据
4、通过客户端的输入流,获取流中的数据
5、通过客户端的Socket对象,获取客户端的输出流,为了实现给客户端反馈信息
6、通过客户端的输出流,写数据到流中
7、关闭流资源
2.2、简单示例
package com.protocol;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpProtocol {
public static void main(String[] args) throws IOException {
int port = 10086;
SendTcpProtocol(port);
}
private static void SendTcpProtocol(int port) throws IOException{
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
OutputStream outputStream = socket.getOutputStream();
outputStream.write("我就是通过TCP协议发送的数据,我叼不叼!".getBytes());
outputStream.close();
socket.close();
}
static class ReceiveTcp {
public static void main(String[] args) throws IOException{
String host = "127.0.0.1";
int port = 10086;
ReceiveTcpProtocol(host , port);
}
private static void ReceiveTcpProtocol(String host , int port) throws IOException{
Socket socket = new Socket(host, port);
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int length = inputStream.read(buffer);
System.out.println("数据:"+new String(buffer, 0 , length) );
inputStream.close();
socket.close();
}
}
static class TcpFileSend{
public static void main(String[] args) throws IOException{
String fileUrl = "E:\\用于输入字节的JPG(空).jpg";
int port = 10086;
fileSend(fileUrl , port);
}
private static void fileSend(String fileUrl , int port) throws IOException {
ServerSocket serverSocket = new ServerSocket(port);
Socket clientSocket = serverSocket.accept();
InetAddress ipObject = clientSocket.getInetAddress();
String ip = ipObject.getHostAddress();
System.out.println("我是服务端IP地址:" + ip);
InputStream inputStream = clientSocket.getInputStream();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(fileUrl));
byte[] bytes = new byte[1024];
int length = -1;
while((length = inputStream.read(bytes)) != -1){
bufferedOutputStream.write(bytes, 0, length);
}
OutputStream out = clientSocket.getOutputStream();
out.write("图片上传成功".getBytes());
out.close();
bufferedOutputStream.close();
inputStream.close();
clientSocket.close();
}
}
static class TcpFileReceive{
public static void main(String[] args) throws IOException {
String host = "127.0.0.1";
int port = 10086;
String fileUrl = "E:\\img\\黑色使者-亚索.jpg";
fileReceive(host , port , fileUrl);
}
private static void fileReceive(String host , int port , String fileUrl) throws IOException{
Socket socket = new Socket(host, port);
OutputStream outputStream = socket.getOutputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(fileUrl));
byte[] bytes = new byte[1024];
int length = -1;
while ((length = bufferedInputStream.read(bytes)) != -1){
outputStream.write(bytes, 0, length);
}
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
byte[] info = new byte[1024];
int count = inputStream.read(info);
System.out.println(new String(info, 0, count) );
inputStream.close();
bufferedInputStream.close();
outputStream.close();
socket.close();
}
private static void filesReceive(String host , int port , String fileUrl) throws IOException{
ServerSocket serverSocket = new ServerSocket(port);
while(true){
final Socket clientSocket = serverSocket.accept();
new Thread(){
public void run() {
try{
InetAddress inetAddress = clientSocket.getInetAddress();
String ip = inetAddress.getHostAddress();
System.out.println("我是服务端IP:" + ip);
InputStream inputStream = clientSocket.getInputStream();
BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream("D:\\"+ip+"("+System.currentTimeMillis()+").jpg"));
byte[] bytes = new byte[1024];
int len = -1;
while((len = inputStream.read(bytes)) != -1){
fileOut.write(bytes, 0, len);
}
OutputStream out = clientSocket.getOutputStream();
out.write("图片上传成功".getBytes());
out.close();
fileOut.close();
inputStream.close();
clientSocket.close();
} catch(IOException e){
e.printStackTrace();
}
};
}.start();
}
}
}
}
更详细请看原文作者(点我)
|