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 no
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:
in = session.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
// this seems to be the key for me!
// somehow I never get out of this loop if I don't
// check for an empty line...
if (line.isEmpty()) {
break;
}
}
out = new BufferedWriter(
new OutputStreamWriter(
new BufferedOutputStream(session.getOutputStream()), "UTF-8")
);
out.write(OUTPUT_HEADERS + OUTPUT.length() + OUTPUT_END_OF_HEADERS + OUTPUT);
out.flush();
out.close();
session.close();
My constants are:
private static final String OUTPUT = "<html><head><title>Example</title></head><body><p>Worked!!!</p></body></html>";
private static final String OUTPUT_HEADERS = "HTTP/1.1 200 OK\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: ";
private static final String OUTPUT_END_OF_HEADERS = "\r\n\r\n";
My "session" variable is a Socket
object.
The below code will solve your problem:
package com.cs.sajal;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class TestCls {
public static void main(String[] args) {
boolean flag = false;
try {
ServerSocket ss = new ServerSocket(8080);
if (ss.isBound()) {
while (flag = true) {
final Socket s = ss.accept();
Thread t1=new Thread(new Runnable (){String line = null;public void run(){try{ InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}});
Thread t2=new Thread(new Runnable (){String line = null;public void run(){try{ OutputStream os = s.getOutputStream();
Scanner sc=new Scanner(System.in);
String inp=null;
String t="HTTP/1.1 200 OK\r\n";
byte[]s=t.getBytes("UTF-8");
os.write(s);
t="Content-Length: 788\r\n";
s=t.getBytes("UTF-8");
os.write(s);
t="Content-Type: text/html\r\n\r\n";
s=t.getBytes("UTF-8");
os.write(s);
t="\r\n\r\n<html><body><h1>this is output</h1><table cellpading=5 border=5><tr><td>name</td><td>age></td><td>height</td></tr></table></body></html>\r\n\r\n";
s=t.getBytes("UTF-8");
os.write(s);
t="Connection: Closed";
s=t.getBytes("UTF-8");
os.write(s);
os.flush();
}
catch(Exception e)
{
e.printStackTrace();
}
}});
t1.start();
t2.start();t1.join();
t2.join();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Try pass the date.
String res = "HTTP/1.0 200 OK\n"
+ "Server: HTTP server/0.1\n";
+ "Date: "+format.format(new java.util.Date())+"\n";
+ "Content-type: text/html; charset=UTF-8\n";
+ "Content-Length: 38\n\n";
+ "<html><body>OK</body></html>";
out.write(res.getBytes());
out.flush();
private SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:Ss z");