// //ReplayServer class // //s90524宅間大介 // //Replay防止用のIDを持つpacketを解釈する。 //headerは //時刻ID(4バイト)・通し番号ID(2バイト)・ //type・文におけるpacket番号・packetの長さ・中身 //というフォーマットになっている。 import java.io.* ; import java.net.* ; import java.util.* ; import java.lang.* ; public class ReplayServer{ public static void main(String args[]){ ServerSocket servsock=null ;//ServerSocket Socket sock=null ; OutputStream outstrm=null ; BufferedReader in=null ;//要求読み取り用 //packetのheaderと内容を一時的に保存するもの String pack="" ;//packet全体 String timeID="" ;//timeID(replay防止用) int numID=0 ;//通し番号ID(replay防止用) int type=0 ;//通常は0、文の終わりのpacketでは1 int packnum=0 ;//文におけるpackの番号 int packlg=0 ;//packetの中身の長さ String contents="" ;//packetの中身 String str[][] ;//各packetのcontentsを収納する String cmpstr[] ;//出来あがった文を収納する String timeIDs[] ;//timeIDの保存 int receive[][]=new int[5][10] ;//受けとったものは1、特に文の最後のpacketは2 boolean success=true ; byte ok[]={'o','k',13,10} ; byte error[]={'e','r','r','o','r',13,10} ; boolean tf=true ; str=new String[5][10] ; cmpstr=new String[5] ; timeIDs=new String[5] ; for(int n=0 ;n<5 ;n++){packnum=-1 ;} for(int n=0 ;n<5 ;n++){ for(int nn=0 ;nn<10 ;nn++){receive[n][nn]=0 ;} } for(int n=0 ;n<5 ;n++){timeIDs[n]="" ;} //接続 try{ servsock=new ServerSocket(6000,10) ; sock=servsock.accept() ; System.out.println("Connection is established .") ; } catch(Exception e){ System.out.println("Connection error .") ; System.exit(1) ; } //読み書きオブジェクトの取得 try{ in=new BufferedReader(new InputStreamReader(sock.getInputStream())) ; outstrm=sock.getOutputStream() ; } catch(Exception e){System.exit(1) ;} //main loop while(tf){ //受信 try{pack=in.readLine() ;} catch(Exception e){ System.out.println("Reading Error .") ; tf=false ; } System.out.println("Receive : "+pack) ; try{ //packetの解読 timeID=pack.substring(0,4) ; numID=Integer.parseInt(pack.substring(4,6)) ; type=Integer.parseInt(pack.substring(6,7)) ; packnum=Integer.parseInt(pack.substring(7,8)) ; packlg=Integer.parseInt(pack.substring(8,9)) ; contents=pack.substring(9,9+packlg) ; } catch(Exception e){System.out.println("str error") ;} //type check if(type==1){receive[numID%5][packnum]=2 ;}//typeが1の時はreceiveに2と記録 else{receive[numID%5][packnum]=1 ;}//そうでない時は1と記録 //timeID check if(timeIDs[numID%5].equals("")){timeIDs[numID%5]=timeID ;} if(timeIDs[numID%5].equalsIgnoreCase(timeID)){} else{success=false ;} //strへの格納 str[numID%5][packnum]=contents ; //出来あがった文の表示 for(int n=0 ;n<5 ;n++){ int nn=0 ; while(receive[n][nn]>0){ if(receive[n][nn]==2){//n番の文は全て受け取った事になる System.out.print(n+"th sentence : ") ; for(int ns=0 ;ns