private void initServer() { this.mSocketServer = new SocketServer<SocketConnectionClientConnector>( SERVER_PORT, new ExampleClientConnectorListener(), new ExampleServerStateListener()) { @Override protected SocketConnectionClientConnector newClientConnector( final SocketConnection pSocketConnection) throws IOException { return new SocketConnectionClientConnector(pSocketConnection); } }; this.mSocketServer.start(); try { final byte[] wifiIPv4Address = WifiUtils.getWifiIPv4AddressRaw(this); this.mSocketServerDiscoveryServer = new SocketServerDiscoveryServer<DefaultDiscoveryData>( DISCOVERY_PORT, new ExampleSocketServerDiscoveryServerListener()) { @Override protected DefaultDiscoveryData onCreateDiscoveryResponse() { return new DefaultDiscoveryData(wifiIPv4Address, SERVER_PORT); } }; this.mSocketServerDiscoveryServer.start(); } catch (final Throwable t) { Debug.e(t); } }
private void initServerDiscovery() { try { this.mSocketServerDiscoveryClient = new SocketServerDiscoveryClient<DefaultDiscoveryData>( WifiUtils.getBroadcastIPAddressRaw(this), DISCOVERY_PORT, LOCAL_PORT, DefaultDiscoveryData.class, new ISocketServerDiscoveryClientListener<DefaultDiscoveryData>() { @Override public void onDiscovery( final SocketServerDiscoveryClient<DefaultDiscoveryData> pSocketServerDiscoveryClient, final DefaultDiscoveryData pDiscoveryData) { try { final String ipAddressAsString = IPUtils.ipAddressToString(pDiscoveryData.getServerIP()); MultiplayerServerDiscoveryExample.this.toast( "DiscoveryClient: Server discovered at: " + ipAddressAsString + ":" + pDiscoveryData.getServerPort()); MultiplayerServerDiscoveryExample.this.initClient( ipAddressAsString, pDiscoveryData.getServerPort()); } catch (final UnknownHostException e) { MultiplayerServerDiscoveryExample.this.toast( "DiscoveryClient: IPException: " + e); } } @Override public void onTimeout( final SocketServerDiscoveryClient<DefaultDiscoveryData> pSocketServerDiscoveryClient, final SocketTimeoutException pSocketTimeoutException) { Debug.e(pSocketTimeoutException); MultiplayerServerDiscoveryExample.this.toast( "DiscoveryClient: Timeout: " + pSocketTimeoutException); } @Override public void onException( final SocketServerDiscoveryClient<DefaultDiscoveryData> pSocketServerDiscoveryClient, final Throwable pThrowable) { Debug.e(pThrowable); MultiplayerServerDiscoveryExample.this.toast( "DiscoveryClient: Exception: " + pThrowable); } }); this.mSocketServerDiscoveryClient.discoverAsync(); } catch (final Throwable t) { Debug.e(t); } }