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

The following examples show how to use java.net.Socket#getPort() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
private SiteData parseData(Socket socket, int _destPort){
    SiteData newSiteData = new SiteData();
    String originalDest = getOriginalDest(socket);
    String[] tokens = originalDest.split(":");
    if (tokens.length == 2){
        String destIP = tokens[0];
        String hostName = DNSProxy.getHostNameFromIp(destIP);
        int destPort = Integer.parseInt(tokens[1]);
        if (destPort != _destPort && _destPort > 0){
            destPort = _destPort;
        }
        newSiteData.destPort = destPort;
        newSiteData.tcpAddress = destIP;
        newSiteData.sourcePort = socket.getPort();
        newSiteData.hostName = hostName;
        newSiteData.name = "";
    }else{
        
    }
    return newSiteData;
}
 
Example 2
Source Project: ignite   File: IgniteClientRejoinTest.java    License: Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected void writeToSocket(Socket sock, TcpDiscoveryAbstractMessage msg, byte[] data,
    long timeout) throws IOException {
    if (blockAll || block && sock.getPort() == 47500)
        throw new SocketException("Test discovery exception");

    super.writeToSocket(sock, msg, data, timeout);
}
 
Example 3
Source Project: ignite   File: IgniteClientRejoinTest.java    License: Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected void writeToSocket(TcpDiscoveryAbstractMessage msg, Socket sock, int res,
    long timeout) throws IOException {
    if (blockAll || block && sock.getPort() == 47500)
        throw new SocketException("Test discovery exception");

    super.writeToSocket(msg, sock, res, timeout);
}
 
Example 4
Source Project: ignite   File: IgniteClientRejoinTest.java    License: Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected Socket openSocket(Socket sock, InetSocketAddress remAddr,
    IgniteSpiOperationTimeoutHelper timeoutHelper) throws IOException, IgniteSpiOperationTimeoutException {
    if (blockAll || block && sock.getPort() == 47500)
        throw new SocketException("Test discovery exception");

    return super.openSocket(sock, remAddr, timeoutHelper);
}
 
Example 5
Source Project: NetBare   File: TcpProxyServer.java    License: MIT License 5 votes vote down vote up
private void onAccept() throws IOException {
    SocketChannel clientChannel = mServerSocketChannel.accept();
    Socket clientSocket = clientChannel.socket();

    // The client ip is the remote server ip
    // The client port is the local port(it is the vpn port not the proxy server port)
    String ip = clientSocket.getInetAddress().getHostAddress();
    int port = clientSocket.getPort();

    // The session should have be saved before the tcp packets be forwarded to proxy server. So
    // we can query it by client port.
    Session session = mSessionProvider.query((short) port);
    if (session == null) {
        throw new IOException("No session saved with key: " + port);
    }

    int remotePort = NetBareUtils.convertPort(session.remotePort);

    // Connect remote server and dispatch data.
    TcpTunnel proxyTunnel = null;
    TcpTunnel remoteTunnel = null;
    try {
        proxyTunnel = new TcpProxyTunnel(clientChannel, mSelector, remotePort);
        remoteTunnel = new TcpRemoteTunnel(mVpnService, SocketChannel.open(),
                mSelector, ip, remotePort);
        TcpVATunnel gatewayTunnel = new TcpVATunnel(session, proxyTunnel,
                remoteTunnel, mMtu);
        gatewayTunnel.connect(new InetSocketAddress(ip, remotePort));
    } catch (IOException e){
        NetBareUtils.closeQuietly(proxyTunnel);
        NetBareUtils.closeQuietly(remoteTunnel);
        throw e;
    }
}
 
Example 6
Source Project: ignite   File: IgniteClientRejoinTest.java    License: Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected void writeToSocket(Socket sock, TcpDiscoveryAbstractMessage msg,
    long timeout) throws IOException, IgniteCheckedException {
    if (blockAll || block && sock.getPort() == 47500)
        throw new SocketException("Test discovery exception");

    super.writeToSocket(sock, msg, timeout);
}
 
