public SocksServer(int port) throws IOException { this.port = port; server = new ServerSocket(); if (port == 0) { server.bind(null); this.port = server.getLocalPort(); } else { server.bind(new InetSocketAddress(port)); } }
public static void main(String[] args) { try { System.out.println("Creando socket servidor"); ServerSocket serverSocket = new ServerSocket(); System.out.println("Realizando el enlace"); InetSocketAddress addr = new InetSocketAddress("localhost", 5555); serverSocket.bind(addr); System.out.println("Aceptando conexiones"); Socket newSocket = serverSocket.accept(); System.out.println("Conexión recibida"); InputStream is = newSocket.getInputStream(); OutputStream os = newSocket.getOutputStream(); byte[] mensaje = new byte[25]; is.read(mensaje); System.out.println("Mensaje recibido: " + new String(mensaje)); System.out.println("Cerrando el nuevo socket"); newSocket.close(); System.out.println("Cerrando el socket servidor"); serverSocket.close(); System.out.println("Terminado"); } catch (IOException e) { e.printStackTrace(); } }
public void connectAndFormStreams() throws Exception { serverSocket = new ServerSocket(); serverSocket.setReuseAddress(true); serverSocket.bind(new InetSocketAddress(listeningPortNumber)); while (!Thread.interrupted()) { Log.i("Connect", "Before accept()"); s = serverSocket.accept(); Log.i("Connect", "after accept"); is = s.getInputStream(); InputStreamReader isr = new InputStreamReader(is); br = new BufferedReader(isr); Thread serverThread = new Thread(this); serverThread.start(); } s.close(); serverSocket.close(); }
public void startServer() throws IOException { serverSock = new ServerSocket(); serverSock.bind(new InetSocketAddress(ip, port)); System.out.println("You have connected"); while (true) { Socket socket = serverSock.accept(); // Remember that .accept is a blocking call. handleClient(socket); } }
/* * Listen on the specified address and port. Return a listener * that encapsulates the ServerSocket. */ ListenKey startListening(String localaddress, int port) throws IOException { InetSocketAddress sa; if (localaddress == null) { sa = new InetSocketAddress(port); } else { sa = new InetSocketAddress(localaddress, port); } ServerSocket ss = new ServerSocket(); ss.bind(sa); return new SocketListenKey(ss); }
public String receiveMessage() { try { if (receiveSocket.equals(null)) receiveSocket = new ServerSocket(port); socket = receiveSocket.accept(); // start of added code receiveSocket.setReuseAddress(true); receiveSocket.bind(new InetSocketAddress("localhost", port)); // End of added code BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); return new String(reader.readLine()); } catch (Exception e) { return ""; } }
/** {@inheritDoc} */ @Override public void start() throws IgniteCheckedException { IpcSharedMemoryNativeLoader.load(log); pid = IpcSharedMemoryUtils.pid(); if (pid == -1) throw new IpcEndpointBindException("Failed to get PID of the current process."); if (size <= 0) throw new IpcEndpointBindException("Space size should be positive: " + size); String tokDirPath = this.tokDirPath; if (F.isEmpty(tokDirPath)) throw new IpcEndpointBindException("Token directory path is empty."); tokDirPath = tokDirPath + '/' + locNodeId.toString() + '-' + IpcSharedMemoryUtils.pid(); tokDir = U.resolveWorkDirectory(tokDirPath, false); if (port <= 0 || port >= 0xffff) throw new IpcEndpointBindException("Port value is illegal: " + port); try { srvSock = new ServerSocket(); // Always bind to loopback. srvSock.bind(new InetSocketAddress("127.0.0.1", port)); } catch (IOException e) { // Although empty socket constructor never throws exception, close it just in case. U.closeQuiet(srvSock); throw new IpcEndpointBindException( "Failed to bind shared memory IPC endpoint (is port already " + "in use?): " + port, e); } gcWorker = new GcWorker(gridName, "ipc-shmem-gc", log); new IgniteThread(gcWorker).start(); if (log.isInfoEnabled()) log.info( "IPC shared memory server endpoint started [port=" + port + ", tokDir=" + tokDir.getAbsolutePath() + ']'); }
private void doBind(InetSocketAddress addr) throws IOException { ServerSocket svr = new ServerSocket(); svr.bind(null); InetSocketAddress bad = (InetSocketAddress) svr.getLocalSocketAddress(); out.write(PROTO_VERS); out.write(REQUEST_OK); out.write(0); out.write(IPV4); out.write(bad.getAddress().getAddress()); out.write((bad.getPort() >> 8) & 0xff); out.write((bad.getPort() & 0xff)); out.flush(); dest = svr.accept(); bad = (InetSocketAddress) dest.getRemoteSocketAddress(); out.write(PROTO_VERS); out.write(REQUEST_OK); out.write(0); out.write(IPV4); out.write(bad.getAddress().getAddress()); out.write((bad.getPort() >> 8) & 0xff); out.write((bad.getPort() & 0xff)); out.flush(); InputStream in2 = dest.getInputStream(); OutputStream out2 = dest.getOutputStream(); Tunnel tunnel = new Tunnel(in2, out); tunnel.start(); int b = 0; do { // Note that the socket might be close from another thread (the tunnel) try { b = in.read(); if (b == -1) { in.close(); out2.close(); return; } out2.write(b); } catch (IOException ioe) { } } while (!client.isClosed()); }
public void listen(EndPoint localEp, boolean isHttp) throws IOException { ServerSocketChannel serverChannel = ServerSocketChannel.open(); ServerSocket ss = serverChannel.socket(); ss.bind(localEp.getInetAddress()); serverChannel.configureBlocking(false); SelectionKeyHandler handler = null; if (isHttp) { handler = new HttpConnectionHandler(); } else { handler = new TcpConnectionHandler(localEp); } SelectionKey key = SelectorManager.getSelectorManager() .register(serverChannel, handler, SelectionKey.OP_ACCEPT); endPoints_.add(localEp); listenSockets_.put(localEp, key); }
void startServer(int portNumber) { try { final ServerSocket serverSocket = new ServerSocket(); serverSocket.setReuseAddress(true); serverSocket.bind(new InetSocketAddress(portNumber), 5); new Thread( new Runnable() { public void run() { while (true) try { serviceClientApp(serverSocket.accept()); } catch (Throwable e) { error("Error on accept", e); } } }) .start(); } catch (IOException e) { error("Unable to bind Server Socket", e); } }
private void go() throws IOException { // Create a new selector Selector selector = Selector.open(); // Open a listener on each port, and register each one // with the selector for (int i = 0; i < ports.length; ++i) { ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.configureBlocking(false); ServerSocket ss = ssc.socket(); InetSocketAddress address = new InetSocketAddress(ports[i]); ss.bind(address); SelectionKey key = ssc.register(selector, SelectionKey.OP_ACCEPT); System.out.println("Going to listen on " + ports[i]); } while (true) { int num = selector.select(); Set selectedKeys = selector.selectedKeys(); Iterator it = selectedKeys.iterator(); while (it.hasNext()) { SelectionKey key = (SelectionKey) it.next(); if ((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) { // Accept the new connection ServerSocketChannel ssc = (ServerSocketChannel) key.channel(); SocketChannel sc = ssc.accept(); sc.configureBlocking(false); // Add the new connection to the selector SelectionKey newKey = sc.register(selector, SelectionKey.OP_READ); it.remove(); System.out.println("Got connection from " + sc); } else if ((key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) { // Read the data SocketChannel sc = (SocketChannel) key.channel(); // Echo data int bytesEchoed = 0; while (true) { echoBuffer.clear(); int r = sc.read(echoBuffer); if (r <= 0) { break; } echoBuffer.flip(); sc.write(echoBuffer); bytesEchoed += r; } System.out.println("Echoed " + bytesEchoed + " from " + sc); it.remove(); } } // System.out.println( "going to clear" ); // selectedKeys.clear(); // System.out.println( "cleared" ); } }
private ConnectionAccepter(File portFile, InetAddress inetAddress) throws IOException { this.socket = new ServerSocket(); socket.bind(new InetSocketAddress(inetAddress, 0)); writeHostAndPortToFile(portFile); }