import java.net.*; import java.io.*; public class SlidingWindowReceiver { private static int BUFFERSIZE = 5; private static int PORT = 5555; public static void main(String[] args) throws Exception { // create serversocket to listen for incoming connections ServerSocket serverSocket = null; serverSocket = new ServerSocket(PORT); // connect to client Socket clientSocket = null; clientSocket = serverSocket.accept(); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); // create buffer RecvBuffer rbuf = new RecvBuffer(BUFFERSIZE); String inputLine; String outputLine; while ((inputLine = in.readLine()) != null) { // let our Packet object parse the inputLine into ID and DATA: Packet packet = new Packet (inputLine); int packet_id = packet.id(); String data = packet.data(); int ack = rbuf.storepacket(packet_id, data); if (ack>=0) { /* send ack back */ out.println (Packet.formatACK(ack)); } else { /* ignore packet */ } // try to read next data packets while ((outputLine = (String)rbuf.getnextpacket()) != null) { System.out.print (outputLine); } } // when the client closed the connection, print all we got left while ((outputLine = (String)rbuf.getnextpacket()) != null) { System.out.print (outputLine); } System.err.print("Client closed connection!"); // clean-up out.close(); in.close(); clientSocket.close(); serverSocket.close(); } }