Created
July 9, 2019 20:04
Star
You must be signed in to star a gist
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.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.InetAddress; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
/** | |
* ラズパイ上で動かすチャットのサーバークラスです | |
* @author reishisu | |
*/ | |
public class ChatServer { | |
/** | |
* ソケット通信を行うポート | |
*/ | |
public static final int PORT = 25252; | |
/** | |
* チャットサーバーの窓口 | |
*/ | |
private static ServerSocket server_socket; | |
/** | |
* 接続してきたIPアドレス | |
*/ | |
private static ArrayList<InetAddress> client_list = new ArrayList<>(); | |
/** | |
* メイン関数 | |
* @param args コマンドライン引数 | |
*/ | |
public static void main(String[] args) { | |
try { | |
// 既にサーバーが建っていない時サーバーソケットの生成 | |
if (server_socket == null) { | |
server_socket = new ServerSocket(PORT); | |
System.out.println("サーバー稼働中..."); | |
} | |
// 受信カウンタ | |
int count = 1; | |
// サーバー稼働 | |
while (true) { | |
// クライアントからの接続が来るまで待機 | |
Socket client_socket = server_socket.accept(); | |
// 新規か確認 | |
boolean is_new = true; | |
for(InetAddress ip : client_list) { | |
if (ip.toString().equals(client_socket.getInetAddress().toString())) { | |
is_new = false; | |
} else { | |
System.out.println("違う?" + ip + "==" + client_socket.getInetAddress()); | |
} | |
} | |
if (is_new) { | |
client_list.add(client_socket.getInetAddress()); | |
System.out.println("追加された:" + client_socket.getInetAddress().toString() + ":" + client_list.size()); | |
} | |
// 計測開始 | |
long start_time = System.currentTimeMillis(); | |
// クライアントから文字列をバイト列で受信 | |
InputStream input_stream = client_socket.getInputStream(); | |
DataInputStream data_input_stream = new DataInputStream(input_stream); | |
// 文字列を受け取るバッファー | |
byte[] buffer = new byte[4096]; | |
int byte_count = 0; | |
String message = ""; | |
try { | |
byte_count = data_input_stream.read(buffer); | |
if (byte_count == 0) return; | |
String view_string = new String(buffer, "UTF-8"); | |
long end_time = System.currentTimeMillis(); | |
SimpleDateFormat time_stamp = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒"); | |
message += count + " IPアドレス:" + ("" + client_socket.getInetAddress()).split("/")[1] + ":" + | |
time_stamp.format(new Date()) + " [" + (end_time - start_time) + "ms]" + "\n" + view_string + "\n"; | |
System.out.println(message); | |
count++; | |
send(message); | |
} | |
catch (Exception e) { | |
long end_time = System.currentTimeMillis(); | |
SimpleDateFormat time_stamp = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒"); | |
message += count + " IPアドレス:" + ("" + client_socket.getInetAddress()).split("/")[1] + ":" + | |
time_stamp.format(new Date()) + " [" + (end_time - start_time) + "ms]" + "\n受信失敗...\n"; | |
System.out.println(message); | |
count++; | |
send(message); | |
} | |
} | |
} | |
catch (Exception e) { | |
System.err.println("エラー発生!!"); | |
e.printStackTrace(); | |
} | |
} | |
public static void send(String message) { | |
byte[] buffer = new byte[4096]; | |
DataOutputStream dataOutputStream = null; | |
try { | |
buffer = message.getBytes("UTF-8"); | |
for (InetAddress ip : client_list) { | |
try { | |
dataOutputStream = new DataOutputStream(new Socket(ip.toString().split("/")[1], PORT).getOutputStream()); | |
dataOutputStream.write(buffer); | |
dataOutputStream.flush(); | |
dataOutputStream.close(); | |
} catch (Exception e) { | |
} | |
} | |
} catch (IOException e) { | |
// TODO 自動生成された catch ブロック | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment