public static void main(String[] args) throws Exception {
		int sendport = 6001;
		DatagramSocket socket = new DatagramSocket(6001);
		long server_isn = 9000L;
		long mws = 0L;
		long mss = 0L;
		String filename = null;
		long sendbase = 0L;
		TreeSet<MyPacket> windows = new TreeSet<MyPacket>();
		long originalbase = 0L;
		while (true) {
			//default mtp mss is 536, plus header 20byte
			//TODO        //70
			DatagramPacket request = new DatagramPacket(new byte[68], 68);	
			socket.receive(request);
			System.out.println("---receive---");
			showPacket(request);
			System.out.println("-------------");
			// Send SYN ACK
			InetAddress clientHost = request.getAddress();
			int clientPort = request.getPort();
			String packetType = getPacketType(request);
			//server sequence number is 8000
			if(packetType.equals("syn")){
				mws = getmws(request);
				mss = getmss(request);
				System.out.println("mws:" + mws + "mss" + mss);
				//send ack syn
				byte[] synackpack = handshakePacket(sendport, request.getPort(), server_isn, getseqno(request) + 1, "synack",mws,mss);
				DatagramPacket synack = new DatagramPacket(synackpack, synackpack.length,clientHost, clientPort);
				writeLog("receive SYN packet",request);
				socket.send(synack);
				System.out.println("---sent syn ack---");
				writeLog("send SYN ACK", synack);
				//show
//				showPacket(synack);
			}else if (packetType.equals("ackpsh")){
				long sn = getseqno(request);
				long acn = getackno(request);
				MyPacket hpacket = new MyPacket(sn, acn, request);
				//add into windows buffer
				windows.add(hpacket);
				//response ack 
				//System.out.println("get data packet");
				byte[] datapart = getdata(request);
				////////////////////////
				printData(datapart);
				//System.out.println("\ndatapart leng" + request.getData().length);
				/////////////////////////
				System.out.println("\n");
				//file writer
				long seqno = getseqno(request); //seq#: 9001, ack#
				byte[] ackcontent;
				
				if(windows.size() == mws){ //5
					//windows is now full
					System.out.println("!! Windows full, send: " + (originalbase + mws + mss));
					ackcontent = ackPacket(sendport, request.getPort(), server_isn+1, originalbase + mws + mss);//datapart.length
					DatagramPacket pk = new DatagramPacket(ackcontent, ackcontent.length,clientHost, clientPort);
					socket.send(pk);
					writeLog("window full",pk);
				}
				
				//packet received is not expected
				if(seqno != sendbase){
					//repeating old sequence number
					ackcontent = ackPacket(sendport, request.getPort(), server_isn+1, sendbase);//datapart.length
					DatagramPacket pk = new DatagramPacket(ackcontent, ackcontent.length,clientHost, clientPort);
					//////////////////This is all right
					writeLog("receive ACKPSH packet",request);
					socket.send(pk);
					
					System.out.println("1send ACK back, ackno: " + sendbase);
					writeLog("response ACK back",pk);
				}else if(seqno == sendbase){  //match expected number
					//problematic ////////////////////
					writeData(datapart,filename);
					
					//cumulative ACK number 
					sendbase = seqno + mss;
					ackcontent = ackPacket(sendport, request.getPort(), server_isn+1, sendbase);//datapart.length
					DatagramPacket pk = new DatagramPacket(ackcontent, ackcontent.length,clientHost, clientPort);
					//write to log file
					writeLog("receive ACKPSH packet",request);
					socket.send(pk);

					System.out.println("2send ACK back, ackno: " + (seqno + mss));
					writeLog("response ACK back",pk);
				}
				
				
			}else if(packetType.equals("fin")){
				writeLog("receive FIN packet",request);
				//strin zero and weird char in file
				////////////////////
				cleanfile(filename);
				File f = new File("mtp_receiver.log");
				f.setWritable(true);
				cleanLog("mtp_receiver.log");
				socket.close();
				System.exit(0);
			}else if (packetType.equals("ast")){
				//get filename from itls
				filename = getfilename(request);
				//get initial sequence number from sender
				//TODO:chang it
				sendbase = 8001L;
				originalbase = sendbase;
			}
		}//end while
		
	}