其实我是因为flink没有封装好的sink,所以自定义了sink来调用这个类。。。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import java.io.IOException; import java.net.*;
public class SocketUDPClient {
private InetAddress ip; private int port; private DatagramSocket socket;
public SocketUDPClient(String ip, int port) throws UnknownHostException, SocketException { this.ip = InetAddress.getByName(ip); this.port = port;
socket = new DatagramSocket();
}
public void send(String data){ try { byte[] outputData=data.getBytes(); DatagramPacket outputPacket=new DatagramPacket(outputData, outputData.length, ip, port); socket.send(outputPacket); } catch (IOException ex) { } }
public void close(){ if (socket != null) socket.close(); }
}
|
方法变量都是封装好的,直接调用完事