-1

My task is to implement an application that will accept all the information that comes to a certain port. It should work like Wireshark.

This code should listen to everything that comes to port 46122. In Wireshark, I see that some information comes to this port. But my program does not see anything.

   ServerSocket serverSocket = new ServerSocket(46122);
   Socket clientSocket = serverSocket.accept();
   PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(
                                new InputStreamReader(clientSocket.getInputStream()));
Makoto
  • 104,088
  • 27
  • 192
  • 230
Kiryl A.
  • 163
  • 2
  • 15
  • 1
    This will only record the data sent over a TCP connection. It won't record the TCP/IP headers, or anything sent with a different protocol. Is that what you intend? – erickson Aug 23 '17 at 17:42
  • @Am_I_Helpful That's the problem. I will not see any packages until no one client is connected to this server, am I understood right? But how does Wireshark do it? – Kiryl A. Aug 23 '17 at 17:48
  • @erickson I see a lot of data that received on this port in Wireshark. But my program does'nt. How to see the data? Appreciate very much – Kiryl A. Aug 23 '17 at 17:57
  • See https://stackoverflow.com/q/3623681/3474 – erickson Aug 23 '17 at 17:58
  • Your program doesn't *do* anything except listen and accept one socket. If you want to see data you have to *read* it. Your assignment should be returned as infeasible in Java and also beyond your present skills. – user207421 Aug 23 '17 at 18:08
  • @EJP Many thanks, but it's up to me. It would be more useful to show me any ideas for solving this problem using jNetPcap – Kiryl A. Aug 23 '17 at 18:10

1 Answers1

1

If you want to record entire TCP packets with Java, you'll need a third-party library that has a "native" component. The core Java libraries cannot do this. Library recommendations are off-topic, "pcap" and "java" might be helpful search keywords.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • Upvoted, +1. Stll that'd be a poor choice to design such application using java. I'd not suggest anyone to use Java for developing such a tool, unless it is the last option. – Am_I_Helpful Aug 23 '17 at 18:00
  • May be you can recommend any library? Without links, only name – Kiryl A. Aug 23 '17 at 18:00
  • And explain please what means "native" component? – Kiryl A. Aug 23 '17 at 18:01
  • @Am_I_Helpful Yes, you are right, but this is my task. I have no choice :( – Kiryl A. Aug 23 '17 at 18:02
  • @Am_I_Helpful Why? – erickson Aug 23 '17 at 18:13
  • @erickson - Because, the libraries you mentioned are also incomplete to achieve the state as perfect as that of Wireshark. I'd bet still on C for developing such tools which deal with layer-2 and layer-3 Internet stack. But, I agree that the simpler model can be built using Java. – Am_I_Helpful Aug 23 '17 at 18:14
  • @Cyryl I commented on your post with a link to another question where some libraries are mentioned. (I also voted to close it.) That's a good place to start. A native component is a library that is written in C or C++, and linked to Java code. This can make your Java code less portable, since you'll need to have that C or C++ compiled for each platform you want to run on. – erickson Aug 23 '17 at 18:16