TCP(面向链接) 在正式收发数据前,必须和对方建立可靠的链接
特点:安全(面向链接,保证对象一定能收到数据),传输数据大小无限制
缺点:效率低(因为有三次握手)
UDP和TCP一样是面向无连接的传输层协议,提供面向事务的简单不可靠信息传送服务
特点:无连接,安全,可靠性不高,效率高
发送方->接受方
1.创建DatagramSocket
2.准备要发送的数据
3.将发送的数据放到包裹里面
4.通过DatagramSocket将包裹发送出去
5.关闭Socket
发送方
1.不需要地址和端口号
DatagramSocket socket = new DatagramSocket();
String str = "好开心啊";
DatagramPack pack = new DatagramPacket(str.getBytes(),str.length,127.0.0.1,2333)
Socket.send(packet);
接收方
DatagramSocket socket = new DatagramSocket(2333);
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf,length);
//阻塞程序向下进行
socket.receve(packet);
String str = new String(packet.getData(),0,pack.length);
System.out.println(str);
利用udp实现互相聊天
服务端
package com.qf.demo3;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.SocketException;
import java.util.Scanner;
public class Recive {
public static void main(String[] args) {
// 1 接收方的socket需要端口号
DatagramSocket socket = null;
try {
socket = new DatagramSocket(5577);
Scanner in = new Scanner(System.in);
while (true) {
// 接收
byte[] buf = new byte[1024];
DatagramPacket p = new DatagramPacket(buf, buf.length);
socket.receive(p);
String string = new String(p.getData(), 0, p.getLength());
System.out.println("发送方发送的" + string + " " + p.getAddress().getHostAddress());
// 回复数据
System.out.println("包裹端口:"+p.getPort());
System.out.println("请输入回复内容:");
String string2 = in.next();
DatagramPacket packet = new DatagramPacket(string2.getBytes(), string2.getBytes().length,
InetAddress.getLocalHost(), p.getPort());
socket.send(packet);
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
}
}
}
客户端
package com.qf.demo3;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
/**
* 1 发送方: socket 没有参数的 接收方 : socket 是有端口号
* 2 send 的 packet 是带有 byte lenth InetAddress port receive的 packet 是带有 byte
* lenth
* 作业: 实现循环的发送和接收 数据由控制台输入
*/
public class Send {
public static void main(String[] args) {
// 1 发送方的快递点不需要参数
DatagramSocket socket = null;
Scanner in = new Scanner(System.in);
try {
socket = new DatagramSocket();
while (true) {
// 2
System.out.println("请输入内容:");
String string = in.next();
// 3 将数据放到包裹里面
DatagramPacket packet = new DatagramPacket(string.getBytes(), string.getBytes().length,
InetAddress.getLocalHost(), 5577);
// 4 发送
socket.send(packet);
// 接收 接收端的 回复
byte[] buf = new byte[1024 * 63];
DatagramPacket p = new DatagramPacket(buf, buf.length);
socket.receive(p);
String string2 = new String(p.getData(), 0, p.getLength());
System.out.println("接收方回复" + string2);
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
}
}
}