Example 7
/**
 * 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 8
Source Project: wasindoor   File: AloneServer.java    License: Apache License 2.0 5 votes vote down vote up
public Client getSokect(String ip, int port) {
	Iterator<Client> it = this.getClients().iterator();
	while (it.hasNext()) {
		Client c = it.next();
		Socket s = c.getSocket();
		if (s.getInetAddress().getHostAddress().equals(ip)
				&& s.getPort() == port) {
			return c;
		}
	}
	return null;
}
 
Example 9
ModbusSlaveConnectionTCP(Socket socket) throws ModbusIOException {
    try {
        this.socket = socket;
        transport = ModbusTransportFactory.createTCP(socket);
        clientInfo = new TcpClientInfo(new TcpParameters(socket.getInetAddress(), socket.getPort(), socket.getKeepAlive()), false);

        open();
    } catch (Exception e) {
        throw new ModbusIOException(e);
    }
}
 
Example 10
Source Project: lucene-solr   File: SocketProxy.java    License: Apache License 2.0 5 votes vote down vote up
public Pump(String kind, Socket source, Socket dest) {
  super("SocketProxy-"+kind+"-" + source.getPort() + ":"
      + dest.getPort());
  src = source;
  destination = dest;
  pause.set(new CountDownLatch(0));
}
 
Example 11
public InterposeSocket(Socket socket, byte[] matchBytes, byte[] replaceBytes) {
    this.socket = socket;
    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 12
private static String getAddress(Socket s) {
    return s.getInetAddress().toString() + ":" + s.getPort();
}
 
Example 13
public InetSocketAddress getRemoteAddress() {
   Socket s = this.connection.getChannel().socket();
   InetAddress ia = s.getInetAddress();
   int port = s.getPort();
   return new InetSocketAddress(ia, port);
}
 
Example 14
Source Project: activemq-artemis   File: SocketProxy.java    License: Apache License 2.0 4 votes vote down vote up
public Pump(Socket source, Socket dest) {
   super("SocketProxy-DataTransfer-" + source.getPort() + ":" + dest.getPort());
   src = source;
   destination = dest;
   pause.set(new CountDownLatch(0));
}
 
Example 15
Source Project: crate   File: MockTcpTransport.java    License: Apache License 2.0 4 votes vote down vote up
public void accept(Executor executor) throws IOException {
    while (isOpen.get()) {
        Socket incomingSocket = serverSocket.accept();
        MockChannel incomingChannel = null;
        try {
            configureSocket(incomingSocket);
            synchronized (this) {
                if (isOpen.get()) {
                    incomingChannel = new MockChannel(incomingSocket,
                                                      new InetSocketAddress(
                                                          incomingSocket.getLocalAddress(),
                                                          incomingSocket.getPort()
                                                      ),
                                                      profile);
                    MockChannel finalIncomingChannel = incomingChannel;
                    incomingChannel.addCloseListener(new ActionListener<Void>() {
                        @Override
                        public void onResponse(Void aVoid) {
                            workerChannels.remove(finalIncomingChannel);
                        }

                        @Override
                        public void onFailure(Exception e) {
                            workerChannels.remove(finalIncomingChannel);
                        }
                    });
                    serverAcceptedChannel(incomingChannel);
                    //establish a happens-before edge between closing and accepting a new connection
                    workerChannels.add(incomingChannel);

                    // this spawns a new thread immediately, so OK under lock
                    incomingChannel.loopRead(executor);
                    // the channel is properly registered and will be cleared by the close code.
                    incomingSocket = null;
                    incomingChannel = null;
                }
            }
        } finally {
            // ensure we don't leak sockets and channels in the failure case. Note that we null both
            // if there are no exceptions so this becomes a no op.
            IOUtils.closeWhileHandlingException(incomingSocket, incomingChannel);
        }
    }
}
 
Example 16
public String getClientInfo(Socket s) {
    return " " + s.getInetAddress().getHostAddress() + " " + s.getPort();
}
 
Example 17
private String socketToStr(Socket socket) {
    if (socket == null) {
        return "";
    }
    return socket.getLocalAddress().toString() + ":" + socket.getPort();
}
 
Example 18
Source Project: pooled-jms   File: SocketProxy.java    License: Apache License 2.0 4 votes vote down vote up
public Pump(Socket source, Socket dest) {
    super("SocketProxy-DataTransfer-" + source.getPort() + ":" + dest.getPort());
    src = source;
    destination = dest;
    pause.set(new CountDownLatch(0));
}
 
Example 19
Source Project: gemfirexd-oss   File: HandShake.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 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 getDistributedMember(Socket sock) {
  return new InternalDistributedMember(
      sock.getInetAddress(), sock.getPort(), false);
}
 
Example 20
Source Project: gemfirexd-oss   File: HandShake.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 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 getDistributedMember(Socket sock) {
  return new InternalDistributedMember(
      sock.getInetAddress(), sock.getPort(), false);
}