use of java.net.DatagramSocket in project pinot by linkedin.
the class NetUtil method getHostAddress.
/**
* Get the ip address of local host.
*/
public static String getHostAddress() throws SocketException, UnknownHostException {
DatagramSocket ds = new DatagramSocket();
ds.connect(InetAddress.getByName(DUMMY_OUT_IP), 80);
InetAddress localAddress = ds.getLocalAddress();
if (localAddress.getHostAddress().equals("0.0.0.0")) {
localAddress = InetAddress.getLocalHost();
}
return localAddress.getHostAddress();
}
use of java.net.DatagramSocket in project jmxtrans by jmxtrans.
the class DatagramSocketFactoryTest method testDatagramSocketFactoryMakeObject.
@Test
public void testDatagramSocketFactoryMakeObject() throws Exception {
DatagramSocketFactory socketFactory = new DatagramSocketFactory();
InetSocketAddress socketAddress = new InetSocketAddress(Inet4Address.getLocalHost(), PORT);
DatagramSocket socketObject = socketFactory.makeObject(socketAddress);
// Test if the remote address/port is the correct one.
assertThat(socketObject.getPort()).isEqualTo(PORT);
assertThat(socketObject.getInetAddress()).isEqualTo(Inet4Address.getLocalHost());
}
use of java.net.DatagramSocket in project Openfire by igniterealtime.
the class ConferenceReceiver method initLoneReceiverChannel.
private void initLoneReceiverChannel(int loneReceiverPort) {
if (this.loneReceiverPort != loneReceiverPort && loneReceiverChannel != null) {
close();
}
this.loneReceiverPort = loneReceiverPort;
try {
selector = Selector.open();
} catch (IOException e) {
Logger.println("Conference receiver failed to open selector " + e.getMessage());
return;
}
if (loneReceiverPort == 0) {
return;
}
Logger.println("Init lone channel using port " + loneReceiverPort);
try {
loneReceiverChannel = DatagramChannel.open();
if (Logger.logLevel >= Logger.LOG_INFO) {
Logger.println("Opened lone receiver channel " + loneReceiverChannel);
}
} catch (IOException e) {
Logger.println("Conference receiver failed to open DatagramChannel " + " " + e.getMessage());
return;
}
try {
loneReceiverChannel.configureBlocking(false);
} catch (IOException e) {
Logger.println("Conference receiver failed to configureBlocking to false " + e.getMessage());
return;
}
DatagramSocket socket = loneReceiverChannel.socket();
try {
socket.setReceiveBufferSize(RtpSocket.MAX_RECEIVE_BUFFER);
} catch (SocketException e) {
Logger.println("ConferenceReceiver failed to set receive buffer size " + e.getMessage());
return;
}
try {
socket.setSoTimeout(0);
} catch (SocketException e) {
Logger.println("ConferenceReceiver failed to set timeout " + e.getMessage());
return;
}
InetSocketAddress bridgeAddress = Bridge.getLocalBridgeAddress();
InetSocketAddress isa = new InetSocketAddress(bridgeAddress.getAddress(), loneReceiverPort);
try {
socket.bind(isa);
} catch (IOException e) {
Logger.println("Conference receiver unable to bind to " + loneReceiverPort + " " + e.getMessage());
return;
}
try {
SelectionKey selectionKey = loneReceiverChannel.register(selector, SelectionKey.OP_READ);
} catch (Exception e) {
Logger.println("Conference receiver unable to register: " + e.getMessage());
return;
}
memberCount++;
Logger.println("Lone Channel uses port " + loneReceiverPort);
}
use of java.net.DatagramSocket in project Openfire by igniterealtime.
the class ConferenceMember method initializeChannel.
private void initializeChannel() throws IOException {
datagramChannel = conferenceManager.getConferenceReceiver().getChannel(cp);
if (datagramChannel != null) {
synchronized (datagramChannel) {
if (loneRtcpReceiver == null) {
int rtcpPort = datagramChannel.socket().getLocalPort() + 1;
Logger.println("Starting lone RtcpReceiver on port " + rtcpPort);
loneRtcpReceiver = new RtcpReceiver(new DatagramSocket(rtcpPort), true);
}
rtcpReceiver = loneRtcpReceiver;
}
return;
}
/*
* We are trying to find a pair of sockets with consecutive port nums.
* The first socket must have an even port.
*
* If we find a socket that we don't like, we have to keep it open
* otherwise when we try to find another socket, we may get the same
* one as before.
*
* So we make a list of the bad sockets and close them all
* after we're done.
*/
ArrayList badChannels = new ArrayList();
int nextRtpPort = firstRtpPort;
try {
while (true) {
datagramChannel = DatagramChannel.open();
if (Logger.logLevel >= Logger.LOG_DETAIL) {
Logger.println("Call " + cp + " Opened datagram channel " + datagramChannel);
}
datagramChannel.configureBlocking(false);
DatagramSocket socket = datagramChannel.socket();
socket.setReceiveBufferSize(RtpSocket.MAX_RECEIVE_BUFFER);
socket.setSoTimeout(0);
InetSocketAddress bridgeAddress = Bridge.getLocalBridgeAddress();
InetSocketAddress isa = new InetSocketAddress(bridgeAddress.getAddress(), nextRtpPort);
if (nextRtpPort > 0) {
nextRtpPort += 2;
if (lastRtpPort != 0 && (nextRtpPort + 1) > lastRtpPort) {
Logger.println("No more RTP ports available, last is " + lastRtpPort);
closeBadChannels(badChannels);
throw new IOException("No more RTP ports available, last is " + lastRtpPort);
}
}
try {
socket.bind(isa);
int localPort = socket.getLocalPort();
if ((localPort & 1) != 0) {
/*
* Port is odd, can't use this datagramSocket
*/
if (Logger.logLevel >= Logger.LOG_INFO) {
Logger.println("Call " + cp + " skipping DatagramSocket with odd port " + localPort);
}
badChannels.add(datagramChannel);
continue;
}
Logger.writeFile("Call " + cp + " RTCP Port " + (localPort + 1));
rtcpReceiver = new RtcpReceiver(new DatagramSocket(localPort + 1), false);
break;
} catch (SocketException e) {
/*
* Couldn't bind, can't use this DatagramSocket.
*/
if (Logger.logLevel >= Logger.LOG_INFO) {
Logger.println("Call " + cp + " skipping DatagramSocket " + e.getMessage());
}
badChannels.add(datagramChannel);
continue;
}
}
} catch (Exception e) {
closeBadChannels(badChannels);
throw new IOException("Call " + cp + " MemberReceiver exception! " + e.getMessage());
}
closeBadChannels(badChannels);
if (Logger.logLevel >= Logger.LOG_INFO) {
Logger.println("Call " + cp + " port " + datagramChannel.socket().getLocalPort());
}
}
use of java.net.DatagramSocket in project camel by apache.
the class Mina2SslContextParametersUdpTest method sendUdpMessages.
protected void sendUdpMessages() throws Exception {
DatagramSocket socket = new DatagramSocket();
try {
InetAddress address = InetAddress.getByName("127.0.0.1");
for (int i = 0; i < messageCount; i++) {
String text = "Hello Message: " + Integer.toString(i);
byte[] data = text.getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, address, getPort());
socket.send(packet);
}
Thread.sleep(2000);
} finally {
socket.close();
}
}
Aggregations