gpt4 book ai didi

java - 已在 netbeans 中发送的数据包不会出现在 wireshark 或 cmd->netstat 中

转载 作者:可可西里 更新时间:2023-11-01 02:41:05 25 4
gpt4 key购买 nike

我一直在尝试从一个简单的客户端向一个简单的服务器发送一个 tcp/ip 数据包...

当我在 netbeans 中运行它们时,我从服务器得到响应,但我在 wireshark 中找不到数据包,如果我在 cmd 中使用 netstat 命令,数据包似乎也不存在......我尝试了几个不同的端口(8080、80、5000 等),我使用的是本地主机作为 IP 地址。

netbeans 或 wireshark 中是否有我遗漏的某些设置,我仍需要配置?

客户端:

public class TCPClient {

private static InetAddress host;
private static final int PORT=1235;

public static void main(String[] args)
{
try
{
host=InetAddress.getByName("localhost");
}
catch(UnknownHostException e)
{
System.out.println("Host id not found");
System.exit(1);

}
run();

}

private static void run()
{
Socket link=null;
try
{
link = new Socket(host,PORT);

BufferedReader in = new BufferedReader
(new InputStreamReader(link.getInputStream()));

PrintWriter out = new PrintWriter(link.getOutputStream(),true);

BufferedReader userEntry = new BufferedReader
(new InputStreamReader(System.in));

String message,response;

do
{
System.out.println("Enter message: ");
message=userEntry.readLine();
out.println(message);
response=in.readLine();
System.out.println("\nSERVER> " + response);
}
while(!message.equals("***CLOSE***"));

}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
System.out.println("\n* Closing connection... *");

link.close();
}
catch(IOException e)
{
System.out.println("unable to disconnect!");
System.exit(1);
}
}
}

服务器:

public class TCPServer {

private static ServerSocket serverSock;
private static final int PORT = 1235;


public static void main(String[] args)
{
System.out.println("opening port...\n");
try
{
serverSock=new ServerSocket(PORT);
}
catch(IOException e)
{
System.out.println("unable to attach port");
System.exit(1);
}
do
{
run();
}
while(true);

}

private static void run()
{
Socket link=null;
try
{
link = serverSock.accept();

BufferedReader in = new BufferedReader
(new InputStreamReader(link.getInputStream()));

PrintWriter out = new PrintWriter(link.getOutputStream(),true);

int numMessages=0;
String message = in.readLine();

while(!message.equals("***CLOSE***"))
{
System.out.println("Message received.");
numMessages++;
out.println("Message " + numMessages + ": " + message);
message=in.readLine();
}
out.println(numMessages + " messages received");

}
catch(IOException e)
{
System.out.println("unable to disconnect!");
System.exit(1);
}
}
}

最佳答案

尝试使用 RawCap捕获数据包。然后,您可以使用 Wireshark 对数据包进行后期分析,或者使用类似于以下的方法将输出通过管道传输到 Wireshark ...假设您有 cygwintail可用::

cmd1:RawCap.exe -f 127.0.0.1 localhost_capture.pcap

cmd2:tail -c +0 -f localhost_capture.pcap | Wireshark.exe -k -i -

-f 开关告诉 RawCap flush每个数据包直接到磁盘而不是缓冲它。这在使用上面的 cmd2 中的 tail + Wireshark 进行实时分析时很有意义;但是它确实会带来性能损失,因此请谨慎使用。

关于java - 已在 netbeans 中发送的数据包不会出现在 wireshark 或 cmd->netstat 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20777965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com