I've been trying to write the beginnings of a simple web server, but can't seem to get the response to get sent. I've tried every type of Output stream imaginable but nothing seems to work. I'm at a loss. Here are the two classes I'm using, sorry about some of the extraneous code:
package edu.xsi.webserver;
import java.io.IOException;
import java.net.ServerSocket;
public class WebServer {
int port;
ServerSocket server;
public WebServer(int port) throws IOException{
this.port = port;
this.server = new ServerSocket(port);
Thread t = new Thread(new ServerExec());
t.start();
}
public class ServerExec implements Runnable {
public void run(){
int i = 0;
while (true) {
try {
new WebSession(server.accept(), i++);
System.out.println("Should print before session");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
try {
WebServer webServer = new WebServer(8888);
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is the session class that handles the response.
package edu.xsi.webserver;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class WebSession implements Runnable {
Socket client;
int num;
public WebSession(Socket client, int num) {
this.num = num;
this.client = client;
Thread t = new Thread(this);
t.start();
}
public void run() {
Scanner s = null;
DataOutputStream out = null;
try {
s = new Scanner(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
//Get all input from header
while (s.hasNextLine()) {
System.out.println(s.nextLine());
}
out.writeBytes("HTTP/1.1 200 OK\r\n");
out.writeBytes("Content-Type: text/html\r\n\r\n");
out.writeBytes("<html><head></head><body><h1>Hello</h1></body></html>");
s.close();
out.flush();
out.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
Try pass the date.
The below code will solve your problem:
Closing the Scanner closes the underlying input stream, which closes the socket. Don't close it. Closing the output stream also closes the socket. You don't need both, and you should close the output stream/writer rather than the input stream so it gets flushed.
I've been struggling like crazy with a very similar issue.
After literally head-banding on the wall I found my issue was so trivial I kept on head-banging on the wall for a while.
Basically in my input while loop I was checking for a null line, but I forgot to check for an empty line (and subsequently break).
No guarantee it'll work the same for you but here's my one cent:
My constants are:
My "session" variable is a
Socket
object.