JavaSE socket 基于UDP Server/Client的实现
程序员文章站
2022-06-14 10:18:13
...
上一篇演示了一个简单的基于TCP/IP 双向的控制台聊天程序,
本篇基于Java的UDP实现类
DatagramSocket / DatagramPacket
展示下如何用这两个类完成一个简单的通信。由于时间仓促,读和写两个并发线程的编写并没有调试成功,欢迎完成的小伙伴留言 附上代码 或者 讲出为什么像示例这样做不行的原因,灰常感谢。
Server 端
package com.test.socket;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Scanner;
/**
* Created by szh on 2017/9/12.
*/
public class UDPSocketServer extends Thread {
public static DatagramSocket datagramSocket = null;
public static InetAddress senderAddr = null;
public static int senderPort = -1;
static {
try {
datagramSocket = new DatagramSocket(new InetSocketAddress(InetAddress.getByAddress(new byte[]{127, 0, 0, 1}), 8080));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true) {
byte[] buffer = new byte[8192];
DatagramPacket packet = new DatagramPacket(buffer, 8192);
datagramSocket.receive(packet);
String msg = new String(packet.getData());
senderAddr = packet.getAddress();
senderPort = packet.getPort();
System.out.println("Sender :\n addr -> " + senderAddr + "\n port -> " + senderPort + "\nMsg : " + msg);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
datagramSocket.close();
}
}
public static void main(String[] args) {
try {
//将Server 绑定在本地的127.0.0.1 8080端口
DatagramSocket datagramSocket = new DatagramSocket(new InetSocketAddress(InetAddress.getByAddress(new byte[]{127, 0, 0, 1}), 41002));
// //创建一个读取客户端发送消息的进程 并启动
// new UDPSocketServer().start();
//
// Scanner scanner = new Scanner(System.in);
// String line = null;
// while (!(line = scanner.nextLine()).equals("exit")) {
// if (senderPort != -1 && senderAddr != null) {
// byte[] byteMsg = line.getBytes();
// datagramSocket.send(new DatagramPacket(byteMsg, 0, byteMsg.length, senderAddr, senderPort));
// }
// }
while (true) {
byte[] buffer = new byte[8192];
DatagramPacket packet = new DatagramPacket(buffer, 8192);
datagramSocket.receive(packet);
String msg = new String(packet.getData());
senderAddr = packet.getAddress();
senderPort = packet.getPort();
System.out.println("Sender :\n addr -> " + senderAddr + "\n -> port " + senderPort + "\nMsg : " + msg);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
datagramSocket.close();
}
}
}
Client 端
package com.test.socket;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
/**
* Created by szh on 2017/9/12.
*/
public class UDPSocketClient {
public static InetAddress serverAddr = null;
public static int serverPort = -1;
static {
try {
serverAddr = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});
serverPort = 41002;
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
DatagramSocket datagramSocket = null;
try {
datagramSocket = new DatagramSocket(42000+ (int)Math.floor(Math.random()*1000));
Scanner scanner = new Scanner(System.in);
String line = null;
while (!(line = scanner.nextLine()).equals("exit")) {
byte[] byteMsg = line.getBytes();
if (serverPort != -1 && serverAddr != null){
DatagramPacket sendPacket = new DatagramPacket(new byte[0], 0, serverAddr, serverPort);
sendPacket.setData(byteMsg);
System.out.println("发送消息");
datagramSocket.send(sendPacket);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
datagramSocket.close();
}
}
}
测试截图:
1.
2.
3.
推荐阅读
-
python实现的udp协议Server和Client代码实例
-
【Netty】Socket 编程(C/S):基于Netty的Server、Client示例(少注释)
-
python实现的udp协议Server和Client代码实例
-
C#基于Socket的TCP通讯例程 Server Client
-
基于TCP的socket编程实现client和server通信
-
JavaSE socket 基于UDP Server/Client的实现
-
linux网络编程之用socket实现简单客户端和服务端的通信(基于UDP)
-
基于DWR的点对点聊天实现 server---client
-
基于DWR的点对点聊天实现 server---client
-
python实现的udp协议Server和Client代码实例