java
1 2import java.util.*; 3 4public class chatsev 5{ 6static final int Portnum=12345; 7 8static ServerSocket svSocket; 9static Vector connections; 10 11public static void sendAll(String s){ 12 if(connections !=null){ 13 for(Enumeration e =connections.elements(); 14 e.hasMoreElements();){ 15 try{ 16 PrintWriter out = new PrintWriter( 17 ((Socket)e.nextElement()).getOutputStream()); 18 out.println(s); 19 out.flush(); 20 }catch(IOException ex){} 21 } 22 } 23 System.out.println(s); 24 25 } 26 27 public static void addConnection(Socket s){ 28 if(connections == null){ 29 connections = new Vector(); 30 } 31 connections.addElement(s); 32 } 33 34 public static void deleteConnection(Socket s){ 35 if(connections != null){ 36 connections.removeElement(s); 37 } 38 } 39 40 public static void main(String[] arg){ 41 try{ 42 svSocket =new ServerSocket(Portnum); 43 }catch (IOException e){ 44 System.err.println(e); 45 System.exit(1); 46 } 47 48 while(true){ 49 try{ 50 Socket cs =svSocket.accept(); 51 addConnection(cs); 52 53 Thread ct =new Thread(new clientProc(cs)); 54 ct.start(); 55 56 }catch (IOException e){ 57 System.err.println(e); 58 } 59 60 } 61 62 63 } 64 65} 66 67class clientProc implements Runnable{ 68 69 70 Socket s; 71 72 BufferedReader in; 73 PrintWriter out; 74 String name =null; 75 76 public clientProc(Socket s) throws IOException{ 77 this.s=s; 78 in=new BufferedReader(new InputStreamReader(s.getInputStream())); 79 out = new PrintWriter(s.getOutputStream()); 80 } 81 82 83 84 public void run(){ 85 86 try{ 87 while (name ==null){ 88 name = in.readLine(); 89 90 } 91 92 chatsev.sendAll("::"+name+"さんが入室しました"); 93 94 String line =in.readLine(); 95 while(!"logout".equals(line)){ 96 97 chatsev.sendAll(name +">"+line); 98 line =in.readLine(); 99 } 100 101 chatsev.sendAll("::"+name+"さんが退室しました"); 102 chatsev.deleteConnection(s); 103 s.close(); 104 105 }catch (IOException e){ 106 try{ 107 s.close(); 108 }catch (IOException e2){} 109 110 } 111 112 } 113 114 115} 116```書籍を参考にチャットのサンプルプログラムを動かそうとしたのですが、 117 118 119警告:[rawtypes] raw型が見つかりました: Vector 120static Vector connections; 121 ^ 122 汎用クラスVector<E>の型引数がありません 123 Eが型変数の場合: 124 クラス Vectorで宣言されているE extends Object 125chatsev.java:14: 警告:[rawtypes] raw型が見つかりました: Enumeration 126 for(Enumeration e =connections.elements(); 127 ^ 128 汎用クラスEnumeration<E>の型引数がありません 129 Eが型変数の場合: 130 インタフェース Enumerationで宣言されているE extends Object 131chatsev.java:30: 警告:[rawtypes] raw型が見つかりました: Vector 132 connections = new Vector(); 133 ^ 134 汎用クラスVector<E>の型引数がありません 135 Eが型変数の場合: 136 クラス Vectorで宣言されているE extends Object 137chatsev.java:32: 警告:[unchecked] raw型VectorのメンバーとしてのaddElement(E)への無検査呼出しです 138 connections.addElement(s); 139 ^ 140 Eが型変数の場合: 141 クラス Vectorで宣言されているE extends Object 142警告4個 143 144 145このように警告が出てしまいました。 146 147おそらく書籍が出版されたのが2004年で現在のjavaのバージョンに対応していないためと思うのですが、どう変えれば良いでしょうか? 148 149 150 151 152 153 154 155 156 157 158 よろしくお願いします。
あなたの回答
tips
プレビュー