使用tech.gusavila92:java-android-websocket-client发送Socket数据异常求助
Hey there, let's work through your issue step by step. From what you've described, the core problem seems to be a mismatch between how you're sending data (via WebSocket) and what your target device is expecting to receive. Here's a breakdown of what's going wrong and how to fix it:
1. Why the device sees unrecognizable content
You're using a WebSocket client to connect to a port, but there's a good chance your device is listening for raw TCP Socket traffic—not a WebSocket service. WebSocket requires an initial HTTP handshake (that's the request headers you're seeing in your packet captures) before any actual data is sent in WebSocket-specific frames. If your device isn't set up to handle this handshake, it'll only see those header bytes and have no idea how to parse them.
Even if the device does support WebSocket, the data you send is wrapped in WebSocket protocol frames (not raw bytes), which the device might not be expecting.
2. Quick Fixes to Test First
a. Verify the device's listening protocol
First, confirm: Is the device running a WebSocket server or a plain TCP server?
- If it's plain TCP: Ditch the WebSocket client entirely—use Java's built-in
Socketclass to send raw bytes directly. - If it's WebSocket: Fix your URL prefix and check how you're sending data.
b. Fix your WebSocket URL
Your current URL uses https://—for WebSocket, you need to use:
ws://for unencrypted WebSocket connectionswss://for encrypted (TLS) WebSocket connections
Update your URL definition:
public static final String WEB_SOCKET_URL = "wss://" + HOST_ADDRESS + ":" + PORT_NUMBER;
c. Check how you're sending data
- If you're sending binary data (like sensor readings, raw commands), use the
send(byte[])method instead ofsend(String)to avoid string encoding issues:// Assuming Data is a byte array of your raw payload webSocketClient.send(Data); - If sending text, ensure both client and device use the same character encoding (e.g., UTF-8):
webSocketClient.send(new String(Data, StandardCharsets.UTF_8));
3. Example Code for Plain TCP (if device uses raw sockets)
If your device is listening for plain TCP traffic, replace your WebSocket code with this raw TCP implementation:
private static final int PORT_NUMBER = 3030; private static final String HOST_ADDRESS = "192.168.1.102"; @Override protected Void doInBackground(Void... voids) { Socket socket = null; OutputStream outputStream = null; try { // Establish raw TCP connection socket = new Socket(HOST_ADDRESS, PORT_NUMBER); socket.setSoTimeout(60000); // Match your read timeout // Send raw data outputStream = socket.getOutputStream(); outputStream.write(Data); // Data is your raw byte array outputStream.flush(); // Optional: Receive response from device // InputStream inputStream = socket.getInputStream(); // byte[] responseBuffer = new byte[1024]; // int bytesRead = inputStream.read(responseBuffer); // ... process response } catch (IOException e) { Log.e(TAG, "TCP Connection Error: " + e.getMessage()); e.printStackTrace(); } finally { // Clean up resources try { if (outputStream != null) outputStream.close(); if (socket != null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
4. Debugging Tips to Confirm the Issue
- Use Wireshark for proper packet inspection: Regular HTTP/HTTPS tools won't parse WebSocket frames. Filter for
host 192.168.1.102 and port 3030in Wireshark, and look for frames labeled "WebSocket Text" or "WebSocket Binary" to see the actual data being sent. - Check the device's logs: If possible, get logs from the device to see exactly what bytes it's receiving. This will tell you if it's getting the WebSocket handshake headers (proof you're using the wrong protocol) or wrapped WebSocket frames.
内容的提问来源于stack exchange,提问作者Mir.Ebrahim Jafarpour Zonuzi

