write a Java program that simulates the TCP/IP protocol stack, as per the follow
ID: 3657511 • Letter: W
Question
write a Java program that simulates the TCP/IP protocol stack, as per the following specification. A partial implementation of the program is provided below complete this program to meet all the requirements in the specification below.
Program Specification:
1. The program will have separate classes that represent each layer in the TCP/IP protocol stack.
2. Application Layer: there will be two application layer programs, a sender program, SenderApp.java, and a receiver program, ReceiverApp.java. A complete implementation of these two programs is provided. The sender program will take a string of text from the user and pass it to the transport layer in the sender
Explanation / Answer
public static void main(String... args) throws IOException, InterruptedException { ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.bind(new InetSocketAddress(0)); // open on a random port InetSocketAddress remote = new InetSocketAddress("localhost", ssc.socket().getLocalPort()); SocketChannel sc = SocketChannel.open(remote); configure(sc); SocketChannel accept = ssc.accept(); configure(accept); ByteBuffer bb = ByteBuffer.allocateDirect(16 * 1024 * 1024); // write as much as you can while (sc.write(bb) > 0) Thread.sleep(1); System.out.println("The socket write wrote " + bb.position() + " bytes."); } private static void configure(SocketChannel socketChannel) throws IOException { socketChannel.configureBlocking(false); socketChannel.socket().setSendBufferSize(8); socketChannel.socket().setReceiveBufferSize(8); }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.