-
UDP接收端
-
- 接收端启动文件
1 import java.net.DatagramSocket; 2 import java.net.SocketException; 3 4 public class Server { 5 6 public static void main(String[] args){ 7 8 try { 9 10 DatagramSocket socket = new DatagramSocket(9999); 11 ServerThread sThread = new ServerThread(socket); 12 sThread.start(); 13 14 } catch (SocketException e) { 15 16 e.printStackTrace(); 17 } 21 } 24 }
-
- 接收端线程文件
1 import java.io.IOException; 2 import java.net.DatagramPacket; 3 import java.net.DatagramSocket; 4 import java.net.InetAddress; 5 import java.net.SocketException; 6 7 public class ServerThread extends Thread{ 8 9 DatagramSocket socket = null; 10 11 public ServerThread(DatagramSocket socket) { 12 super(); 13 this.socket = socket; 14 } 15 16 @Override 17 public void run() { 18 super.run(); 19 20 try { 21 // socket = new DatagramSocket(9999); 22 23 //建立数据包 24 byte []buf = new byte[1024]; 25 DatagramPacket packet = new DatagramPacket(buf, buf.length); 26 System.out.println( "服务器开始运行...." ); 27 while(true) { 28 29 //接收客户端数据 30 socket.receive(packet); 31 32 //读取数据包的内容 33 String data = new String(buf, 0, packet.getLength()); 34 System.out.println("我是服务器,接收到客户端的消息:"+data); 35 36 //发送,先获取客户端信息 37 InetAddress address = packet.getAddress(); 38 int port = packet.getPort(); 39 byte []str = "服务器响应:你好".getBytes(); 40 DatagramPacket packet2 = new DatagramPacket(str, str.length,address,port); 41 socket.send(packet2); 42 43 } 44 45 } catch (SocketException e) { 46 e.printStackTrace(); 47 } catch (IOException e) { 48 // TODO Auto-generated catch block 49 e.printStackTrace(); 50 } 51 finally { 52 socket.close(); 53 } 54 55 } 56 }
-
UDP发送端
1 import java.io.IOException; 2 import java.net.DatagramPacket; 3 import java.net.DatagramSocket; 4 import java.net.InetAddress; 5 import java.net.SocketException; 6 import java.text.SimpleDateFormat; 7 import java.util.Date; 8 import java.util.Scanner; 9 10 public class Client { 11 12 public static void main(String[] args) throws IOException{ 13 14 DatagramSocket socket = null; 15 16 try { 17 18 socket = new DatagramSocket();//udp不用端口号 19 20 while(true) { 21 //时间 22 Date myDate = new Date(); 23 SimpleDateFormat dFormat = new SimpleDateFormat("HH:mm:ss"); 24 //发送信息 25 InetAddress address = InetAddress.getLocalHost();//取得服务器地址 26 int port = 9999; 27 28 Scanner input = new Scanner(System.in);//改为输入发送 29 String inContent ="\r\n" + dFormat.format(myDate) + "\r\n" +"黄哥哥说:"+ input.next(); 30 31 byte []str = inContent.getBytes(); 32 DatagramPacket packet2 = new DatagramPacket(str, str.length,address,port);//InetAddress.getByName( "192.168.4.198" ) 33 socket.send(packet2); 34 35 //接收信息 36 //建立数据报包 37 byte []buf = new byte[1024]; 38 DatagramPacket packet = new DatagramPacket(buf, buf.length); 39 40 //接收服务器信息 41 socket.receive(packet); 42 43 //读取数据包的内容 44 String data = new String(buf, 0, packet.getLength()); 45 System.out.println(data); 46 47 } 48 } catch (SocketException e) { 49 // TODO Auto-generated catch block 50 e.printStackTrace(); 51 }finally { 52 //关闭 53 socket.close(); 54 } 55 56 } 57 }
-
运行效果
- 发送端发送消息,接收端回应
2.接收端收到的消息
原文地址:https://www.cnblogs.com/ynhwl/p/9434219.html
时间: 2024-10-15 15:58:52