Search code examples
socketswiresharktcpclient

How can I find my packets of my Java application on Wireshark?


I'm using Windows 10.

I have this server program on Java:

    import java.io.*;
import java.net.*;

public class TCPServer {
    
    public static final int TCP_SERVER_PORT = 6789;    //mer eller mindre tilfeldig valg portnr.
    
    public static void main(String args[]) throws Exception
    {
        String setningFraKlient;
        String oversattSetning;

        //Lytter på port TCP_SERVER_PORT:
        System.out.println("Lytter paa TCP-port " + String.valueOf(TCP_SERVER_PORT) + " ...");     
        
        ServerSocket welcomeSocket = new ServerSocket(TCP_SERVER_PORT);
        while(true) {
            //Tjeneren venter, 'henger' på accept(), på at klienten skal ta kontakt:
            Socket connectionSocket = welcomeSocket.accept();
          
            //Fortsetter, skriver ut IP-adressen til klienten:
            System.out.println("Kontakt med: " + connectionSocket.getRemoteSocketAddress().toString());     
            
            //Kopler connectionsocket til InputStream:
            InputStreamReader isr = new InputStreamReader(connectionSocket.getInputStream());
    
            BufferedReader fraKlient = new BufferedReader(isr);
            
            //Kopler OutputStream til connectionsocket:
            DataOutputStream tilKlient = new DataOutputStream(connectionSocket.getOutputStream());

            //Leser data fra klienten:
            setningFraKlient = fraKlient.readLine();
            System.out.println("Tekst fra klient: " + setningFraKlient);     
            
            //Oversetter til store bokstaver:
            oversattSetning = setningFraKlient.toUpperCase() + '\n';
            System.out.println("Gjør om til store bokstaver, returnerer:" + oversattSetning);
            
            //Sender store bokstaver i retur:
            tilKlient.writeBytes(oversattSetning);
        }
    }
}

And this client program on Java:

import java.io.*;
import java.net.*;

public class TCPClient {
    public static void main(String args[]) throws Exception
    {
        String sentence="";
        String modifiedSentence="";

        //Oppretter forbindelse mot localhost på port 6789:
        Socket clientSocket = new Socket("localhost", TCPServer.TCP_SERVER_PORT);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        //Spør bruker etter tekst:
        System.out.print("Skriv inn tekst: ");
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        sentence = inFromUser.readLine();
        outToServer.writeBytes(sentence + '\n');
        modifiedSentence = inFromServer.readLine();

        //Skriv ut tekst som store bokstaver:
        System.out.println("\nTekst som store bokstaver: ");
        System.out.println(modifiedSentence);

        //Lukk socket og avslutt:
        clientSocket.close();
    }
}

As you see on the server code, I use "localhost" to test the program and I use the port number 6789.

When I run the program it just works as expected:

  1. I first run the server program.
  2. Then I run the client program.
  3. I write some message as a string.
  4. I get the string back where all letters are converted to upper case.

I start WireShark and do the following:

  1. I click "Go -> Options" and there I select the what I use to connect to internet "which is Wi-fi". "Link-layer header" is "Ethernet".
  2. Then I see on the Wireshark that different packets or coming (UDPs, TCPs, TLSs etc.)
  3. I start Java program on Ecplipse, type in string, send it and receive an uppercase version as as described above.
  4. Then I stop the the Wireshark.

The filter line in the Wireshark is blank, there's no filter there. I can see a lot of packets coming in Wireshark. But I can't find any packet that's using the port number 6789 despite my app is using that. And I can't find any packet with name Java.

In short I can't find the packet sent on my Java application. Why is it that? How can I find the packet?

Another thing is, despite I deleted all browser caches, not all websites I visit get visible on WireShark. I visit a website but that's invisible on WireShark. I click a link in that site that directs to Wikipedia, but that's invisible too.

But then I visit stackoverflow.com it's visible on WireShark, I get the response "301 moved permanently".


Solution

  • On Linux, you need to capture packets on the loopback interface. On Windows, you have to install Npcap and select the Npcap loopback interface. AFAIK Npcap comes with the Wireshark installer.

    Not sure if you need to be root (on Linux) or Administrator (on Windows) to capture traffic.