Advertisement
andruala

Untitled

Nov 11th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8. import java.net.http.HttpResponse;
  9. import java.util.Date;
  10.  
  11. public class Start {
  12.  
  13. //static ServerSocket variable
  14. //socket server port on which it will listen
  15. private static int port = 80;
  16.  
  17. public static void main(String args[]) throws IOException, ClassNotFoundException{
  18. ServerSocket server = new ServerSocket(port);
  19. System.out.println("Listening for connection on port 8080 ....");
  20. while (true) {
  21. try (Socket socket = server.accept()) {
  22. System.out.println("a venit cineva");
  23. int red = -1;
  24. byte[] buffer = new byte[5*1024]; // a read buffer of 5KiB
  25. byte[] redData;
  26. StringBuilder clientData = new StringBuilder();
  27. String redDataText;
  28. while ((red = socket.getInputStream().read(buffer)) > -1) {
  29. redData = new byte[red];
  30. System.arraycopy(buffer, 0, redData, 0, red);
  31. redDataText = new String(redData,"UTF-8"); // assumption that client sends data UTF-8 encoded
  32. System.out.println("message part recieved:" + redDataText);
  33. clientData.append(redDataText);
  34. }
  35. System.out.println("Data From Client :" + clientData.toString() );
  36.  
  37. ///
  38. System.out.println("pregatesc raspunsul");
  39. Date today = new Date();
  40. String httpResponse = "HTTP/1.1 200 OK\r\n\r\n" +
  41. "<html><head>asd</head><body><form method=\"GET\" action=\"\"><input type=\"text\" name=\"test\" value=\"Test\"/><input type=\"submit\" value=\"Submit\"/></form></body></html>";
  42. socket.getOutputStream().write(httpResponse.getBytes("UTF-8"));
  43. System.out.println("am dat raspunsul");
  44.  
  45. }
  46. }
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement