import java.net.*; import java.io.*; class acceptor implements Runnable { ServerSocket servsock; server parent; Thread t; acceptor(ServerSocket ss, server parent) { servsock = ss; this.parent = parent; t = new Thread(this); t.start(); } public void run() { System.out.println("wait for new connection."); while(true) { Socket tmpsock; try { tmpsock = servsock.accept(); } catch(IOException e) { System.out.println("accept error."); tmpsock = null; } System.out.println("New socket. " + tmpsock); parent.addSocket(tmpsock); } } } class server implements Runnable { public static void main(String args[]) { if(args.length == 0) { System.out.println("Usage: java server portno"); System.exit(0); } server serv; serv = new server(Integer.parseInt(args[0])); } int playerno = 5; Thread thread; ServerSocket servsock; Socket sock[] = new Socket[playerno]; // B?M?tJ,$@$1MQ0U$9$kB boolean using[] = new boolean[playerno]; // InputStream in[] = new InputStream[playerno]; OutputStream out[] = new OutputStream[playerno]; int timeout[] = new int[playerno]; // // B%?%$%%"%&%HB int timeout_max = 300; acceptor ac; // B%W%l%$%d!<$N>pJsB int x[] = new int[playerno]; int y[] = new int[playerno]; server(int port) { try { ServerSocket servsock = new ServerSocket(port); ac = new acceptor(servsock , this); } catch(IOException e) { System.out.println("Can't make ServerSocket."); } for(int i = 0 ; i < playerno ; i++) { using[i] = false; x[i] = y[i] = 50; // B=i4|0LCV$OE,EvB timeout[i] = 0; } thread = new Thread(this); // thread.start(); } // acceptorB$+$i8F$S=P$5$l$k!#%=%1%C%H$N6u$-$rC5$7$FEPO?$9$k!#B public void addSocket(Socket soc) { if(soc == null) return; for(int i = 0 ; i < playerno ; i++) { if(using[i] == false) { sock[i] = soc; try { in[i] = soc.getInputStream(); out[i] = soc.getOutputStream(); } catch(IOException e) { System.out.println("getting IN/OUT stream error."); return; } using[i] = true; System.out.println("player " + i + " is added."); return; } } System.out.println("player full."); try { soc.close(); } catch(IOException e) {} } void deletePlayer(int index) { timeout[index] = 0; try { sock[index].close(); in[index].close(); out[index].close(); } catch(IOException exc) {} using[index] = false; System.out.println("Closed socket. player=" + index); } // BF~NO$K1~$8$F0\F0$5$;$kB void playerMove(int index , int key) { int dx , dy; switch(key) { case 'h' : dx = -5; dy = 0; break; case 'j' : dx = 0; dy = 5; break; case 'k' : dx = 0; dy = -5; break; case 'l' : dx = 5; dy = 0; break; case 'q' : dx = dy = 0; deletePlayer(index); return; default : dx = dy = 0; break; } x[index] += dx; if(x[index] < 0) x[index] = 0; if(x[index] > 190) x[index] = 190; y[index] += dy; if(y[index] < 0) y[index] = 0; if(y[index] > 190) y[index] = 190; timeout[index] = 0; // B%?%$%%"%&%H$^$G$N;~4V$r85$KLa$9B } // BA40w$KBP$7$F0LCV$N>pJs$rAw?.$9$kB void sendForAll() { // B$^$:!"Aw$k>pJs$r:n$k!#%P%C%U%!$N%5%$%:$O%W%m%H%3%k$r;2>H!#B short sendbuf[] = new short[(playerno + 1) * 2 * 2]; int counter = 0; for(int i = 0 ; i < playerno ; i++) { if(using[i] == true) { sendbuf[counter++] = (short)x[i]; sendbuf[counter++] = (short)y[i]; } } sendbuf[counter++] = -1; // B%G!<%?$N:G8e$H$$$&0UL#B sendbuf[counter++] = -1; for(int i = 0 ; i < playerno ; i++) { if(using[i] == true) { try { DataOutputStream dout; dout = new DataOutputStream(out[i]); for(int j = 0 ; j < counter ; j++) { dout.writeShort(sendbuf[j]); } dout.flush(); // dout.close(); } catch(IOException e) { // B=q$-9~$_$,$&$^$/$$$+$J$+$C$?$i!"$=$N%W%l%$%d!<$rKu>C$9$k!#B System.out.println("dout ex."); deletePlayer(i); } } } } public void run() { while(true) { try { Thread.sleep(200); } catch(InterruptedException e) {} // B%5!<%P$N>uBV$rI=<($9$kB for(int i = 0 ; i < playerno ; i++) { if(using[i]) System.out.print("ON"); else System.out.print("OFF"); System.out.print(":" + x[i]); System.out.print(":" + y[i]); System.out.print(":" + timeout[i]); System.out.print(" "); } System.out.println(" "); // B$=$l$>$l$N@\B3$N=hM}B for(int i = 0 ; i < playerno ; i++) { if(using[i] == true) { if((++timeout[i]) > timeout_max) { deletePlayer(i); continue; } try { if(in[i].available() > 0) { // B2?$+>pJs$rAw$C$F$-$?$>B int r; r = in[i].read(); playerMove(i , r); sendForAll(); } } catch(IOException e) { // B%=%1%C%H$,$b$&;H$($J$$B deletePlayer(i); } } } } } }