List of usage examples for java.net ServerSocket accept
public Socket accept() throws IOException
From source file:printers.Printers.java
/** * @param args the command line arguments *///from w w w . j av a 2 s .com public static void main(String[] args) throws FileNotFoundException, PrinterException { try { ServerSocket server = new ServerSocket(4500); while (true) { Socket cliente = server.accept(); String sendString = ""; //ObjectOutputStream salida = new ObjectOutputStream(cliente.getOutputStream()); //ObjectInputStream entrada = new ObjectInputStream(cliente.getInputStream()); //BufferedReader entrada = new BufferedReader(new InputStreamReader(cliente.getInputStream())); //BufferedWriter socketWriter = new BufferedWriter(new OutputStreamWriter(cliente.getOutputStream())); BufferedReader entrada = new BufferedReader(new InputStreamReader(cliente.getInputStream())); DataOutputStream salida = new DataOutputStream(cliente.getOutputStream()); getPrinter printer = new getPrinter(); //JSONObject obj = new JSONObject(); //obj.put("printers", printer.searshPrinter()); //sendString = String.join("|", printer.searshPrinter()); try { String mensajeRecibido = entrada.readLine(); String[] mensajeRecibidoArr = mensajeRecibido.split("\\|"); System.out.println(mensajeRecibido); System.out.println(printer.setPrinting(mensajeRecibidoArr)); //System.out.println(printer.savePdf(mensajeRecibidoArr)); //salida.writeUTF(sendString); //System.out.println(sendString); //socketWriter.write("hola"); cliente.close(); } catch (IOException ex) { Logger.getLogger(Printers.class.getName()).log(Level.SEVERE, null, ex); } } } catch (IOException ex) { Logger.getLogger(Printers.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:ThreadedEchoServer.java
public static void main(String[] args) { try {/*from w w w . j ava 2 s .co m*/ int i = 1; ServerSocket s = new ServerSocket(8189); while (true) { Socket incoming = s.accept(); System.out.println("Spawning " + i); Runnable r = new ThreadedEchoHandler(incoming); Thread t = new Thread(r); t.start(); i++; } } catch (IOException e) { e.printStackTrace(); } }
From source file:ezbake.local.accumulo.LocalAccumulo.java
public static void main(String args[]) throws Exception { Logger logger = LoggerFactory.getLogger(LocalAccumulo.class); final File accumuloDirectory = Files.createTempDir(); int shutdownPort = 4445; MiniAccumuloConfig config = new MiniAccumuloConfig(accumuloDirectory, "strongpassword"); config.setZooKeeperPort(12181);//from w w w . j a v a 2 s . c o m final MiniAccumuloCluster accumulo = new MiniAccumuloCluster(config); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { accumulo.stop(); FileUtils.deleteDirectory(accumuloDirectory); System.out.println("\nShut down gracefully on " + new Date()); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }); accumulo.start(); printInfo(accumulo, shutdownPort); if (args.length > 0) { logger.info("Adding the following authorizations: {}", Arrays.toString(args)); Authorizations auths = new Authorizations(args); Instance inst = new ZooKeeperInstance(accumulo.getConfig().getInstanceName(), accumulo.getZooKeepers()); Connector client = inst.getConnector("root", new PasswordToken(accumulo.getConfig().getRootPassword())); logger.info("Connected..."); client.securityOperations().changeUserAuthorizations("root", auths); logger.info("Auths updated"); } // start a socket on the shutdown port and block- anything connected to this port will activate the shutdown ServerSocket shutdownServer = new ServerSocket(shutdownPort); shutdownServer.accept(); System.exit(0); }
From source file:HTTPServer.java
public static void main(String[] args) throws Exception { ServerSocket sSocket = new ServerSocket(1777); while (true) { System.out.println("Waiting for a client..."); Socket newSocket = sSocket.accept(); System.out.println("accepted the socket"); OutputStream os = newSocket.getOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(newSocket.getInputStream())); String inLine = null;/*from w ww . jav a 2 s .c o m*/ while (((inLine = br.readLine()) != null) && (!(inLine.equals("")))) { System.out.println(inLine); } System.out.println(""); StringBuffer sb = new StringBuffer(); sb.append("<html>\n"); sb.append("<head>\n"); sb.append("<title>Java \n"); sb.append("</title>\n"); sb.append("</head>\n"); sb.append("<body>\n"); sb.append("<H1>HTTPServer Works!</H1>\n"); sb.append("</body>\n"); sb.append("</html>\n"); String string = sb.toString(); byte[] byteArray = string.getBytes(); os.write("HTTP/1.0 200 OK\n".getBytes()); os.write(new String("Content-Length: " + byteArray.length + "\n").getBytes()); os.write("Content-Type: text/html\n\n".getBytes()); os.write(byteArray); os.flush(); os.close(); br.close(); newSocket.close(); } }
From source file:paxos.Receiver.java
/** * Contoh kode program untuk node yang menerima paket. Idealnya dalam paxos * balasan juga dikirim melalui UnreliableSender. *//* ww w . j a v a2 s.c om*/ public static void main(String args[]) throws Exception { System.out.println("STARTED:"); int listenPort = 9876; /*DatagramSocket serverSocket = new DatagramSocket(listenPort); byte[] receiveData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength()); JSONObject receivedMessage = new JSONObject(sentence); System.out.println("RECEIVED: " + receivedMessage.optString("method")); }*/ try { ServerSocket serverSocket = new ServerSocket(listenPort); System.out.println("Server connected\n"); while (true) { Socket server = serverSocket.accept(); DataInputStream in = new DataInputStream(server.getInputStream()); System.out.println(in.readUTF()); DataOutputStream out = new DataOutputStream(server.getOutputStream()); out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress() + "\nGoodbye!"); } } catch (Exception e) { e.printStackTrace(); System.out.println("It didn't work"); } }
From source file:flink.iso8583.example.Server.java
public static void main(String[] args) throws Exception { mfact = ConfigParser.createFromClasspathConfig("flink/iso8583/example/config.xml"); log.info("Setting up server socket..."); ServerSocket server = new ServerSocket(9999); log.info("Waiting for connections..."); while (true) { Socket sock = server.accept(); log.info("New connection from " + sock.getInetAddress() + ":" + sock.getPort()); new Server(sock).start(); }/*from w w w . j a va 2 s . co m*/ }
From source file:BufferedSocketClient.java
public static void main(String args[]) throws Exception { int cTosPortNumber = 1777; String str;/*from ww w . j av a 2 s . c om*/ ServerSocket servSocket = new ServerSocket(cTosPortNumber); System.out.println("Waiting for a connection on " + cTosPortNumber); Socket fromClientSocket = servSocket.accept(); PrintWriter pw = new PrintWriter(fromClientSocket.getOutputStream(), true); BufferedReader br = new BufferedReader(new InputStreamReader(fromClientSocket.getInputStream())); while ((str = br.readLine()) != null) { System.out.println("The message: " + str); if (str.equals("bye")) { pw.println("bye"); break; } else { str = "Server returns " + str; pw.println(str); } } pw.close(); br.close(); fromClientSocket.close(); }
From source file:com.googlecode.lineblog.websocket.v2.WebSocketServer.java
public static void main(String[] args) { try {//from w ww .j ava 2 s .co m ServerSocket ss = new ServerSocket(SERVER_PORT); log.info("Listen----" + SERVER_PORT + "----port!"); new Console().start(); while (true) { Socket sk = ss.accept(); new TokenThread(sk).start(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:ObjServer.java
public static void main(String args[]) throws Exception { ServerSocket ssock = new ServerSocket(1234); Hashtable hash = new Hashtable(); hash.put("Java Source and Support", "www.java2s.com"); while (true) { System.out.println("Listening"); Socket sock = ssock.accept(); ObjectOutputStream ostream = new ObjectOutputStream(sock.getOutputStream()); ostream.writeObject(hash);/*from w w w . j a v a 2s. com*/ ostream.close(); sock.close(); } }
From source file:j8583.example.Server.java
public static void main(String[] args) throws Exception { mfact = ConfigParser.createFromClasspathConfig("j8583/example/config.xml"); System.err.println("Setting up server socket..."); ServerSocket server = new ServerSocket(9999); System.err.println("Waiting for connections..."); while (true) { Socket sock = server.accept(); System.err.println(String.format("New connection from %s:%d", sock.getInetAddress(), sock.getPort())); new Thread(new Server(sock), "j8583-server").start(); }/*from w w w.j a va 2s.co m*/ }