mirror of
https://github.com/Dichgrem/Java.git
synced 2026-02-05 04:21:56 -05:00
update:demo8
This commit is contained in:
26
com/study/demo8/UDPSender.java
Normal file
26
com/study/demo8/UDPSender.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.study.demo8;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
|
||||
public class UDPSender {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
|
||||
String message = "Hello UDP";
|
||||
byte[] data = message.getBytes();
|
||||
|
||||
InetAddress address = InetAddress.getByName("localhost");
|
||||
DatagramPacket packet = new DatagramPacket(data, data.length, address, 9999);
|
||||
|
||||
socket.send(packet);
|
||||
System.out.println("Sent message to receiver.");
|
||||
|
||||
socket.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user