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

The following examples show how to use java.net.Socket#getPort() . 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: NetworkHostNameResolver.java    From PADListener with GNU General Public License v2.0 6 votes vote down vote up
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 File: IgniteClientRejoinTest.java    From ignite with 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 3
Source File: TestSocketFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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 4
Source File: SocketProxy.java    From lucene-solr with 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 5
Source File: ModbusSlaveConnectionTCP.java    From jlibmodbus with Apache License 2.0 5 votes vote down vote up
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 6
Source File: AloneServer.java    From wasindoor with 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 7
Source File: TestSocketFactory.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 File: IgniteClientRejoinTest.java    From ignite with 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 9
Source File: TcpProxyServer.java    From NetBare with 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 10
Source File: IgniteClientRejoinTest.java    From ignite with 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 11
Source File: IgniteClientRejoinTest.java    From ignite with 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 12
Source File: SocketProxy.java    From activemq-artemis with 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 13
Source File: MockTcpTransport.java    From crate with 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 14
Source File: AS2ReceiverHandler.java    From OpenAs2App with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public String getClientInfo(Socket s) {
    return " " + s.getInetAddress().getHostAddress() + " " + s.getPort();
}
 
Example 15
Source File: WebSocketEventPublisher.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
private String socketToStr(Socket socket) {
    if (socket == null) {
        return "";
    }
    return socket.getLocalAddress().toString() + ":" + socket.getPort();
}
 
Example 16
Source File: ExchangeImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
public InetSocketAddress getRemoteAddress() {
   Socket s = this.connection.getChannel().socket();
   InetAddress ia = s.getInetAddress();
   int port = s.getPort();
   return new InetSocketAddress(ia, port);
}
 
Example 17
Source File: IOEvent.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static String getAddress(Socket s) {
    return s.getInetAddress().toString() + ":" + s.getPort();
}
 
Example 18
Source File: SocketProxy.java    From pooled-jms with 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 File: HandShake.java    From gemfirexd-oss with 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 File: HandShake.java    From gemfirexd-oss with 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);
}