Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.InetAddress;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8. import java.text.SimpleDateFormat;
  9. import java.util.ArrayList;
  10. import java.util.Date;
  11.  
  12. /**
  13. * ラズパイ上で動かすチャットのサーバークラスです
  14. * @author reishisu
  15. */
  16. public class ChatServer {
  17.  
  18. /**
  19. * ソケット通信を行うポート
  20. */
  21. public static final int PORT = 25252;
  22. /**
  23. * チャットサーバーの窓口
  24. */
  25. private static ServerSocket server_socket;
  26. /**
  27. * 接続してきたIPアドレス
  28. */
  29. private static ArrayList<InetAddress> client_list = new ArrayList<>();
  30.  
  31. /**
  32. * メイン関数
  33. * @param args コマンドライン引数
  34. */
  35. public static void main(String[] args) {
  36. try {
  37. // 既にサーバーが建っていない時サーバーソケットの生成
  38. if (server_socket == null) {
  39. server_socket = new ServerSocket(PORT);
  40. System.out.println("サーバー稼働中...");
  41. }
  42.  
  43. // 受信カウンタ
  44. int count = 1;
  45.  
  46. // サーバー稼働
  47. while (true) {
  48. // クライアントからの接続が来るまで待機
  49. Socket client_socket = server_socket.accept();
  50.  
  51. // 新規か確認
  52. boolean is_new = true;
  53. for(InetAddress ip : client_list) {
  54. if (ip.toString().equals(client_socket.getInetAddress().toString())) {
  55. is_new = false;
  56. } else {
  57. System.out.println("違う?" + ip + "==" + client_socket.getInetAddress());
  58. }
  59. }
  60. if (is_new) {
  61. client_list.add(client_socket.getInetAddress());
  62. System.out.println("追加された:" + client_socket.getInetAddress().toString() + ":" + client_list.size());
  63. }
  64.  
  65. // 計測開始
  66. long start_time = System.currentTimeMillis();
  67.  
  68. // クライアントから文字列をバイト列で受信
  69. InputStream input_stream = client_socket.getInputStream();
  70. DataInputStream data_input_stream = new DataInputStream(input_stream);
  71.  
  72. // 文字列を受け取るバッファー
  73. byte[] buffer = new byte[4096];
  74. int byte_count = 0;
  75. String message = "";
  76. try {
  77. byte_count = data_input_stream.read(buffer);
  78. if (byte_count == 0) return;
  79. String view_string = new String(buffer, "UTF-8");
  80. long end_time = System.currentTimeMillis();
  81. SimpleDateFormat time_stamp = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒");
  82. message += count + " IPアドレス:" + ("" + client_socket.getInetAddress()).split("/")[1] + ":" +
  83. time_stamp.format(new Date()) + " [" + (end_time - start_time) + "ms]" + "\n" + view_string + "\n";
  84. System.out.println(message);
  85. count++;
  86.  
  87. send(message);
  88. }
  89. catch (Exception e) {
  90. long end_time = System.currentTimeMillis();
  91. SimpleDateFormat time_stamp = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒");
  92. message += count + " IPアドレス:" + ("" + client_socket.getInetAddress()).split("/")[1] + ":" +
  93. time_stamp.format(new Date()) + " [" + (end_time - start_time) + "ms]" + "\n受信失敗...\n";
  94. System.out.println(message);
  95. count++;
  96.  
  97. send(message);
  98. }
  99. }
  100. }
  101. catch (Exception e) {
  102. System.err.println("エラー発生!!");
  103. e.printStackTrace();
  104. }
  105. }
  106.  
  107. public static void send(String message) {
  108. byte[] buffer = new byte[4096];
  109. DataOutputStream dataOutputStream = null;
  110. try {
  111. buffer = message.getBytes("UTF-8");
  112.  
  113. for (InetAddress ip : client_list) {
  114. try {
  115. dataOutputStream = new DataOutputStream(new Socket(ip.toString().split("/")[1], PORT).getOutputStream());
  116. dataOutputStream.write(buffer);
  117. dataOutputStream.flush();
  118. dataOutputStream.close();
  119. } catch (Exception e) {
  120. }
  121. }
  122.  
  123. } catch (IOException e) {
  124. // TODO 自動生成された catch ブロック
  125. e.printStackTrace();
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement