Created
March 30, 2017 13:00
-
-
Save liuzhengyang/33feb315b96de8ed2f43e8de36bea58c to your computer and use it in GitHub Desktop.
udp echo client with raw datagram api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.lzy.web.net; | |
import java.io.IOException; | |
import java.net.DatagramPacket; | |
import java.net.DatagramSocket; | |
import java.net.InetSocketAddress; | |
import java.net.SocketException; | |
/** | |
* Description: | |
* | |
* @author liuzhengyang | |
* @version 1.0 | |
* @since 2017-03-30 | |
*/ | |
public class UDPTest { | |
public static void main(String[] args) { | |
InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 8089); | |
InetSocketAddress localAddress = new InetSocketAddress(10093); | |
try { | |
DatagramSocket datagramSocket = new DatagramSocket(localAddress); | |
DatagramPacket datagramPacket = new DatagramPacket("hello".getBytes("UTF-8"), 0, 5, inetSocketAddress); | |
datagramSocket.send(datagramPacket); | |
datagramSocket.receive(datagramPacket); | |
byte[] data = datagramPacket.getData(); | |
System.out.println(new String(data, "UTF-8")); | |
} catch (SocketException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment