Java Code Examples for java.net.Socket.getPort()

The following are Jave code examples for showing how to use getPort() of the java.net.Socket class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: L2J-Global   File: MMOConnection.java   Source Code and License Vote up 6 votes
public MMOConnection(SelectorThread<T> selectorThread, Socket socket, SelectionKey key, boolean tcpNoDelay)
{
	_selectorThread = selectorThread;
	_socket = socket;
	_address = socket.getInetAddress();
	_readableByteChannel = socket.getChannel();
	_writableByteChannel = socket.getChannel();
	_port = socket.getPort();
	_selectionKey = key;
	
	_sendQueue = new NioNetStackList<>();
	
	try
	{
		_socket.setTcpNoDelay(tcpNoDelay);
	}
	catch (SocketException e)
	{
		e.printStackTrace();
	}
}
 
Example 2
Project: T0rlib4Android   File: ProxyServer.java   Source Code and License Vote up 6 votes
/**
 * Create a server with the specified port, listen backlog, and local IP
 * address to bind to. The localIP argument can be used on a multi-homed
 * host for a ServerSocket that will only accept connect requests to one of
 * its addresses. If localIP is null, it will default accepting connections
 * on any/all local addresses. The port must be between 0 and 65535,
 * inclusive. <br>
 * This methods blocks.
 */
public void start(final int port, final int backlog,
		final InetAddress localIP) {
	try {
		ss = new ServerSocket(port, backlog, localIP);
		final String address = ss.getInetAddress().getHostAddress();
		final int localPort = ss.getLocalPort();
		log.info("Starting SOCKS Proxy on: {}:{}", address, localPort);

		while (true) {
			final Socket s = ss.accept();
			final String hostName = s.getInetAddress().getHostName();
			final int port2 = s.getPort();
			log.info("Accepted from:{}:{}", hostName, port2);

			final ProxyServer ps = new ProxyServer(auth, s);
			(new Thread(ps)).start();
		}
	} catch (final IOException ioe) {
		ioe.printStackTrace();
	} finally {
	}
}
 
Example 3
Project: fdt   File: ControlChannel.java   Source Code and License Vote up 6 votes
/**
 * A remote peer connected to FDT
 *
 * @param s - the socket
 * @throws Exception - if anything goes wrong in intialization
 */
public ControlChannel(Socket s, ControlChannelNotifier notifier) throws Exception {
    try {
        this.controlSocket = s;

        this.remoteAddress = s.getInetAddress();
        this.remotePort = s.getPort();
        this.localPort = s.getLocalPort();

        this.notifier = notifier;

        initStreams();
        controlSocket.setTcpNoDelay(true);
        controlSocket.setSoTimeout(1000);

    } catch (Throwable t) {
        close("Cannot instantiate ControlChannel", t);
        throw new Exception(t);
    }
}
 
Example 4
Project: fdt   File: ControlChannel.java   Source Code and License Vote up 6 votes
/**
 * @param parent
 */
public ControlChannel(GSIServer parent, Socket s, Subject peerSubject, ControlChannelNotifier notifier)
        throws Exception {
    try {

        this.controlSocket = s;
        this.subject = peerSubject;
        this.remoteAddress = s.getInetAddress();
        this.remotePort = s.getPort();
        this.localPort = s.getLocalPort();

        this.notifier = notifier;

        initStreams();
        controlSocket.setTcpNoDelay(true);
        controlSocket.setSoTimeout(1000);

    } catch (Throwable t) {
        close("Cannot instantiate ControlChannel", t);
        throw new Exception(t);
    }
}
 
Example 5
Project: L2jBrasil   File: MMOConnection.java   Source Code and License Vote up 5 votes
public MMOConnection(final SelectorThread<T> selectorThread, final Socket socket, final SelectionKey key)
{
    _selectorThread = selectorThread;
    _socket = socket;
    _address = socket.getInetAddress();
    _readableByteChannel = socket.getChannel();
    _writableByteChannel = socket.getChannel();
    _port = socket.getPort();
    _selectionKey = key;

    _sendQueue = new NioNetStackList<SendablePacket<T>>();
}
 
Example 6
Project: minicraft-plus-revived   File: MinicraftServerThread.java   Source Code and License Vote up 5 votes
public MinicraftServerThread(Socket socket, MinicraftServer serverInstance) {
	super("MinicraftServerThread", socket);
	
	this.serverInstance = serverInstance;
	if(serverInstance.isFull()) {
		sendError("server at max capacity.");
		super.endConnection();
		return;
	}
	
	client = new RemotePlayer(null, false, socket.getInetAddress(), socket.getPort());
	
	// username is set later
	
	packetTypesToKeep.addAll(InputType.tileUpdates);
	packetTypesToKeep.addAll(InputType.entityUpdates);
	
	gameTimers = new ArrayList<>();
	
	Timer t = new Timer("ClientPing");
	t.schedule((new MyTask() {
		public void run() { MinicraftServerThread.this.ping(); }
	}), 1000, PING_INTERVAL*1000);
	gameTimers.add(t);
	
	start();
}
 
Example 7
Project: cyberduck   File: KeychainX509KeyManager.java   Source Code and License Vote up 5 votes
@Override
public String chooseClientAlias(final String[] keyTypes, final Principal[] issuers, final Socket socket) {
    final Key key = new Key(socket.getInetAddress().getHostName(), socket.getPort(), issuers);
    final String alias = this.find(key);
    if(alias != null) {
        return alias;
    }
    final String s = super.chooseClientAlias(keyTypes, issuers, socket);
    if(null == s) {
        return null;
    }
    return this.save(key, s);
}
 
Example 8
Project: FreeCol   File: FreeColServer.java   Source Code and License Vote up 5 votes
/**
 * Add a new user connection.  That is a new connection to the server
 * that has not yet logged in as a player.
 *
 * @param socket The client {@code Socket} the connection arrives on.
 * @exception IOException if the socket was already broken.
 */
public void addNewUserConnection(Socket socket) throws IOException {
    final String name = socket.getInetAddress() + ":" + socket.getPort();
    Connection c = new Connection(socket, FreeCol.SERVER_THREAD + name)
        .setMessageHandler(this.userConnectionHandler);
    getServer().addConnection(c);
    // Short delay here improves reliability
    Utils.delay(100, "New connection delay interrupted");
    c.send(new GameStateMessage(this.serverState));
    if (this.serverState == ServerState.IN_GAME) {
        c.send(new VacantPlayersMessage().setVacantPlayers(getGame()));
    }
    logger.info("Client connected from " + name);
}
 
Example 9
Project: fdt   File: XDRTcpSocket.java   Source Code and License Vote up 5 votes
public XDRTcpSocket(Socket s) throws IOException {
      super("XDRTcpSocket for [ " + s.getInetAddress() + ":" + s.getPort() + " ] ", new XDROutputStream(s.getOutputStream()), new XDRInputStream(s.getInputStream()));
      this.rawSocket = s;
      closed = false;
/*this.mode = mode;
this.auth = auth;*/
  }
 
Example 10
Project: openjdk-jdk10   File: TestSocketFactory.java   Source Code and License Vote up 5 votes
/**
 * Construct a socket that interposes on a socket to match and replace.
 * @param socket the underlying socket
 * @param triggerBytes array of bytes to enable matching
 * @param matchBytes the bytes that must match
 * @param replaceBytes the replacement bytes
 */
public InterposeSocket(Socket socket, byte[]
        triggerBytes, byte[] matchBytes, byte[] replaceBytes) {
    this.socket = socket;
    this.triggerBytes = Objects.requireNonNull(triggerBytes, "triggerBytes");
    this.matchBytes = Objects.requireNonNull(matchBytes, "matchBytes");
    this.replaceBytes = Objects.requireNonNull(replaceBytes, "replaceBytes");
    this.inLogStream = new ByteArrayOutputStream();
    this.outLogStream = new ByteArrayOutputStream();
    this.name = "IS" + ++num + "::"
            + Thread.currentThread().getName() + ": "
            + socket.getLocalPort() + " <  " + socket.getPort();
}
 
Example 11
Project: pooled-jms   File: SocketProxy.java   Source Code and License Vote up 4 votes
public Pump(Socket source, Socket dest) {
    super("SocketProxy-DataTransfer-" + source.getPort() + ":" + dest.getPort());
    src = source;
    destination = dest;
    pause.set(new CountDownLatch(0));
}
 
Example 12
Project: hadoop-oss   File: NetUtils.java   Source Code and License Vote up 4 votes
/**
 * Like {@link NetUtils#connect(Socket, SocketAddress, int)} but
 * also takes a local address and port to bind the socket to. 
 * 
 * @param socket
 * @param endpoint the remote address
 * @param localAddr the local address to bind the socket to
 * @param timeout timeout in milliseconds
 */
public static void connect(Socket socket, 
                           SocketAddress endpoint,
                           SocketAddress localAddr,
                           int timeout) throws IOException {
  if (socket == null || endpoint == null || timeout < 0) {
    throw new IllegalArgumentException("Illegal argument for connect()");
  }
  
  SocketChannel ch = socket.getChannel();
  
  if (localAddr != null) {
    Class localClass = localAddr.getClass();
    Class remoteClass = endpoint.getClass();
    Preconditions.checkArgument(localClass.equals(remoteClass),
        "Local address %s must be of same family as remote address %s.",
        localAddr, endpoint);
    socket.bind(localAddr);
  }

  try {
    if (ch == null) {
      // let the default implementation handle it.
      socket.connect(endpoint, timeout);
    } else {
      SocketIOWithTimeout.connect(ch, endpoint, timeout);
    }
  } catch (SocketTimeoutException ste) {
    throw new ConnectTimeoutException(ste.getMessage());
  }

  // There is a very rare case allowed by the TCP specification, such that
  // if we are trying to connect to an endpoint on the local machine,
  // and we end up choosing an ephemeral port equal to the destination port,
  // we will actually end up getting connected to ourself (ie any data we
  // send just comes right back). This is only possible if the target
  // daemon is down, so we'll treat it like connection refused.
  if (socket.getLocalPort() == socket.getPort() &&
      socket.getLocalAddress().equals(socket.getInetAddress())) {
    LOG.info("Detected a loopback TCP socket, disconnecting it");
    socket.close();
    throw new ConnectException(
      "Localhost targeted connection resulted in a loopback. " +
      "No daemon is listening on the target port.");
  }
}
 
Example 13
Project: apcs_final   File: LANUtils.java   Source Code and License Vote up 4 votes
public static String getHostPort(Socket s)
{
	String h = s.getInetAddress().getHostAddress().toString();
	int port = s.getPort();
	return h + ":" + port;
}
 
Example 14
Project: lighthouse   File: BHttpConnectionBase.java   Source Code and License Vote up 4 votes
@Override
public int getRemotePort() {
    final Socket socket = this.socketHolder.get();
    return socket != null ? socket.getPort() : -1;
}
 
Example 15
Project: vscrawler   File: SocketExtension.java   Source Code and License Vote up 4 votes
private String createHandle(Socket socket) {
	return "Socket:" + socket.getInetAddress().toString() + ":" + socket.getPort() + "/" + (++socketIdx);
}
 
Example 16
Project: tcc-rpg   File: ClientConnection.java   Source Code and License Vote up 4 votes
public ClientConnection(Server serv, Socket s) {
    this.serv = serv;
    this.s = s;
    byte[] addr = s.getInetAddress().getAddress();
    chId = (addr[0] << 48 | addr[1] << 32 | addr[2] << 24 | addr[3] << 16) + s.getPort(); //generate unique chId from client's IP and port
}
 
Example 17
Project: java-android-websocket-client   File: BHttpConnectionBase.java   Source Code and License Vote up 4 votes
@Override
public int getRemotePort() {
    final Socket socket = this.socketHolder.get();
    return socket != null ? socket.getPort() : -1;
}
 
Example 18
Project: FreeCol   File: Connection.java   Source Code and License Vote up 2 votes
/**
 * Get the port for this connection.
 *
 * @return The port number, or negative on error.
 */
public int getPort() {
    Socket socket = getSocket();
    return (socket == null) ? -1 : socket.getPort();
}
 
Example 19
Project: freecol   File: Connection.java   Source Code and License Vote up 2 votes
/**
 * Get the port for this connection.
 *
 * @return The port number, or negative on error.
 */
public int getPort() {
    Socket socket = getSocket();
    return (socket == null) ? -1 : socket.getPort();
}
 
Example 20
Project: monarch   File: HandShake.java   Source Code and License Vote up 2 votes
/**
 * Return fake, temporary DistributedMember to represent the other vm this vm is connecting to
 * 
 * @param sock the socket this handshake is operating on
 * @return temporary id to reprent the other vm
 */
private DistributedMember getIDForSocket(Socket sock) {
  return new InternalDistributedMember(sock.getInetAddress(), sock.getPort(), false);
}