本文整理汇总了Java中java.net.DatagramPacket.setSocketAddress方法的典型用法代码示例。如果您正苦于以下问题:Java DatagramPacket.setSocketAddress方法的具体用法?Java DatagramPacket.setSocketAddress怎么用?Java DatagramPacket.setSocketAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.DatagramPacket
的用法示例。
在下文中一共展示了DatagramPacket.setSocketAddress方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: send
import java.net.DatagramPacket; //导入方法依赖的package包/类
private void send(byte[] data) throws Exception{
if(data == null){
return;
}
if(ds == null){
return;
}
DatagramPacket dp = new DatagramPacket(data,data.length);
dp.setSocketAddress(ds.getRemoteSocketAddress());
ds.send(dp);
lastSent = System.currentTimeMillis();
this.sentPackets++;
}
示例2: echo
import java.net.DatagramPacket; //导入方法依赖的package包/类
private void echo (String s) throws IOException{
if ( (s == null) || (s.length() == 0) ){
return;
}
byte[] b = s.getBytes();
DatagramPacket echogram = new DatagramPacket(b,b.length);
echogram.setSocketAddress(new InetSocketAddress(packet.getAddress(),packet.getPort()));
datagramSocket.send(echogram);
}
示例3: onDnsRequestReceived
import java.net.DatagramPacket; //导入方法依赖的package包/类
public void onDnsRequestReceived(IPHeader ipHeader,UDPHeader udpHeader,DnsPacket dnsPacket){
if(!interceptDns(ipHeader,udpHeader,dnsPacket)){
//转发DNS
QueryState state = new QueryState();
state.ClientQueryID =dnsPacket.Header.ID;
state.QueryNanoTime = System.nanoTime();
state.ClientIP = ipHeader.getSourceIP();
state.ClientPort = udpHeader.getSourcePort();
state.RemoteIP = ipHeader.getDestinationIP();
state.RemotePort = udpHeader.getDestinationPort();
// 转换QueryID
m_QueryID++;// 增加ID
dnsPacket.Header.setID(m_QueryID);
synchronized (m_QueryArray) {
clearExpiredQueries();//清空过期的查询,减少内存开销。
m_QueryArray.put(m_QueryID, state);// 关联数据
}
InetSocketAddress remoteAddress = new InetSocketAddress(CommonMethods.ipIntToInet4Address(state.RemoteIP ), state.RemotePort);
DatagramPacket packet = new DatagramPacket(udpHeader.m_Data, udpHeader.m_Offset+8, dnsPacket.Size);
packet.setSocketAddress(remoteAddress);
try {
if(LocalVpnService.Instance.protect(m_Client)){
m_Client.send(packet);
}else {
System.err.println("VPN protect udp socket failed.");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
示例4: onDnsRequestReceived
import java.net.DatagramPacket; //导入方法依赖的package包/类
public void onDnsRequestReceived(IPHeader ipHeader, UDPHeader udpHeader, DnsPacket dnsPacket) {
if (!interceptDns(ipHeader, udpHeader, dnsPacket)) {
//转发DNS
QueryState state = new QueryState();
state.ClientQueryID = dnsPacket.Header.ID;
state.QueryNanoTime = System.nanoTime();
state.ClientIP = ipHeader.getSourceIP();
state.ClientPort = udpHeader.getSourcePort();
state.RemoteIP = ipHeader.getDestinationIP();
state.RemotePort = udpHeader.getDestinationPort();
// 转换QueryID
m_QueryID++;// 增加ID
dnsPacket.Header.setID(m_QueryID);
synchronized (m_QueryArray) {
clearExpiredQueries();//清空过期的查询,减少内存开销。
m_QueryArray.put(m_QueryID, state);// 关联数据
}
InetSocketAddress remoteAddress = new InetSocketAddress(CommonMethods.ipIntToInet4Address(state.RemoteIP), state.RemotePort);
DatagramPacket packet = new DatagramPacket(udpHeader.m_Data, udpHeader.m_Offset + 8, dnsPacket.Size);
packet.setSocketAddress(remoteAddress);
try {
if (LocalVpnService.Instance.protect(m_Client)) {
m_Client.send(packet);
} else {
System.err.println("VPN protect udp socket failed.");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}