Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ResponseServer {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- int clientNum = 0;
- ServerSocket serverSocket = new ServerSocket(8000);
- Socket socket = null;
- while (true) {
- socket = serverSocket.accept();
- System.out.println("CLIENT NUM " + clientNum);
- new HandleClient(socket, clientNum++).run();
- System.out.println("DONE");
- }
- }
- static class HandleClient implements Runnable {
- Socket socket;
- int counter;
- public HandleClient(Socket socket, int counter) {
- System.out.println("RECIEVED " + counter);
- this.socket = socket;
- this.counter = counter;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- InputStream in = socket.getInputStream();
- BufferedReader read = new BufferedReader(new InputStreamReader(in));
- OutputStream out = socket.getOutputStream();
- String response = "<h2>" + counter + "</h2>";
- out.write("HTTP/1.1 200 OKn".getBytes());
- out.write("Content-Type: text/html; charset=utf-8n".getBytes());
- out.write(("Content-Length: " + response.length() + "nn").getBytes());
- out.write(response.getBytes());
- out.flush();
- out.close();
- } catch (IOException e) {
- System.err.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement