本文整理汇总了Java中java.net.DatagramPacket.getPort方法的典型用法代码示例。如果您正苦于以下问题:Java DatagramPacket.getPort方法的具体用法?Java DatagramPacket.getPort怎么用?Java DatagramPacket.getPort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.DatagramPacket
的用法示例。
在下文中一共展示了DatagramPacket.getPort方法的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: 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);
}
}
}
}
示例4: 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());
}
}
示例5: 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");
}
示例6: 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);
}
}
示例7: run
import java.net.DatagramPacket; //导入方法依赖的package包/类
public void run() {
try {
InetAddress multicastAddress = InetAddress.getByName(Constants.MULTICAST_ADDRESS);
this.msocket = new MulticastSocket(Constants.SSDP_PORT);
this.msocket.setReuseAddress(true);
this.msocket.joinGroup(multicastAddress);
byte[] buffer = new byte[2048];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (!this.terminated) {
this.msocket.receive(packet);
String message = new String(packet.getData(), "UTF-8");
if (message.contains("M-SEARCH")) {
log.debug("Received Search Request from " + packet.getSocketAddress().toString() + ":\n" + message);
if (this.checkSearch(message) && packet.getPort() == 50000) {
log.info("DiscoveryResponse needed");
this.sendDiscoveryResponse(packet.getAddress(), packet.getPort());
}
}
// Reset the length of the packet before reusing it.
packet.setLength(buffer.length);
}
} catch (IOException e) {
log.error("Error while listening for UDP packages", e);
} finally {
this.msocket.close();
}
}
示例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: 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();
}
}
}
示例10: receive
import java.net.DatagramPacket; //导入方法依赖的package包/类
/**
* Receives udp packet. If packet have arrived from the proxy relay server,
* it is processed and address and port of the packet are set to the address
* and port of sending host.<BR>
* If the packet arrived from anywhere else it is not changed.<br>
* <B> NOTE: </B> DatagramPacket size should be at least 10 bytes bigger
* than the largest packet you expect (this is for IPV4 addresses). For
* hostnames and IPV6 it is even more.
*
* @param dp
* Datagram in which all relevent information will be copied.
*/
public void receive(DatagramPacket dp) throws IOException {
super.receive(dp);
if (server_mode) {
// Drop all datagrams not from relayIP/relayPort
final int init_length = dp.getLength();
final int initTimeout = getSoTimeout();
final long startTime = System.currentTimeMillis();
while (!relayIP.equals(dp.getAddress())
|| (relayPort != dp.getPort())) {
// Restore datagram size
dp.setLength(init_length);
// If there is a non-infinit timeout on this socket
// Make sure that it happens no matter how often unexpected
// packets arrive.
if (initTimeout != 0) {
final long passed = System.currentTimeMillis() - startTime;
final int newTimeout = initTimeout - (int) passed;
if (newTimeout <= 0) {
throw new InterruptedIOException(
"In Socks5DatagramSocket->receive()");
}
setSoTimeout(newTimeout);
}
super.receive(dp);
}
// Restore timeout settings
if (initTimeout != 0) {
setSoTimeout(initTimeout);
}
} else if (!relayIP.equals(dp.getAddress())
|| (relayPort != dp.getPort())) {
return; // Recieved direct packet
// If the datagram is not from the relay server, return it it as is.
}
byte[] data;
data = dp.getData();
if (encapsulation != null) {
data = encapsulation.udpEncapsulate(data, false);
}
// FIXME: What is this?
final int offset = 0; // Java 1.1
// int offset = dp.getOffset(); //Java 1.2
final ByteArrayInputStream bIn = new ByteArrayInputStream(data, offset,
dp.getLength());
final ProxyMessage msg = new Socks5Message(bIn);
dp.setPort(msg.port);
dp.setAddress(msg.getInetAddress());
// what wasn't read by the Message is the data
final int data_length = bIn.available();
// Shift data to the left
System.arraycopy(data, offset + dp.getLength() - data_length, data,
offset, data_length);
dp.setLength(data_length);
}
示例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;
}
}