Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

HTTP server capable of Keep-Alive

  • 20-12-2007 5:33pm
    #1
    Registered Users, Registered Users 2 Posts: 590 ✭✭✭


    Hi,

    I'm trying to create a http server in Java which is capable of providing keep-alive connections. I'm using the com.sun.net.httpserver.HttpServer class.

    Here's my code:
    package Test;
    
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.InetSocketAddress;
    
    import com.sun.net.httpserver.Headers;
    import com.sun.net.httpserver.HttpExchange;
    import com.sun.net.httpserver.HttpHandler;
    import com.sun.net.httpserver.HttpServer;
    
    public class httpHandler implements HttpHandler {
    	
    	private String resp = "<?xml version='1.0'?><root-node></root-node>";
    	
    	private OutputStream os = null;
    	
    	public void handle(HttpExchange t) throws IOException {
    		System.out.println("Handling message...");
    		java.io.InputStream is = t.getRequestBody();
    		
    		System.out.println("Got request body. Reading request body...");
    		byte[] b = new byte[500];
    		is.read(b);
    		System.out.println("This is the request: " + new String(b));
    			
    		String response = resp;
    		Headers header = t.getResponseHeaders();
    		header.add("Connection", "Keep-Alive");
    		header.add("Keep-Alive", "timeout=14 max=100");
    		header.add("Content-Type", "application/soap+xml");
    		t.sendResponseHeaders(200, response.length());
    		
    		if(os == null) {
    			os = t.getResponseBody();
    		}
    		
    		os.write(response.getBytes());
    		
    		System.out.println("Done with exchange. Closing connection");
    		os.close();
    	}
    
    	public static void main(String[] args) {
    		HttpServer server = null;
    		try {
    			server = HttpServer.create(new InetSocketAddress(8080), 5);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		server.createContext("/", new httpHandler());
    		server.setExecutor(null); // creates a default executor
    		System.out.println("Starting server...");
    		server.start();
    	}
    }
    

    The client does not close the connection. The server seems to close it instead directly after the exchange has occurred. I tried deleting the os.close line but then the server will not reply to the second request. But it doesn't close it either. I have a feeling it involves doing something in the main code with the server object but I have no idea what. Google isn't turning up much either.

    Anyone here got any ideas? Any help would be much appreciated.


Advertisement