Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: TCP socket and Client - capturing in Wireshark

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default TCP socket and Client - capturing in Wireshark

    Hi,

    I am having problems with this code, I am trying to investigate what packets are transmitted when I try and connect a TCP server to a Client using my local machine. I have found the code on the internet and I can get the two to connect, however I do not see any packets within the wire shark environment. I am therefore assuming that I am doing something wrong?? I am from a background of C++, and really have never used Java before, so am very lost with the syntax of it all, but I understand what is going on.


    The following code came from: http://systembash.com/content/a-simp...nd-tcp-client/

    Below: TCP Client.


    import java.lang.*;
    import java.io.*;
    import java.net.*;
     
    class Client {
       public static void main(String args[]) {
          try {
             Socket skt = new Socket(MY IP ADDRESS IS QUOTATION MARKS, 2009);
             BufferedReader in = new BufferedReader(new
                InputStreamReader(skt.getInputStream()));
             System.out.print("Received string: '");
     
             while (!in.ready()) {}
             System.out.println(in.readLine()); // Read one line and output it
     
             System.out.print("'\n");
             in.close();
          }
          catch(Exception e) {
             System.out.print("Whoops! It didn't work!\n");
          }
       }
    }


    Below TCP server
    import java.lang.*;
    import java.io.*;
    import java.net.*;
     
    class Server {
       public static void main(String args[]) {
          String data = "Toobie ornaught toobie";
          try {
             ServerSocket srvr = new ServerSocket(2009);
             Socket skt = srvr.accept();
             System.out.print("Server has connected!\n");
             PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
             System.out.print("Sending string: '" + data + "'\n");
             out.print(data);
             out.close();
             skt.close();
             srvr.close();
          }
          catch(Exception e) {
             System.out.print("Whoops! It didn't work!\n");
          }
       }
    }


    Note: I have blocked out my outsider iP address for security reasons.


    I am curious to know If I need two copies of Eclipse open simultaneously, or If (like i have been doing) having one window open and running the two different classes together.


    Many thanks people (don't want to be sexist and say just say 'guys' !) :-) any help would be much appreciated!


    KJ


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: TCP socket and Client - capturing in Wireshark

    Quote Originally Posted by KingJohnno View Post
    ...
    I am having problems with this code...

    ...I am curious to know If I need two copies of Eclipse open simultaneously, or If (like i have been doing) having one window open and running the two different classes together....
    I think the code is OK.
    .
    Here's what I would do to test it: (In fact, here's what I did on my Centos 6.3 Linux system.)

    Forget about Eclipse for now. Here's the drill:

    1. Create a working directory for this project. Anywhere you want. (On my Linux system, I have a Java test directory under my home directory.) Make a brand new directory with nothing else in it.

    2. Open a command window and navigate to the project working directory that you just created.

    3. Open an editing session with your favorite text editor (gvim, right?). Paste in the server stuff and save as Server.java. Now, I changed the socket line to the following so that I can make absolutely, positively, unequivocally sure that everything that happens on my machine stays on my machine (just like Las Vegas) and won't go out over my LAN or WAN or the Internet or anywhere else.

      At least that's my fond hope when I am developing new network applications---don't get blacklisted by my ISP for spewing garbage. They really, really (really) don't like that. They're funny that way.

               Socket skt = new Socket("localhost",  2009); // Could use "127.0.0.1" but I like "localhost" for certain reasons.

    4. Open another editing session and paste in the client stuff and save as Client.java (You will probably find that you need to add import staements for java.io.* stuff and java.net.* stuff at the top of Client.java, right?)

    5. Execute javac Server.java and javac Client.java from the command line. Make any corrections needed to get a good, clean compile from both.

    6. Open another terminal window in your project working directory.

    7. Open Wireshark and set it to capture port "lo" or whatever it is that your system uses to identify 127.0.0.1 as localhost. Now we are ready for action.

    8. In one of the terminal windows, execute java Server
      You should see some stuff in the Wireshark capture window.

    9. In the other terminal window, execute java Client You should see some more activity in Wireshark, and equally importantly, you should see your expected messages in the Server command window and the Client command window.


      In the Server window:
      Server has connected!
      Sending string: 'Toobie ornaught toobie'

      Then the Server application closes and you are back at a command prompt in the server window.

      In the Client window:
      'Toobie ornaught toobie
      '

      Then the client application closes and you are back at the command prompt in the client window.

    10. In the client window execute java Client again. Now what happens?



    Cheers!

    Z

Similar Threads

  1. TCP/IP java client, c++ server
    By akboyd88 in forum Java Networking
    Replies: 0
    Last Post: March 24th, 2011, 10:46 AM
  2. Replies: 0
    Last Post: February 24th, 2011, 06:31 AM
  3. windowsize TCP socket
    By ra65ma in forum Java Networking
    Replies: 0
    Last Post: January 29th, 2011, 12:58 PM
  4. Decode Binary Packet via TCP Socket
    By maxice in forum Java Networking
    Replies: 0
    Last Post: November 8th, 2010, 03:38 PM
  5. TCP Client Server: Object Oriented
    By nffc luke in forum Object Oriented Programming
    Replies: 2
    Last Post: April 28th, 2010, 06:39 PM