本文整理汇总了Java中java.net.DatagramPacket.getAddress方法的典型用法代码示例。如果您正苦于以下问题:Java DatagramPacket.getAddress方法的具体用法?Java DatagramPacket.getAddress怎么用?Java DatagramPacket.getAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.DatagramPacket
的用法示例。
在下文中一共展示了DatagramPacket.getAddress方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import java.net.DatagramPacket; //导入方法依赖的package包/类
@Override
public void run() {
while (true) {
DatagramPacket packet = new DatagramPacket(receive_str_byte, receive_str_byte.length);
try {
client.getDs().receive(packet);
InetAddress address = packet.getAddress();
int port = packet.getPort();
int len = packet.getLength();
byte[] data = packet.getData();
System.out.printf("Request from %s Port %d:%n%s%n", address, port,
new String(data, 0, len));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
示例2: ImmutableDatagramPacket
import java.net.DatagramPacket; //导入方法依赖的package包/类
public ImmutableDatagramPacket(DatagramPacket p) {
address = p.getAddress();
port = p.getPort();
byte[] temp_data = p.getData();
int length = p.getLength();
byte[] b_temp = new byte[length];
if (length < temp_data.length) {
b_temp = new byte[length];
for (int i = 0; i < length; i++) {
b_temp[i] = temp_data[i];
}
} else if (length > temp_data.length) { //should not happen.
throw new RuntimeException(
"Length is bigger then message, packet is garbage.");
}
data = b_temp;
}
示例3: receivePackets
import java.net.DatagramPacket; //导入方法依赖的package包/类
private Packet[] receivePackets(int timeout) throws IOException {
mSocket.setSoTimeout(timeout);
ArrayList<Packet> responses = new ArrayList<>();
ArrayList<DatagramPacket> datagramPackets = new ArrayList<>();
try {
while (true) {
byte[] buffer = new byte[65536];
DatagramPacket datagramPacket = new DatagramPacket(buffer, 65536);
mSocket.receive(datagramPacket);
datagramPackets.add(datagramPacket);
}
} catch (Exception e) {
Log.w(LOG_TAG, "Exception: " + e.getMessage());
}
for (DatagramPacket p : datagramPackets) {
String data = new String(p.getData(), 0, p.getLength());
InetAddress address = p.getAddress();
Log.d(LOG_TAG, String.format("Received response from %s: %s", address.getHostAddress(), data));
Packet response = Utils.deserializePacket(data, mKeyChain);
// Filter out packets sent by us
if (response.cid != null && response.cid != AppPacket.CID)
responses.add(response);
}
return responses.toArray(new Packet[0]);
}
示例4: receive
import java.net.DatagramPacket; //导入方法依赖的package包/类
public void receive() throws InterruptedException, IOException {
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
final DataOutputStream dataOut = new DataOutputStream(byteOut);
isRunning.set(true);
int packetsReceived = 0;
while(isRunning.get()) {
if (Thread.interrupted()) {
settings.socket.close();
isRunning.set(false);
throw new InterruptedException();
}
byte data[] = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
settings.socket.receive(packet);
packetsReceived++;
if (callback != null) {
callback.onReceive(packet);
}
if (packetsReceived >= settings.packets) {
isRunning.set(false);
}
if (settings.sendResponse)
{
byteOut.reset();
if (callback != null && callback.onSend(dataOut, packetsReceived)) {
final byte[] dataToSend = byteOut.toByteArray();
DatagramPacket dp = new DatagramPacket(dataToSend, dataToSend.length, packet.getAddress(), packet.getPort());
settings.socket.send(dp);
}
}
}
}
示例5: send
import java.net.DatagramPacket; //导入方法依赖的package包/类
public void send(DatagramPacket p) throws IOException {
TCPTun tun=null;
if(client){
tun=capEnv.tcpManager.getDefaultTcpTun();
if(tun!=null){
if(!tun.remoteAddress.getHostAddress().equals(p.getAddress().getHostAddress())
||CapEnv.toUnsigned(tun.remotePort)!=p.getPort()){
capEnv.tcpManager.removeTun(tun);
capEnv.tcpManager.setDefaultTcpTun(null);
}
}else {
tryConnectTun_Client(p.getAddress(),(short) p.getPort());
tun=capEnv.tcpManager.getDefaultTcpTun();
}
}else {
tun=capEnv.tcpManager.getTcpConnection_Server(p.getAddress().getHostAddress(), (short) p.getPort());
}
if(tun!=null){
if(tun.preDataReady){
tun.sendData(p.getData());
}else{
throw new IOException("隧道未连接!");
}
}else{
throw new IOException("隧道不存在! "+" thread "+Route.es.getActiveCount()+" "+p.getAddress()+":"+p.getPort());
}
}
示例6: sendPacketToClient
import java.net.DatagramPacket; //导入方法依赖的package包/类
private void sendPacketToClient(DatagramPacket receivePacket)
throws IOException
{
displayMessage("\n\nEcho data to client...");
// create packet to send
DatagramPacket sendPacket = new DatagramPacket(
receivePacket.getData(), receivePacket.getLength(),
receivePacket.getAddress(), receivePacket.getPort());
socket.send(sendPacket); // send packet to client
displayMessage("Packet sent\n");
}
示例7: run
import java.net.DatagramPacket; //导入方法依赖的package包/类
@Override
public void run() {
try {
byte[] buffer = new byte[65535];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
if (localDestination == null) {
socket.receive(packet);
this.localDestination = new InetSocketAddress(packet.getAddress(), packet.getPort());
handlePacket(packet);
channelHandler.createChannel(channelId, this);
}
while (!socket.isClosed()) {
try {
socket.receive(packet);
handlePacket(packet);
} catch (Exception e) {
if(!socket.isClosed()) {
e.printStackTrace();
}
}
}
} catch (IOException ex) {
Logger.getLogger(UDPChannel.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例8: main
import java.net.DatagramPacket; //导入方法依赖的package包/类
public static void main(String args[]) throws Exception
{
UDPSocket udpsocket = new UDPSocket();
DatagramSocket serverSocket = new DatagramSocket(80); // Port Nimber
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String( receivePacket.getData());
System.out.println("RECEIVED: " + sentence);
String[] tempdata = sentence.split(",");
String module = tempdata[0];
String[] temprature = tempdata[1].split(":");
System.out.println("Module : "+module);
// System.out.println("Temprature : "+temprature[1]);
// udpsocket.insertData(receivePacket.getAddress()+"",module,""+temprature[1]);
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
System.out.println("IPAddress: " + IPAddress +" Port : "+port+" Module : "+module);
// DatagramPacket sendPacket =
// new DatagramPacket(sendData, sendData.length, IPAddress, port);
// serverSocket.send(sendPacket);
}
}
示例9: forwardPacket
import java.net.DatagramPacket; //导入方法依赖的package包/类
void forwardPacket(DatagramPacket outPacket, IpPacket parsedPacket) throws DaedalusVpnService.VpnNetworkException {
Socket dnsSocket;
try {
// Packets to be sent to the real DNS server will need to be protected from the VPN
dnsSocket = SocketChannel.open().socket();
service.protect(dnsSocket);
SocketAddress address = new InetSocketAddress(outPacket.getAddress(), DNSServerHelper.getPortOrDefault(outPacket.getAddress(), outPacket.getPort()));
dnsSocket.connect(address, 5000);
dnsSocket.setSoTimeout(5000);
Logger.info("TcpProvider: Sending DNS query request");
DataOutputStream dos = new DataOutputStream(dnsSocket.getOutputStream());
byte[] packet = processUdpPacket(outPacket, parsedPacket);
dos.writeShort(packet.length);
dos.write(packet);
dos.flush();
if (parsedPacket != null) {
dnsIn.add(new TcpProvider.WaitingOnSocketPacket(dnsSocket, parsedPacket));
} else {
dnsSocket.close();
}
} catch (IOException e) {
if (e.getCause() instanceof ErrnoException) {
ErrnoException errnoExc = (ErrnoException) e.getCause();
if ((errnoExc.errno == OsConstants.ENETUNREACH) || (errnoExc.errno == OsConstants.EPERM)) {
throw new DaedalusVpnService.VpnNetworkException("Cannot send message:", e);
}
}
Log.w(TAG, "handleDnsRequest: Could not send packet to upstream", e);
}
}
示例10: run
import java.net.DatagramPacket; //导入方法依赖的package包/类
@Override
public void run() {
while (isRunning) {
try {
byte[] data = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(data, data.length);
datagramSocket.receive(datagramPacket);
data = datagramPacket.getData();
String text = new String(data);
final String[] splits = text.split("\r\n");
ApplicationManager.getApplication().invokeLater(() -> {
ProjectUtil.focusProjectWindow(project,true);
String slnFilePath = splits[1];
SolutionOpenStrategy strategy = new OpenExistingSolution(slnFilePath, true);
project = RiderProjectOpenProcessor.Companion.doOpenSolution(null, false, strategy);
VirtualFile vf = LocalFileSystem.getInstance().findFileByPath(splits[2]);
int line = Integer.parseInt(splits[0]) - 1;
if (line < 0) line = 0;
int column = 0;
if (splits.length == 4) {
column = Integer.parseInt(splits[0]) - 1;
if (column < 0) column = 0;
}
new OpenFileDescriptor(project, vf, line, column).navigate(true);
});
InetAddress senderAddress = datagramPacket.getAddress();
int senderPort = datagramPacket.getPort();
String sendDataRaw = "ok";
byte[] sendData = sendDataRaw.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, senderAddress, senderPort);
datagramSocket.send(sendPacket);
} catch (IOException e) {
e.printStackTrace();
stop();
}
}
}
示例11: read
import java.net.DatagramPacket; //导入方法依赖的package包/类
/**
* {@inheritDoc}
* <p>
* If the connection type is a server, or replacing of remote data was defined, and data from a new source was
* received than that source is now the remote connection.
* </p>
*/
@Override
public boolean read(Packet packet) {
if(!isOpened()) return false;
try {
DatagramPacket recp = new DatagramPacket(data, data.length);
socket.receive(recp);
if(server && ((portOut < 0 && outInet == null) || replace)){
outInet = recp.getAddress();
portOut = recp.getPort();
}else if(recp.getPort() != portOut ||
!FlashUtil.equals(outInet.getAddress(), recp.getAddress().getAddress())){
FlashUtil.getLog().log("Unknow sender!!");
}
newDataRead();
packet.data = recp.getData();
packet.length = recp.getLength();
if(isHandshake(packet.data, packet.length)){
packet.length = 1;
return true;
}
/*if(server && isHandshakeClient(packet.data, packet.length)){
write(HANDSHAKE_CONNECT_SERVER);
packet.length = 1;
return true;
}
if(!server && isHandshakeServer(packet.data, packet.length)){
write(HANDSHAKE_CONNECT_CLIENT);
packet.length = 1;
return true;
}*/
return true;
} catch (IOException e) {
packet.length = 0;
return false;
}
}