Advertisement
Alice_Kim

turing test socket draft

Jan 19th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. package pgdp.test;
  2.  
  3. import org.junit.jupiter.api.BeforeEach;
  4. import org.junit.jupiter.api.Test;
  5.  
  6. import java.io.*;
  7. import java.net.ConnectException;
  8. import java.net.ServerSocket;
  9. import java.net.Socket;
  10. import java.net.UnknownHostException;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.List;
  14.  
  15. public class PinguinTest {
  16.  
  17. @BeforeEach
  18. public static void main(String[] args) {
  19. Socket sock = null;
  20. boolean server = false;
  21. try {
  22. // connection established
  23. ServerSocket serverSock = new ServerSocket(25565);
  24. System.out.println("Server started, waiting for connections...");
  25. sock = serverSock.accept();
  26. serverSock.close();
  27. server = true;
  28. } catch (UnknownHostException e) {
  29. System.err.println("Unknown host, try again!");
  30. } catch (NumberFormatException e) {
  31. System.err.println("Port invalid, try again!");
  32. } catch (ConnectException e) {
  33. System.err.println("Connection refused, try again!");
  34. } catch (IOException e) {
  35. System.err.println("I/O error, try again!");
  36. }
  37.  
  38. try (BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  39. PrintWriter out = new PrintWriter(sock.getOutputStream(), true)) {
  40. if (server) {
  41. System.out.println("Connected! You can now send something.");
  42. String input = in.readLine();
  43. if (input.equals("Was ergibt die folgende Summe?")) {
  44. BufferedReader in2 = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  45. input = in2.readLine();
  46. if (input.matches("<int>( + <int>)*")) {
  47. out.println(input);
  48. out.flush();
  49. long sum = Arrays.stream(args[0].split("\\\\d+")).mapToLong(Long::parseLong).sum();
  50. System.out.println(sum);
  51. } else {
  52. System.err.println("Das ist keine Summe!");
  53. }
  54. } else if (input.equals("Zähle die Fische in")) {
  55. BufferedReader in2 = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  56. input = in2.readLine();
  57. if (input.length() == 0) {
  58. System.out.println(0);
  59. }
  60. List<String> words = new ArrayList<String>();
  61. Arrays.stream(input.split("\\\\w+")).forEach(word -> {
  62. int i = Integer.parseInt(word);
  63. if (i == 0 && word.contains("Fisch") || word.contains("fisch")) {
  64. words.add(word);
  65. }
  66. });
  67. int quantity = (int) words.stream().count();
  68. out.println(input);
  69. out.flush();
  70. System.out.println(quantity);
  71. }
  72.  
  73. // jump to step 1
  74. PinguinTest.main(new String[1]);
  75. if (!args[0].equals("Was ergibt die folgende Summe?") || !args[0].equals("Zähle die Fische in")) {
  76. out.println(args[0]);
  77. out.flush();
  78. System.err.println("Tut mir leid, das habe ich nicht verstanden.");
  79. }
  80. BufferedReader in3 = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  81. args[0] = in3.readLine();
  82. if (args[0].contains("Bye") || args[0].contains("Goodbye") || args[0].contains("Servus")
  83. || args[0].contains("Bis dann")) {
  84. out.println(args[0]);
  85. out.flush();
  86. System.out.println(args[0]);
  87. } else {
  88. System.out.println("See ya in a bit.");
  89. }
  90. }
  91. } catch (IOException ex) {
  92. System.err.println("Connection error, exit.");
  93. } finally {
  94. try {
  95. sock.close();
  96. } catch (IOException e) {
  97. System.err.println("Connection failed.");
  98. }
  99. }
  100. }
  101.  
  102.  
  103. @Test
  104. public void Test01() {
  105. // tomorrow
  106. }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement