Results 1 to 3 of 3
  1. #1
    jobud9 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Java http server instability

    I have a custom server that sends several files to a client via http. Sometimes the server becomes unresponsive and prints out "errornull". i'm not sure why this is but i'm not the best at Java.. so here is source code: (note I am using an API from Bukkit for Minecraft. its really straight forward just not everything is static.)
    Java Code:
    package me.JnH.ChatterBox;
    
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class FileServer implements Runnable{
    	public final Main plugin;
    	public FileServer(Main plugin) {
    		this.plugin = plugin;
    	}
    
    	public void run() {
    		int port = plugin.config.getInt("webserver.port");
    		ServerSocket serversocket = null;
    		try {
    			serversocket = new ServerSocket(port);
    		}
    		catch (Exception e) {
    			plugin.toConsole("Cannot bind to port"+port+"! It is allready in use.",3);
    			return;
    		}
    		while (true) {
    			try {
    				Socket connectionsocket = serversocket.accept();
    				http_handler(new BufferedReader(new InputStreamReader(connectionsocket.getInputStream())), new DataOutputStream(connectionsocket.getOutputStream()));
    			}
    			catch (Exception e) {
    				plugin.toConsole("Error:" + e.getMessage(),2);
    			}
    
    		}
    	}
    
    	private void http_handler(BufferedReader input, DataOutputStream output) {
    		String path = new String();
    		try {
    			String tmp = input.readLine();
    			String tmp2 = new String(tmp);
    			tmp.toUpperCase();
    			int start = 0;
    			int end = 0;
    			for (int a = 0; a < tmp2.length(); a++) {
    				if (tmp2.charAt(a) == ' ' && start != 0) {
    					end = a;
    					break;
    				}
    				if (tmp2.charAt(a) == ' ' && start == 0) {
    					start = a;
    				}
    			}
    			path = tmp2.substring(start + 2, end);
    		}
    		catch (Exception e) {
    			plugin.toConsole("error" + e.getMessage(),2);
    		}
    
    		FileInputStream requestedfile = null;
    
    		try {
    			if(path.matches("applet.class")|| path.matches("dirt.png") ||path.matches("stone.png") || path.matches("grass.png") || path.matches("banner.png")||
    			   path.matches("index.html")  || path.matches("favicon.ico")){
    				requestedfile = new FileInputStream(plugin.PluginDirPath+"html"+File.separator+path);
    			}
    			else{
    				requestedfile = new FileInputStream(plugin.PluginDirPath+"html"+File.separator+"index.htm");
    			}
    
    		} catch (FileNotFoundException e1) {
    			e1.printStackTrace();
    		}
    
    		try {
    			int file_type;
    			if(path.matches("dirt.png")){
    				file_type = 1;
    			}
    			else if(path.matches("banner.png")){
    				file_type = 1;
    			}
    			else if(path.matches("grass.png")){
    				file_type = 1;
    			}
    			else if(path.matches("stone.png")){
    				file_type = 1;
    			}
    			else if(path.matches("favicon.ico")){
    				file_type = 3;
    			}
    			else if(path.matches("applet.class")){
    				file_type = 2;
    			}
    			else{
    				file_type = 0;
    			}
    			output.writeBytes(construct_http_header(file_type));
    			while (true) {
    				int b = requestedfile.read();
    				if (b == -1) {
    					break;
    				}
    				output.write(b);
    			}
    
    			output.close();
    			requestedfile.close();
    		}
    
    		catch (Exception e) {}
    
    	}
    
    	private String construct_http_header(int file_type) {
    		String s = "HTTP/1.0 ";
    		s = s + "200 OK";
    		s = s + "\r\n";
    		s = s + "Connection: close\r\n";
    
    		switch (file_type) {
    		case 0:
    			s = s + "Content-Type: text/html\r\n";
    			break;
    		case 1:
    			s = s + "Content-Type: image/png\r\n";
    			break;
    		case 2:
    			s = s + "Content-Type: application/x-java-applet\r\n";
    			break;
    		case 3:
    			s = s + "Content-Type: image/vnd.microsoft.icon\r\n";
    			break;
    		default:
    			s = s + "Content-Type: text/html\r\n";
    			break;
    		}
    
    		s = s + "\r\n";
    		return s;
    	}
    
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java http server instability

    Almost nobody knows about it but the Java JRE comes bundled with a complete http server; here's its documentation. I don't know why it is bundled with an ordinary JSE and why Sun/Oracle doesn't mention it, but it's there in all its glory. Give it a try, there's no need to install jar files or anything, it is already on board.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Java http server instability

    prints out "errornull"
    Can you copy and post here the full text of the error message.
    Also add a printStackTrace() call to the catch blocks to show the full error message.

Similar Threads

  1. Replies: 0
    Last Post: 06-14-2011, 06:51 PM
  2. A Multithreaded HTTP Server using Sockets in Java
    By zameer in forum Reviews / Advertising
    Replies: 1
    Last Post: 05-30-2011, 06:51 PM
  3. Replies: 1
    Last Post: 05-07-2011, 03:48 PM
  4. server java HTTP request
    By kptN in forum New To Java
    Replies: 0
    Last Post: 10-22-2010, 03:42 PM
  5. java.io.Exception: Server returned HTTP response code: 403
    By navishkumarb in forum Advanced Java
    Replies: 1
    Last Post: 01-05-2008, 01:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •