java - How to demonstrate all the TCP Socket States -


i trying observe simple java echo server & client program in runtime on laptop using ubuntu os.to understand whats happening behind scene, ran server client & used command

   sudo netstat -pant 10007  proto recv-q send-q local address           foreign address         state       pid/program name  tcp6       0      0 :::10007                :::*                    listen      3551/java           tcp6       0      0 127.0.0.1:35459         127.0.0.1:10007         established 3570/java        tcp6       0      0 127.0.0.1:10007         127.0.0.1:35459         established 3551/java       

so, can see passive server(loop back) in listen state , after accepting client(127.0.0.1.35459) socket creates new tcp connection active server (127.0.0.1.10007) in established state. however, know there several tcp socket states such as:syn_send, syn_received, established, listen, fin_wait_1, timed_wait, close_wait, fin_wait_2, last_ack, closed etc. so, in confusion other states, whether program skipping these socket states or not.

i want see tcp socket states understand entire socket sate transition scenario. doing wrong? did gave wrong command? or missing code modification? lacking network tools ? went wrong in step step process?

so, how demonstrate all tcp socket states?

here server code:

import java.net.*;  import java.io.*;   public class echoserver  {   public static void main(string[] args) throws ioexception     {      serversocket serversocket = null;       try {           serversocket = new serversocket(10007);          }      catch (ioexception e)          {           system.err.println("could not listen on port: 10007.");           system.exit(1);          }       socket clientsocket = null;      system.out.println ("waiting connection.....");      try {           clientsocket = serversocket.accept();          }      catch (ioexception e)          {           system.err.println("accept failed.");           system.exit(1);          }       system.out.println ("connection successful");     system.out.println ("waiting input.....");      printwriter out = new printwriter(clientsocket.getoutputstream(),                                        true);      bufferedreader in = new bufferedreader(              new inputstreamreader( clientsocket.getinputstream()));       string inputline;       while ((inputline = in.readline()) != null)          {           system.out.println ("server: " + inputline);           out.println(inputline);            if (inputline.equals("bye."))               break;          }       out.close();      in.close();      clientsocket.close();      serversocket.close();     }  }  

and here client code:

import java.io.*; import java.net.*;  public class echoclient {     public static void main(string[] args) throws ioexception {          string serverhostname = new string ("127.0.0.1");          if (args.length > 0)            serverhostname = args[0];         system.out.println ("attemping connect host " +         serverhostname + " on port 10007.");          socket echosocket = null;         printwriter out = null;         bufferedreader in = null;          try {              echosocket = new socket(serverhostname, 10007);             out = new printwriter(echosocket.getoutputstream(), true);             in = new bufferedreader(new inputstreamreader(                                         echosocket.getinputstream()));         } catch (unknownhostexception e) {             system.err.println("don't know host: " + serverhostname);             system.exit(1);         } catch (ioexception e) {             system.err.println("couldn't i/o "                                + "the connection to: " + serverhostname);             system.exit(1);         }      bufferedreader stdin = new bufferedreader(                                    new inputstreamreader(system.in));     string userinput;          system.out.print ("input: ");     while ((userinput = stdin.readline()) != null) {         out.println(userinput);         system.out.println("echo: " + in.readline());             system.out.print ("input: ");     }      out.close();     in.close();     stdin.close();     echosocket.close();     } } 

the issue here is, os take care of states. can't controll l4 protocol java. if wanted that, have implement own tcp stack on linux raw sockets (maybe there implementation, haven't looked).

so, having os take care of states , using loopback testing, states have lifetime of few microseconds. (actually believe modern linux skip of when both ends @ same machine , there no actual network involved.) i'd trying see states through netstat has little no value in comprehending how tcp works.

i suggest, take @ wikipedia tcp page instead. describes states , has nice diagram depicting state transitions well.

if want see happens 'on wire', suggest try either tcpdump. tcpdump -n -i lo port 10007 (see manpage further usage, more detailed output, etc.), or gui app wireshark. wireshark better option you, since dissects packets , explains pretty every bit.

(actually tcpdump can used capture packets file - using -w file flag, , opened in wireshark later.)


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -