Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Last active March 25, 2016 22:36
Show Gist options
  • Select an option

  • Save Viacheslav77/2bf2f572f6fcee477ab1 to your computer and use it in GitHub Desktop.

Select an option

Save Viacheslav77/2bf2f572f6fcee477ab1 to your computer and use it in GitHub Desktop.
Написать сервер, который будет отправлять пользователю информацию о системе и номер запроса.
Windows 7 Operating system. Version: 6.1OS: Windows_NT
Processor: - Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
architecture - AMD64
cores - 2
RAM: 31488 Gb
Free memory: 30668 Gb
Request number: 8
package MyServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static Integer counterRequest = 0;
private int port;
private Thread thread;
public Server(int port) {
this.port = port;
}
public void stop(){
thread.interrupt();
}
public void start() throws IOException {
Thread thread = new Thread(){
@Override
public void run(){
try (ServerSocket server = new ServerSocket(port)){
while (!isInterrupted()) {
try (Socket socket = server.accept();
OutputStream output = socket.getOutputStream()){
output.write(getData().toString().getBytes());
}
}
} catch (Exception ex) {
System.out.println("Error to server Socket Open!!!");
}
}
};
thread.start();
}
private StringBuilder getData() {
Runtime run = Runtime.getRuntime();
StringBuilder sb = new StringBuilder(System.getProperty("os.name") +" Operating system. Version: " + System.getProperty("os.version"));
sb.append("OS: ").append(System.getenv("OS"))
.append("\n\n Processor: - ").append(System.getenv("PROCESSOR_IDENTIFIER"))
.append("\n architecture - ").append(System.getenv("PROCESSOR_ARCHITECTURE"))
.append("\n cores - ").append(Runtime.getRuntime().availableProcessors())
.append("\n\n RAM: " + (run.totalMemory() / 2048) + " Gb")
.append("\n Free memory: " + (run.freeMemory() / 2048) + " Gb")
.append("\n\nRequest number: ").append(++counterRequest);
return sb;
}
}
package MyServer;
/*
Написать сервер, который будет отправлять пользователю информацию о системе и номер запроса.
*/
import java.io.IOException;
public class Main {
public static void main(String[] args) {
Server server = new Server(10000);
try {
server.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment