/** List file names */ public Enumeration nlst(String s) throws IOException { InetAddress inetAddress = InetAddress.getLocalHost(); byte ab[] = inetAddress.getAddress(); serverSocket_ = new ServerSocket(0, 1); StringBuffer sb = new StringBuffer(32); sb.append("PORT "); for (int i = 0; i < ab.length; i++) { sb.append(String.valueOf(ab[i] & 255)); sb.append(","); } sb.append(String.valueOf(serverSocket_.getLocalPort() >>> 8 & 255)); sb.append(","); sb.append(String.valueOf(serverSocket_.getLocalPort() & 255)); if (issueCommand(sb.toString()) != FTP_SUCCESS) { serverSocket_.close(); throw new IOException(getResponseString()); } else if (issueCommand("NLST " + ((s == null) ? "" : s)) != FTP_SUCCESS) { serverSocket_.close(); throw new IOException(getResponseString()); } dataSocket_ = serverSocket_.accept(); serverSocket_.close(); serverSocket_ = null; Vector v = readServerResponse_(dataSocket_.getInputStream()); dataSocket_.close(); dataSocket_ = null; return (v == null) ? null : v.elements(); }
public SetIfModifiedSince() throws Exception { serverSock = new ServerSocket(0); int port = serverSock.getLocalPort(); Thread thr = new Thread(this); thr.start(); Date date = new Date(new Date().getTime() - 1440000); // this time yesterday URL url; HttpURLConnection con; // url = new URL(args[0]); url = new URL("http://localhost:" + String.valueOf(port) + "/anything"); con = (HttpURLConnection) url.openConnection(); con.setIfModifiedSince(date.getTime()); con.connect(); int ret = con.getResponseCode(); if (ret == 304) { System.out.println("Success!"); } else { throw new RuntimeException( "Test failed! Http return code using setIfModified method is:" + ret + "\nNOTE:some web servers are not implemented according to RFC, thus a failed test does not necessarily indicate a bug in setIfModifiedSince method"); } }
private void writeHostAndPortToFile(File portFile) { String host = socket.getInetAddress().getHostName(); int port = socket.getLocalPort(); // The motivation for the Log.warn would be better satisfied by Bug 38. Log.warn("echo " + host + ":" + port + " > " + portFile); StringUtilities.writeFile(portFile, host + ":" + port + "\n"); }
public void run() { while (true) { try { System.out.println( "TCP Server waiting for client on port " + serverSocket.getLocalPort() + "..."); Socket server = serverSocket.accept(); System.out.println("Just received request from " + server.getRemoteSocketAddress()); /* parsing a request from socket */ DataInputStream in = new DataInputStream(server.getInputStream()); String request = in.readUTF(); System.out.println("Request: " + request); /* handle the request */ String output = null; output = requestHandler(request); System.out.print(output); /* display dataTable */ System.out.print("-dataTable--------\n"); Enumeration<String> key = dataTable.keys(); while (key.hasMoreElements()) { String str = key.nextElement(); System.out.println(str + ": " + dataTable.get(str)); } System.out.print("------------------\n"); /* send the result back to the client */ DataOutputStream out = new DataOutputStream(server.getOutputStream()); out.writeUTF(output); server.close(); } catch (SocketTimeoutException s) { System.out.println("Socket timed out!"); break; } catch (IOException e) { e.printStackTrace(); break; } } }
public void run() { try { this.serverSocket = new ServerSocket(Settings.getPropertyInteger("server_port")); } catch (Exception e) { Logger.log( Level.WARNING, Messages.getString("can_not_open_port_", Settings.getLocale()) + Settings.getPropertyInteger("server_port")); if (FancyFileServer.getGUI() != null) { FancyFileServer.getGUI().updateServerStatus(); } return; } running = true; this.parameters = new BasicHttpParams(); this.parameters.setIntParameter(HttpConnectionParams.SO_TIMEOUT, 5000); this.parameters.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024); this.parameters.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false); this.parameters.setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true); this.parameters.setParameter( HttpProtocolParams.ORIGIN_SERVER, FancyFileServer.NAME.replaceAll("\\s", "-") + "/" + FancyFileServer.VERSION); Logger.log(Level.INFO, Messages.getString("server_started", Settings.getLocale())); try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface i = (NetworkInterface) interfaces.nextElement(); if (i.isLoopback() || i.isVirtual()) { continue; } Enumeration<InetAddress> addresses = i.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress a = (InetAddress) addresses.nextElement(); if (a instanceof Inet4Address) { Logger.log( Level.INFO, Messages.getString("server_address_", Settings.getLocale()) + "http://" + a.getHostAddress() + ":" + serverSocket.getLocalPort() + "/"); } } } } catch (Exception e) { Logger.log( Level.WARNING, Messages.getString("can_not_get_server_address_", Settings.getLocale()) + e.getMessage()); } if (FancyFileServer.getGUI() != null) { FancyFileServer.getGUI().updateServerStatus(); } while (running) { try { /** Set up HTTP connection */ Socket socket = this.serverSocket.accept(); DefaultHttpServerConnection connection = new DefaultHttpServerConnection(); Logger.log( Level.DEBUG, Messages.getString("incoming_connection_from_", Settings.getLocale()) + socket.getInetAddress().getHostAddress()); connection.bind(socket, this.parameters); /** Set up HTTP protocol processor */ BasicHttpProcessor processor = new BasicHttpProcessor(); processor.addInterceptor(new ResponseDate()); processor.addInterceptor(new ResponseServer()); processor.addInterceptor(new ResponseContent()); processor.addInterceptor(new ResponseConnControl()); /** Set up HTTP request handlers */ HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry(); registry.register("*", new FileHandler()); /** Set up HTTP service */ HttpService service = new HttpService( processor, new DefaultConnectionReuseStrategy(), new DefaultHttpResponseFactory(), registry, this.parameters); /** Start worker thread */ WorkerThread t = new WorkerThread(this, service, connection); t.setDaemon(true); t.start(); } catch (IOException e) { if (running) { running = false; Logger.log( Level.SEVERE, Messages.getString("i_o_error_", Settings.getLocale()) + e.getMessage()); break; } } } if (FancyFileServer.getGUI() != null) { FancyFileServer.getGUI().updateServerStatus(); } Logger.log(Level.INFO, Messages.getString("server_stopped", Settings.getLocale())); }