Saturday, August 17, 2013

TCP- Close wait simulation - Part 3

Lets try to do some dirty way of coding to produce close_wait!

Check out my server code! I am opening up new socket and waiting for request from client!
Once request is received, writing out a message to client and closing the connection then and there!
Server Code

import java.io.*;
import java.net.*;
/**
* Created with IntelliJ IDEA.
* User: vanji
* Date: 5/17/13
* Time: 9:31 AM
*/
public class Server
{
public static void main(String[] args) throws Exception
{
Socket socket = new ServerSocket(12345).accept();
OutputStream out = socket.getOutputStream();
out.write("Hello World\n".getBytes());
out.flush();
out.close();
}
}
view raw Server.java hosted with ❤ by GitHub

In Client code I am creating new connection with Server and puting the clinet into sleep mode with out closing the connection.
Client Code
import java.io.*;
import java.net.*;
/**
* Created with IntelliJ IDEA.
* User: vanji
* Date: 5/17/13
* Time: 9:31 AM
* To change this template use File | Settings | File Templates.
*/
public class Client{
public static void main(String[] args) throws Exception
{
Socket socket = new Socket(InetAddress.getByName("localhost"), 12345);
InputStream in = socket.getInputStream();
int c;
while ((c = in.read()) != -1)
{
System.out.write(c);
}
System.out.flush();
// should now be in CLOSE_WAIT
Thread.sleep(Integer.MAX_VALUE);
}
}
view raw Client.java hosted with ❤ by GitHub


This will results as bellow!


No comments:

Post a Comment