Last active
August 18, 2020 22:21
-
-
Save kryvoboker/b5231f96266ad0fdbd54cb30a4e44eca to your computer and use it in GitHub Desktop.
HomeWork9(Работа с сетью)(2)
This file contains 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
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.PrintWriter; | |
import java.net.Socket; | |
public class Client implements Runnable { | |
private Socket soc; | |
private String ansv; | |
private Thread t; | |
public Client(Socket soc, String ansv) { | |
super(); | |
this.soc = soc; | |
this.ansv = ansv; | |
t = new Thread(this); | |
t.start(); | |
} | |
public void run() { | |
try (InputStream is = soc.getInputStream(); OutputStream os = soc.getOutputStream(); | |
PrintWriter pw = new PrintWriter(os)) { | |
byte[] rec1 = new byte[is.available()]; | |
is.read(rec1); | |
String response = "HTTP/1.1 200 OK\r\n" + "Server: My_Server\r\n" + "Content-Type: text / html\r\n" | |
+ "Content-Length: " + "\r\n" + "Connection: close\r\n\r\n"; | |
pw.print(response + ansv); | |
pw.flush(); | |
} catch (IOException e) { | |
System.out.println(e.toString()); | |
} | |
} | |
} |
This file contains 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
import java.util.ArrayList; | |
public class ListOfSystemParametrs { | |
private String name; | |
private ArrayList<ParametrsOfSystem> list = new ArrayList<>(); | |
public ListOfSystemParametrs(String name) { | |
super(); | |
this.name = name; | |
} | |
public void addParametrsOfSystem(ParametrsOfSystem a) { | |
list.add(a); | |
} | |
public ParametrsOfSystem[] getListOfSystemParametrs() { | |
ParametrsOfSystem[] array = new ParametrsOfSystem[list.size()]; | |
for (int i = 0; i < list.size(); i++) { | |
array[i] = list.get(i); | |
} | |
return array; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
} |
This file contains 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
import java.io.IOException; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
public class Main { | |
private static int count = 1; | |
public static void main(String[] args) { | |
String ansver = ""; | |
ListOfSystemParametrs losp = new ListOfSystemParametrs("List about ASUS PC"); | |
losp.addParametrsOfSystem(new ParametrsOfSystem(Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().freeMemory(), | |
Runtime.getRuntime().totalMemory(), Runtime.getRuntime().maxMemory(), System.getProperty("os.name").toLowerCase())); | |
ansver += "<html><head><title>ParametrsOfSystem</title> <meta charset='utf-8'></head><body><p>Список параметров PC: " | |
+ losp.getName() + "</p><br>"; | |
ansver += "<table border='2' cellpadding='5' ><tr><th>Количество ядер процессора</th><th>Свободная память</th>" | |
+ "<th>Общий объем памяти</th><th>Максимальный объем памяти</th><th>Операционная система</th>" | |
+ "<th>Номер запроса</th></tr>"; | |
ParametrsOfSystem[] array = losp.getListOfSystemParametrs(); | |
for (int i = 0; i < array.length; i += 1) { | |
ansver += "<tr>"; | |
ansver += "<td>" + array[i].getInfoProcessor() + "</td>"; | |
ansver += "<td>" + array[i].getInfoFreeMemory() + "</td>"; | |
ansver += "<td>" + array[i].getInfoTotalMemory() + "</td>"; | |
ansver += "<td>" + array[i].getInfoMaxMemory() + "</td>"; | |
ansver += "<td>" + array[i].getOS() + "</td>"; | |
ansver += "<td>" + count + "</td>"; | |
ansver += "</tr>"; | |
} | |
ansver += "</table></body></html>"; | |
try (ServerSocket soc = new ServerSocket(8080)) { | |
for (;;) { | |
Socket clisoc = soc.accept(); | |
Client cli = new Client(clisoc, ansver); | |
count += 1; | |
} | |
} catch (IOException e) { | |
System.out.println("Error to server Socket Open!!!"); | |
} | |
} | |
} |
This file contains 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
public class ParametrsOfSystem { | |
private long infoProcessor; | |
private long infoFreeMemory; | |
private long infoTotalMemory; | |
private long infoMaxMemory; | |
private String OS; | |
public ParametrsOfSystem(long infoProcessor, long infoFreeMemory, long infoTotalMemory, long infoMaxMemory, String OS) { | |
this.infoProcessor = infoProcessor; | |
this.infoFreeMemory = infoFreeMemory; | |
this.infoTotalMemory = infoTotalMemory; | |
this.infoMaxMemory = infoMaxMemory; | |
this.OS = OS; | |
} | |
public long getInfoProcessor() { | |
return infoProcessor; | |
} | |
public long getInfoFreeMemory() { | |
return infoFreeMemory; | |
} | |
public long getInfoTotalMemory() { | |
return infoTotalMemory; | |
} | |
public long getInfoMaxMemory() { | |
return infoMaxMemory; | |
} | |
public String getOS() { | |
return OS; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment