Socket概述
UDP
UDP数据传输流程:
以下是代码实现:
因为UDP是无连接的不可靠传输,所以接收方需要在发送方发送数据之前就启动,否则会接收不到数据。也就是说必须先运行UDPSocketReceive再运行UDPSocketSend。控制台打印如下:
发送方:
Sender Start...
接受方:
Receiver Start...
address:/127.0.0.1---port:61177---content:God bless you!
聊天实现:
package Socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class SocketFemale {
public static void main(String[] args) {
try {
new Thread(new Runnable() {
@Override
public void run(){
try {
receive();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
send();
} catch (IOException e) {
e.printStackTrace();
}
}
//接收消息方法
private static void receive() throws IOException {
System.out.println("SocketFemale Receiver Start...");
//1.创建udp的socket服务,并声明端口号
DatagramSocket dsReceive = new DatagramSocket(6666);
//无限循环,一直处于接收状态
while (true) {
//2.创建接收数据的数据包
byte bytes[] = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
//3.将数据接收到数据包中
dsReceive.receive(dp);
//4.解析数据
String content = new String(dp.getData(), 0, dp.getLength());
System.out.println("SocketFemale Receive:" + content);
}
}
private static void send() throws IOException {
//1.创建socket服务
DatagramSocket dsSend = new DatagramSocket();
//将键盘输入的信息转换成输入流再放入到缓冲区
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line=br.readLine())!=null){
//2.封装数据
byte[] bytes = line.getBytes();
//地址
InetAddress address =InetAddress.getByName("127.0.0.1");
//参数:数据、长度、地址、端口
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,7777);
//3.发送数据包
dsSend.send(dp);
}
//4.关闭socket服务
dsSend.close();
}
}
package Socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class SocketMale {
public static void main(String[] args){
try {
new Thread(new Runnable() {
@Override
public void run() {
try {
receive();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
send();
} catch (IOException e) {
e.printStackTrace();
}
}
//接收消息方法
private static void receive() throws IOException {
System.out.println("SocketMale Receiver Start...");
//1.创建udp的socket服务,并声明端口号
DatagramSocket dsReceive = new DatagramSocket(7777);
//无限循环,一直处于接收状态
while (true) {
//2.创建接收数据的数据包
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
//3.将数据接收到数据包中
dsReceive.receive(dp);
//4.解析数据
String content = new String(dp.getData(), 0, dp.getLength());
System.out.println("SocketMale Receive:" + content);
}
//关闭服务
// ds.close;
}
private static void send() throws IOException {
//1.创建socket服务
DatagramSocket dsSend = new DatagramSocket();
//将键盘输入的信息转换成输入流再放入到缓冲区
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line=br.readLine())!=null){
//2.封装数据
byte[] bytes = line.getBytes();
//地址
InetAddress address =InetAddress.getByName("127.0.0.1");
//参数:数据、长度、地址、端口
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,6666);
//3.发送数据包
dsSend.send(dp);
}
//4.关闭socket服务
dsSend.close();
}
}
TCP
TCP聊天实现:
一个服务端可以同时和多个客户端进行通信。如何区分不同客户端?服务端首先通过accept获取到客户端Socket,然后通过客户端的Socket获取的流进行通讯,服务端得以区分每个客户端。
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