List of usage examples for java.net Socket getPort
public int getPort()
From source file:davmail.AbstractConnection.java
/** * Only set the thread name and socket//from w w w.j av a 2s. co m * * @param name thread type name * @param clientSocket client socket */ public AbstractConnection(String name, Socket clientSocket) { super(name + '-' + clientSocket.getPort()); this.client = clientSocket; setDaemon(true); }
From source file:fm.last.moji.tracker.impl.AbstractTrackerFactory.java
public Tracker newTracker(InetSocketAddress newAddress) throws TrackerException { log.debug("new {}()", TrackerImpl.class.getSimpleName()); Tracker tracker = null;/* w ww.ja v a2 s . c o m*/ BufferedReader reader = null; Writer writer = null; Socket socket = null; try { socket = new Socket(netConfig.getProxy()); socket.setSoTimeout(netConfig.getTrackerReadTimeout()); log.debug("Connecting to: {}:", newAddress, socket.getPort()); socket.connect(newAddress, netConfig.getTrackerConnectTimeout()); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); RequestHandler requestHandler = new RequestHandler(writer, reader); tracker = new TrackerImpl(socket, requestHandler); } catch (IOException e) { IOUtils.closeQuietly(reader); IOUtils.closeQuietly(writer); IOUtils.closeQuietly(socket); throw new TrackerException(e); } return tracker; }
From source file:davmail.AbstractConnection.java
/** * Initialize the streams and set thread name. * * @param name thread type name/*from www . j av a 2 s . c o m*/ * @param clientSocket client socket * @param encoding socket stream encoding */ public AbstractConnection(String name, Socket clientSocket, String encoding) { super(name + '-' + clientSocket.getPort()); this.client = clientSocket; try { in = new LineReaderInputStream(client.getInputStream(), encoding); os = new BufferedOutputStream(client.getOutputStream()); } catch (IOException e) { close(); DavGatewayTray.error(new BundleMessage("LOG_EXCEPTION_GETTING_SOCKET_STREAMS"), e); } }
From source file:org.squidy.manager.commander.ControlServer.java
@Override public void run() { try {// w w w. java 2 s . c o m incomings = new ArrayList<Incoming>(); outgoings = new ArrayList<Outgoing>(); while (running) { Socket client = server.accept(); client.setTcpNoDelay(true); if (LOG.isInfoEnabled()) { LOG.info("Client connected: " + client.getInetAddress() + " on port " + client.getPort()); } clients.add(client); Incoming incoming = new Incoming(client.getInputStream(), context); Outgoing outgoing = new Outgoing(client.getOutputStream(), context); ConnectionPeer connectionPeer = new ConnectionPeer(incoming, outgoing); incomings.add(incoming); outgoings.add(outgoing); } for (Incoming incoming : incomings) { incoming.close(); } for (Outgoing outgoing : outgoings) { outgoing.close(); } for (Socket client : clients) { client.close(); } } catch (IOException e) { if (LOG.isErrorEnabled()) { LOG.error("Error occured in control server: " + e.getMessage(), e); } } }
From source file:org.wso2.carbon.databridge.agent.endpoint.binary.BinaryClientPoolFactory.java
@Override public void terminateClient(Object client) { Socket socket = null; try {// w w w .ja va 2 s.co m socket = (Socket) client; socket.close(); } catch (IOException e) { log.warn("Cannot close the socket successfully from " + socket.getLocalAddress().getHostAddress() + ":" + socket.getPort()); } }
From source file:org.coltram.nsd.bonjour.BonjourProcessor.java
public void xscribe(JSONObject object, String serviceId, AtomConnection connection) throws JSONException { final DiscoveredZCService bonjourService = topManager.getServiceManager().findBonjourService(serviceId); if (bonjourService == null) { log.info("no service with id " + serviceId + " in un/subscribe"); return;/* w w w .j av a 2s . c om*/ } log.finer("xscribe Bonjour " + object.getString("eventName") + " local?" + bonjourService.isLocal()); if (bonjourService.isLocal()) { // find the LocalExposedBonjourService in question LocalExposedBonjourService localcbs = LocalExposedBonjourService.getServiceById(serviceId); // send the info to that service object.put("address", "localhost"); object.put("port", bonjourService.getPort()); object.put("originAtom", connection.getId()); if ("subscribe".equals(object.optString("purpose"))) { localcbs.subscribe(object); } else { localcbs.unsubscribe(object); } } else { try { Socket socket = bonjourService.getSocket(); final int port = socket.getPort(); int listenerPort = 0; String callback = object.optString("callback"); if ("subscribe".equals(object.optString("purpose"))) { EventListener eventListener = new EventListener(connection, callback); listeners.add(eventListener); listenerPort = eventListener.getListenerPort(); } else { for (EventListener l : listeners) { if (l.is(connection, callback)) { // todo implement pool of listener ports listeners.remove(l); listenerPort = l.getListenerPort(); l.stop(); break; } } } object.put("address", LocalHost.name); object.put("port", port + ""); object.put("listenerPort", listenerPort + ""); object.put("originAtom", connection.getId()); DataOutputStream dos = bonjourService.getSocketDOS(); dos.writeBytes(object.toString() + "\n"); dos.flush(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:org.coltram.nsd.bonjour.LocalExposedBonjourService.java
public void run() { try {//from w ww. java 2 s . c om log.fine( "starting coltram bonjour service on port " + serverSocket.getLocalPort() + " id:" + serviceId); while (!threadStopper) { Socket socket = serverSocket.accept(); log.fine("socket accepted on " + socket.getPort()); InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream()); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String message; while (true) { message = bufferedReader.readLine(); log.fine("BSS receive on port " + socket.getPort() + " :" + message); if (message == null) { break; } //log.info("relaying bonjour message to:" + serviceId + " -- " + message); log.fine("receiveBonjourMsg " + message); JSONObject msg = new JSONObject(message); String purpose = msg.getString("purpose"); if ("subscribe".equals(purpose)) { subscribe(msg); } else if ("unsubscribe".equals(purpose)) { unsubscribe(msg); } else { notifyListeners(message); } } bufferedReader.close(); inputStreamReader.close(); socket.close(); } log.finer("bonjour service actually stopped"); } catch (Exception e) { e.printStackTrace(); } }
From source file:net.NetUtils.java
/** * This is a drop-in replacement for /* ww w . j av a2 s. co m*/ * {@link Socket#connect(SocketAddress, int)}. * In the case of normal sockets that don't have associated channels, this * just invokes <code>socket.connect(endpoint, timeout)</code>. If * <code>socket.getChannel()</code> returns a non-null channel, * connect is implemented using Hadoop's selectors. This is done mainly * to avoid Sun's connect implementation from creating thread-local * selectors, since Hadoop does not have control on when these are closed * and could end up taking all the available file descriptors. * * @see java.net.Socket#connect(java.net.SocketAddress, int) * * @param socket * @param endpoint * @param timeout - timeout in milliseconds */ public static void connect(Socket socket, SocketAddress endpoint, int timeout) throws IOException { if (socket == null || endpoint == null || timeout < 0) { throw new IllegalArgumentException("Illegal argument for connect()"); } SocketChannel ch = socket.getChannel(); if (ch == null) { // let the default implementation handle it. socket.connect(endpoint, timeout); } else { SocketIOWithTimeout.connect(ch, endpoint, timeout); } // 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."); } }
From source file:net.mybox.mybox.Server.java
/** * Attach a new client to the server//ww w . jav a 2 s . c o m * @param socket */ private void addClient(Socket socket) { if (clients.size() >= maxClients) { printMessage("Too many clients connected"); return; } try { clients.put(socket.getPort(), new ServerClientConnection(this, socket)); printMessage("Client accepted: " + socket); } catch (Exception e) { printMessage("Error attaching client: " + e); } }
From source file:nl.nn.adapterframework.ftp.FTPsClient.java
protected Socket _openDataConnection_(int cmdNr, String param) throws IOException { // if explicit FTPS, the socket connection is establisch unsecure if (session.getFtpType() == FtpSession.FTPS_EXPLICIT_SSL || session.getFtpType() == FtpSession.FTPS_EXPLICIT_TLS) { if (session.isProtp()) { // With Prot P the result is returned over a different port // .. send protp commands sendCommand("PBSZ", "0"); checkReply("PBSZ 0"); sendCommand("PROT", "P"); checkReply("PROT P"); sendCommand("PASV"); checkReply("PASV"); // Parse the host and port name to which the result is send String reply = getReplyString(); String line = reply.substring(reply.indexOf('(') + 1, reply.lastIndexOf(')')); String[] hostinfo = line.split(","); String host = hostinfo[0] + "." + hostinfo[1] + "." + hostinfo[2] + "." + hostinfo[3]; int port = (Integer.parseInt(hostinfo[4]) << 8) + Integer.parseInt(hostinfo[5]); log.debug("channel from pasv reply=" + host + ":" + port); InetSocketAddress address = new InetSocketAddress(host, port); // connect to the result address Socket socket = new Socket(); socket.connect(address);/* w w w . j a v a 2 s . c o m*/ socket.setSoTimeout(1000); host = socket.getInetAddress().getHostAddress(); port = socket.getPort(); log.debug("channel from socket=" + host + ":" + port); socket = socketFactory.createSocket(socket, host, port, true); String cmdLine = FTPCommand.getCommand(cmdNr); if (param != null) { cmdLine += ' ' + param; } // send the requested command (over the original socket) <-- toch maar niet! GvB // _sendCommand(cmdLine, _socket_.getOutputStream(), null); sendCommand(cmdNr, param); // return the new socket for the reply return socket; } } return super._openDataConnection_(cmdNr, param); }