Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Q7

When you access a web server using multiple tabs in your web browser, the netstat command
will show that there are multiple active TCP connections to the web server. Netstat will show
the transport-layer states and events for each of these connections.

For example, when you first initiate a connection to the web server, netstat will show the state
of the connection as "SYN_SENT", which indicates that the client has sent a SYN (synchronize)
packet to the server to start the connection. The server will then respond with a SYN-ACK
(synchronize-acknowledge) packet, and the client will send an ACK (acknowledge) packet to
complete the connection. At this point, the state of the connection will change to
"ESTABLISHED", which indicates that the connection has been successfully established and data
can be exchanged between the client and the server.

If you open additional tabs in your web browser and access the same web server, netstat will
show additional "ESTABLISHED" connections for each of these tabs. When you close a tab in
your web browser, the corresponding connection will be closed and the state of the connection
will change to "FIN_WAIT_1" (waiting for the server to close the connection). The server will
then send a FIN (finish) packet to the client to close the connection, and the state of the
connection will change to "FIN_WAIT_2" (waiting for the client to acknowledge the server's FIN
packet). The client will then send an ACK packet to the server, and the state of the connection
will change to "TIME_WAIT" (waiting for the server to close the connection). Finally, the server
will close the connection, and the state of the connection will change to "CLOSED".
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class MultiThreadedWebServer {

public static void main(String[] args) throws Exception {


if (args.length != 2) {
System.out.println("Usage: java WebServer <port> <directory>");
System.exit(1);
}

// Get the port number and directory from the command line arguments
int port = Integer.parseInt(args[0]);
String directory = args[1];

try {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Web server listening on port " + port + " with web root at " +
directory);

while (true) {
Socket socket = serverSocket.accept();
WebRequest request = new WebRequest(socket,directory);
// Create a new thread to process the request
Thread thread = new Thread(request);

// Start the thread


thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

You might also like