You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Socket通信嗅探问题:PC端Wireshark无法捕获树莓派间流量

Troubleshooting Wireshark Capture for Raspberry Pi Socket Communication

Hey there! As a student who’s spent way too much time debugging network traffic headaches, I totally get how frustrating it is when Wireshark doesn’t show the packets you expect. Let’s walk through the most common reasons you’re missing those s.send()/conn.sendall() packets, and how to fix them step by step.

1. Check Your Network Topology & Capture Interface

First off, Wireshark can only capture traffic that passes through your PC’s network interface. If your two Raspberry Pis are communicating directly (e.g., via a crossover Ethernet cable) without your PC in the middle, your PC can’t see that traffic at all. Here’s how to fix this:

  • Connect both Raspberry Pis and your PC to the same local network (like a home router’s LAN ports, or a Wi-Fi hotspot hosted by your PC).
  • In Wireshark, make sure you select the correct interface to capture on:
    • If your PC is wired to the router, pick the Ethernet interface.
    • If using a Wi-Fi hotspot, select the Wi-Fi interface associated with the hotspot.

2. Verify You’re Not Using Localhost (Loopback)

If your Python code uses 127.0.0.1 or localhost as the target IP, that traffic never leaves the Raspberry Pi’s loopback interface—it stays entirely local to the device. Wireshark on your PC will never see it.

  • Find the actual local IP of each Raspberry Pi by running ip a (or ifconfig on older systems) in their terminal. Look for the inet address under your active network interface (e.g., eth0 or wlan0).
  • Update your Socket code to use these real local IPs. For example:
    • Server: Bind to 0.0.0.0 (to listen on all interfaces) or the Pi’s specific local IP.
    • Client: Connect to the server Pi’s local IP (not 127.0.0.1).

Here’s a quick corrected code snippet example:

Server Pi Code

import socket

HOST = '0.0.0.0'  # Listen on all available interfaces
PORT = 65432

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print(f"Connected by {addr}")
        # Send test data
        conn.sendall(b"Test message from Server Pi")

Client Pi Code

import socket

# Replace with your Server Pi's actual local IP (e.g., 192.168.1.105)
HOST = '192.168.1.105'
PORT = 65432

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    # Send test data
    s.send(b"Test message from Client Pi")
    data = s.recv(1024)
    print(f"Received: {data!r}")

3. Adjust Wireshark Capture Filters

Sometimes you’re capturing traffic, but your filter is too strict and hides the packets. Try these tips:

  • Start with no filter at all to see all traffic on the interface.
  • Use a targeted filter to narrow down to just the two Pis: host <Pi1_IP> and host <Pi2_IP>. Replace <Pi1_IP> and <Pi2_IP> with your Raspberry Pis’ local IPs.
  • If using TCP, you can also filter by your port: tcp port 65432 (match the port in your code).

4. Check for TCP Nagle Algorithm Delays

TCP’s Nagle algorithm can delay sending small packets to optimize bandwidth. This might make it seem like your send() call isn’t generating traffic, when it’s just waiting to bundle packets. To disable this for testing:
Add this line right after creating your Socket in both client and server code:

s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

This forces TCP to send packets immediately, making them easier to catch in Wireshark.

5. Rule Out Firewall Blockages

While unlikely for a test setup, firewalls can block traffic or prevent Wireshark from capturing it:

  • On your Raspberry Pis: Run sudo ufw status to check if the firewall is active. If it is, either disable it temporarily (sudo ufw disable) or allow your test port (sudo ufw allow 65432).
  • On your PC: Temporarily disable your firewall (Windows Firewall, macOS Firewall) to rule out it interfering with capture.

Final Checklist

  1. All three devices (PC + 2 Pis) are on the same local network.
  2. Socket code uses local IPs, not localhost.
  3. Wireshark is capturing on the correct network interface.
  4. Capture filters are set to show traffic between the two Pis.
  5. TCP_NODELAY is enabled if using TCP.

Give these steps a try—chances are one of them will get you seeing those packets in Wireshark!

内容的提问来源于stack exchange,提问作者Rawan AlSleebi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:53:11