Java Code Examples for java.net.DatagramSocket#bind()
The following examples show how to use
java.net.DatagramSocket#bind() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Ssdp.java From physical-web with Apache License 2.0 | 7 votes |
public synchronized boolean start(Integer timeout) throws IOException { if (mThread == null) { // create a DatagramSocket without binding to any address mDatagramSocket = new DatagramSocket(null); mDatagramSocket.setReuseAddress(true); // bind to any free port mDatagramSocket.bind(null); if (timeout != null && timeout > 0) { mDatagramSocket.setSoTimeout(timeout); } mThread = new Thread(this); mThread.start(); return true; } return false; }
Example 2
Source File: Network.java From OberonEmulator with ISC License | 6 votes |
public Network(InetSocketAddress addr) throws IOException { this.addr = addr; ds = new DatagramSocket(null); ds.setReuseAddress(true); ds.bind(new InetSocketAddress((InetAddress) null, addr.getPort())); Thread t = new Thread(new Runnable() { @Override public void run() { try { while (true) { byte[] buf = new byte[576]; DatagramPacket dp = new DatagramPacket(buf, buf.length); ds.receive(dp); if (dp.getLength() % 32 == 0) netQueue.put(dp); } } catch (Exception ex) { throw new RuntimeException(ex); } } }); t.setDaemon(true); t.start(); }
Example 3
Source File: BroadcastSearch.java From onpc with GNU General Public License v3.0 | 5 votes |
@Override protected Void doInBackground(Void... params) { Logging.info(this, "started, network=" + connectionState.isNetwork() + ", wifi=" + connectionState.isWifi()); final Character[] models = new Character[]{ 'x', 'p' }; int modelId = 0; try { final InetAddress target = InetAddress.getByName("255.255.255.255"); final DatagramSocket socket = new DatagramSocket(null); socket.setReuseAddress(true); socket.setBroadcast(true); socket.setSoTimeout(500); socket.bind(new InetSocketAddress(ISCP_PORT)); while (!isStopped()) { request(socket, target, models[modelId]); modelId++; if (modelId > 1) { modelId = 0; } } socket.close(); } catch (Exception e) { Logging.info(this, "Can not open socket: " + e.toString()); } Logging.info(this, "stopped"); return null; }
Example 4
Source File: UDPReceiver.java From pinpoint with Apache License 2.0 | 5 votes |
private void bindSocket(DatagramSocket socket, InetSocketAddress bindAddress) { try { logger.info("DatagramSocket.bind() {}/{}", bindAddress.getHostString(), bindAddress.getPort()); socket.bind(bindAddress); } catch (SocketException ex) { throw new IllegalStateException("Socket bind Fail. port:" + bindAddress.getPort() + " Caused:" + ex.getMessage(), ex); } }
Example 5
Source File: NetworkManager.java From attic-apex-malhar with Apache License 2.0 | 4 votes |
public synchronized <T extends SelectableChannel> ChannelAction<T> registerAction(int port, ConnectionType type, ChannelListener<T> listener, int ops) throws IOException { boolean startProc = (channels.size() == 0); SelectableChannel channel = null; SocketAddress address = new InetSocketAddress(port); ConnectionInfo connectionInfo = new ConnectionInfo(); connectionInfo.address = address; connectionInfo.connectionType = type; ChannelConfiguration channelConfiguration = channels.get(connectionInfo); if (channelConfiguration == null) { Object socket = null; if (type == ConnectionType.TCP) { SocketChannel schannel = SocketChannel.open(); schannel.configureBlocking(false); Socket ssocket = schannel.socket(); ssocket.bind(address); socket = ssocket; channel = schannel; } else if (type == ConnectionType.UDP) { DatagramChannel dchannel = DatagramChannel.open(); dchannel.configureBlocking(false); DatagramSocket dsocket = dchannel.socket(); dsocket.bind(address); socket = dsocket; channel = dchannel; } if (channel == null) { throw new IOException("Unsupported connection type"); } channelConfiguration = new ChannelConfiguration(); channelConfiguration.actions = new ConcurrentLinkedQueue<ChannelAction>(); channelConfiguration.channel = channel; channelConfiguration.connectionInfo = connectionInfo; channels.put(connectionInfo, channelConfiguration); channelConfigurations.put(channel, channelConfiguration); } else { channel = channelConfiguration.channel; } ChannelAction channelAction = new ChannelAction(); channelAction.channelConfiguration = channelConfiguration; channelAction.listener = listener; channelAction.ops = ops; channelConfiguration.actions.add(channelAction); if (startProc) { startProcess(); } if (listener != null) { channel.register(selector, ops); } return channelAction; }
Example 6
Source File: BasicNetworkInterface.java From mochadoom with GNU General Public License v3.0 | 4 votes |
@Override public void InitNetwork() { //struct hostent* hostentry; // host information entry doomcom = new doomcom_t(); //netbuffer = new doomdata_t(); DOOM.setDoomCom(doomcom); //DM.netbuffer = netbuffer; // set up for network if (!DOOM.cVarManager.with(CommandVariable.DUP, 0, (Character c) -> { doomcom.ticdup = (short) (c - '0'); if (doomcom.ticdup < 1) { doomcom.ticdup = 1; } if (doomcom.ticdup > 9) { doomcom.ticdup = 9; } })) { doomcom.ticdup = 1; } if (DOOM.cVarManager.bool(CommandVariable.EXTRATIC)) { doomcom.extratics = 1; } else { doomcom.extratics = 0; } DOOM.cVarManager.with(CommandVariable.PORT, 0, (Integer port) -> { DOOMPORT = port; System.out.println("using alternate port " + DOOMPORT); }); // parse network game options, // -net <consoleplayer> <host> <host> ... if (!DOOM.cVarManager.present(CommandVariable.NET)) { // single player game DOOM.netgame = false; doomcom.id = DOOMCOM_ID; doomcom.numplayers = doomcom.numnodes = 1; doomcom.deathmatch = 0; // false doomcom.consoleplayer = 0; return; } DOOM.netgame = true; // parse player number and host list doomcom.consoleplayer = (short) (DOOM.cVarManager.get(CommandVariable.NET, Character.class, 0).get() - '1'); RECVPORT = SENDPORT = DOOMPORT; if (doomcom.consoleplayer == 0) { SENDPORT++; } else { RECVPORT++; } doomcom.numnodes = 1; // this node for sure String[] hosts = DOOM.cVarManager.get(CommandVariable.NET, String[].class, 1).get(); for (String host: hosts) { try { InetAddress addr = InetAddress.getByName(host); DatagramSocket ds = new DatagramSocket(null); ds.setReuseAddress(true); ds.connect(addr, SENDPORT); sendaddress[doomcom.numnodes] = ds; } catch (SocketException | UnknownHostException e) { e.printStackTrace(); } doomcom.numnodes++; } doomcom.id = DOOMCOM_ID; doomcom.numplayers = doomcom.numnodes; // build message to receive try { insocket = new DatagramSocket(null); insocket.setReuseAddress(true); insocket.setSoTimeout(1); insocket.bind(new InetSocketAddress(RECVPORT)); } catch (SocketException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
Example 7
Source File: BroadcastingEchoServer.java From tutorials with MIT License | 4 votes |
public BroadcastingEchoServer() throws IOException { socket = new DatagramSocket(null); socket.setReuseAddress(true); socket.bind(new InetSocketAddress(4445)); }