Javaで通信プログラムを書こうとしています
まだ初心者なのでわからないことだらけなのですが回答宜しくい願いします
問題はクライアントから受け取った文字列を接続しているクライアントすべてに送信するプログラムなのですがクライアントに送信できないことが問題です
エラーは出ていません
以下作成したコードです(インポート文は省略させていただきます
========サーバー========
public class ServerBace {
static ServerSocket SSocket = null;
static Socket socket = null;
static Vector<Player> PlayerList = new Vector<Player>(0);
public static void main (String[] args) { while (true) { try { SSocket = new ServerSocket(8080); System.out.println("サーバーを起動しました"); while (true) { socket = SSocket.accept(); System.out.println(socket.getLocalSocketAddress() + "と接続しています"); new Thread(new Player(socket)).start(); socket = null; } } catch (Exception e) { } } } public static void sendMessage (String message) { try { System.out.println("送信メッセージ:" + message); for (int count = 0; count < PlayerList.size(); count ++) { PlayerList.get(count).OUT.write(message); PlayerList.get(count).OUT.newLine(); PlayerList.get(count).OUT.flush(); } } catch (Exception e) { System.out.println(e); } } public static class Player implements Runnable { Socket socket = null; BufferedWriter OUT = null; BufferedReader IN = null; public Player (Socket socketDummy) { try { socket = socketDummy; OUT = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); IN = new BufferedReader(new InputStreamReader(socket.getInputStream())); if (PlayerList.size() < 2) { PlayerList.add(this); } else { OUT.write("セッション数が上限を超えました"); OUT.newLine(); OUT.flush(); System.out.println(socket.getInetAddress() + "がセッション上限に達したため切断されました"); socket.close(); } } catch (Exception e) { } } public void run () { try { while (true) { String Message = IN.readLine(); sendMessage(Message); } } catch (Exception e) { System.out.println(e); } finally { try { System.out.println(socket.getInetAddress() + "とセッションを切断します"); socket.close(); OUT.close(); IN.close(); } catch (Exception e) { } } } }
}
=========クライアント=========
public class Client {
static Socket socket = null;
static PrintWriter OUT = null;
static BufferedReader IN = null;
public static void main (String[] args) { Scanner scan = new Scanner(System.in); try { socket = new Socket(); socket.connect(new InetSocketAddress(args[0], 8080)); OUT = new PrintWriter(socket.getOutputStream(), true); IN = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (true) { String Message = scan.nextLine(); if (Message.equals("exit")) { break; } OUT.println(Message); OUT.flush(); } scan.close(); } catch (Exception e) { System.out.println(e); } finally { try { socket.close(); OUT.close(); IN.close(); System.out.println("セッションを終了しました"); } catch (Exception e) { } } }
}
何か間違いがあればご指摘お願いします
回答1件
あなたの回答
tips
プレビュー