Created
November 27, 2019 12:49
-
-
Save trykopa/068d0bed0a108174c5040164da3c66b7 to your computer and use it in GitHub Desktop.
HomeWork 12 Task 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package HomeWork12; | |
/* | |
* 2. Написать сервер, который будет отправлять пользователю информацию о системе и номер запроса. | |
*/ | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.nio.charset.StandardCharsets; | |
import java.util.Properties; | |
public class SimpleServer { | |
public static void main(String[] args) throws IOException { | |
Properties p = System.getProperties(); | |
ServerSocket servSoc = new ServerSocket(8080); | |
int count = 0; | |
System.out.println("START!"); | |
while (count < 5) { | |
Socket socket = servSoc.accept(); | |
System.out.println("Client connected! " + (++count)); | |
InputStreamReader is = new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8); | |
BufferedReader input = new BufferedReader(is); | |
PrintWriter output = new PrintWriter(socket.getOutputStream(), true); | |
while (!input.ready()); | |
System.out.println(); | |
while (input.ready()) { | |
System.out.println(input.readLine()); | |
} | |
sendAswer(output, p); | |
output.println("Connection No:" + (count)); | |
System.out.println("Client disconnected!"); | |
} | |
servSoc.close(); | |
} | |
public static void sendAswer(PrintWriter output, Properties p) { | |
output.println("HTTP/1.1 200 OK"); | |
output.println("Content-Type: text/html; charset=utf-8"); | |
output.println(); | |
output.println("Server OS: "); | |
output.println(); | |
output.println(p.getProperty("os.name")); | |
output.println(p.getProperty("os.arch")); | |
output.println(p.getProperty("os.version")); | |
output.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment