Code example for ServerSocket

Methods: getLocalPort, setSoTimeout

0
 
	public StreamProxy(DownloadService downloadService) {
 
		// Create listening socket 
		try { 
			socket = new ServerSocket(0, 0, InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }));
			socket.setSoTimeout(5000);
			port = socket.getLocalPort();
			this.downloadService = downloadService;
		} catch (UnknownHostException e) { // impossible
		} catch (IOException e) {
			Log.e(TAG, "IOException initializing server", e);
		} 
	} 
	 
	public int getPort() { 
		return port;
	} 
 
	public void start() { 
		thread = new Thread(this);