TCP和UDP协议的区别
TCP的概念:
TCP:Transfer Control Protocol(传输控制协议),一种面向连接的、可靠的、基于字节流的运输层通信协议。
特点:面向连接、点到点的同信、可靠性高、占用系统资源多、效率低
生活案例:打电话、微信视频和微信语音
UDP的概念:
UDP:User DatagramProtocol(用户数据报协议), 一种无连接的传输层协议,提供面向事务的简单不可靠信息传输服务。
特点:(1)非面向连接,传输不可靠,可能丢失数据
(2)发送不管对方是否准备好,接收方是否收到也不确定
(3)数据报的大小限制在64K内
(4)非常简单的协议,开销小
生活案例:发送信息
基于TCP的聊天室实现
发送信息线程类:
public class Send implements Runnable{
private BufferedReader br;
//发送数据的数据流
private DataOutputStream dos;
Scanner s = new Scanner(System.in);
public Send(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public Send(Socket client){
this();
try {
dos = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
send(getMessage());
}
}
public String getMessage(){
String str="";
// try {
// str = br.readLine();
// } catch (IOException e) {
// e.printStackTrace();
// }
str = s.nextLine();
return str;
}
public void send(String str){
try {
dos.writeUTF(str);
// dos.flush();
} catch (IOException e) {
try {
if(dos!=null){
dos.close();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
}
接收信息线程类:
public class Accept implements Runnable{
private Socket client ;
public Accept(Socket client) {
this.client = client;
}
@Override
public void run() {
while (true){
try {
System.out.println(Thread.currentThread().getName()+":"+getMessage(client));
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getMessage(Socket client) throws IOException {
InputStream is = client.getInputStream();
DataInputStream di = new DataInputStream(is);
return di.readUTF();
}
}
服务端类:
public class Service {
public static ArrayList<MyChanel> list = new ArrayList<MyChanel>();
public static void main(String[] args) throws IOException {
System.out.println("-------服务端启动了-------");
ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = serverSocket.accept();
Send send = new Send(socket);
Accept accept = new Accept(socket);
new Thread(send,"服务端").start();
new Thread(accept,"服务端").start();
}
}
客户端类:
public class Client {
public static void main(String[] args) throws IOException {
Socket client = new Socket("localhost",9999);
Send send = new Send(client);
Accept accept = new Accept(client);
new Thread(send,"客户端").start();
new Thread(accept,"客户端").start();
}
}
基于TCP的聊天室实现
客户类:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
System.out.println("咨询者");
Scanner input=new Scanner(System.in);
//创建DatagramSocket对象
DatagramSocket ds=new DatagramSocket(8888);//本应用程序的端口
while(true){
//准备要发送的数据
String s=input.next();
byte[] buf=s.getBytes();
//创建数据报对象
//发送的数据, 发多少,发到哪台主机,主机程序使用的端口
DatagramPacket dp=new DatagramPacket(buf, buf.length, InetAddress.getByName("localhost"), 9999);
//发送
ds.send(dp);
/**接收数据*/
byte [] buf2=new byte[1024];
DatagramPacket dp2=new DatagramPacket(buf2, buf2.length);
// 借助String的构造方法查看
ds.receive(dp2);
String str=new String(dp2.getData(),0,dp2.getLength());
System.out.println("客服说:"+str);
if("bye".equals(s)){
break;
}
}
ds.close();
}
}
客服类:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Scanner;
public class Service {
public static void main(String[] args) throws IOException {
Scanner input=new Scanner(System.in);
System.out.println("客服人员");
DatagramSocket ds=new DatagramSocket(9999);
while(true){
//准 备接收数据
byte [] buf=new byte[1024];
//准 备数据报接收
DatagramPacket dp=new DatagramPacket(buf, buf.length);
//接收
ds.receive(dp);
//查看接收到的数据
String str=new String(dp.getData(),0,dp.getLength());
System.out.println("客户说:"+str);
String s=input.next();
/**回复数据*/
byte [] buf2=s.getBytes();
DatagramPacket dp2=new DatagramPacket(buf2, buf2.length, dp.getAddress(), dp.getPort());
ds.send(dp2);
if("bye".equals(s)){
break;
}
}
//关闭
ds.close();
}
}