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

BeagleBone Black与PC的TCP/IP通信实现及运行咨询

BeagleBone Black 客户端运行指南与代码潜在问题分析

一、如何正确运行BeagleBone端的客户端程序

Let’s walk through getting your client up and running on the BeagleBone Black (BBB) step by step:

  1. Confirm Network Connectivity First

    • SSH into your BBB and run ping 192.168.1.20 to make sure it can reach your PC server. If the ping fails, double-check your Ethernet cable and verify both devices are on the same subnet with the correct static IPs.
  2. Transfer the Client Code to BBB

    • Use scp from your PC to send the client.cpp file to the BBB. Assuming you’re using the default Debian username debian, run this command on your PC:
      scp client.cpp debian@192.168.1.15:/home/debian/
      
    • Alternatively, you can create/edit the file directly on the BBB via SSH using nano client.cpp.
  3. Fix Critical Gaps in Client Code

    • Your client.cpp is missing essential header files and the error function that’s defined in your server code. Add these at the top of client.cpp:
      #include <iostream>
      #include <netinet/in.h>
      #include <arpa/inet.h>
      #include <errno.h>
      #include <string.h>
      #include <unistd.h>
      #include <stdio.h>
      #include <stdlib.h>
      #include <sys/socket.h>
      #include <sys/types.h>
      #include <netdb.h> // Required for gethostbyname()
      
      using namespace std;
      
      void error(const char *msg) {
          perror(msg);
          exit(1);
      }
      
  4. Compile the Client on BBB

    • In your SSH session, navigate to the directory where client.cpp is stored, then compile it with:
      g++ client.cpp -o Client -Wall
      
    • The -Wall flag enables warning messages to catch small issues you might miss otherwise.
  5. Launch the Client

    • Run the compiled client, passing your PC’s IP address (192.168.1.20) and the server port (20001) as arguments:
      ./Client 192.168.1.20 20001
      
    • You’ll see the prompt Please enter the message: — type your text and hit enter, and it should be sent to your PC server immediately.

二、代码中的潜在问题

Let’s break down areas in both server and client code that could cause issues or be improved:

Client.cpp Issues

  • Outdated Host Lookup: gethostbyname() is deprecated, doesn’t support IPv6, and isn’t thread-safe. Replace it with getaddrinfo() for modern, reliable host resolution.
  • Unwanted Newline in Messages: fgets() includes the trailing newline character from your input. If you don’t want this sent to the server, add a line to strip it:
    buffer[strcspn(buffer, "\n")] = '\0';
    
  • Incomplete Error Handling: You check for negative return values from write() and read(), but you don’t handle cases where these functions return 0 (which means the remote end closed the connection gracefully).

Server.cpp Issues

  • Redundant Port Macro: You defined #define PORT 20001 but never use it — the port is fetched from argv[1] instead. Remove the unused macro to clean up the code.
  • Single Connection Limit: Your server only handles one client connection and then exits. To support multiple concurrent clients, use multithreading or fork() to spawn a new process for each incoming connection.
  • Incorrect Send Length: The string "Hello, world!\n" is 14 characters long (including the newline), but you’re sending only 13 bytes. This means the final newline won’t be transmitted. Update the send call to:
    send(newsockfd, "Hello, world!\n", 14, 0);
    
  • Unused Buffer Size Macro: SERVER_BUFFER_SIZE is defined but not used (you’re using a 256-byte buffer instead). Align these to avoid confusion.
  • No Persistent Connection Loop: After handling one client, the server closes both sockets and exits. Wrap the accept() and subsequent logic in a while(1) loop to keep the server running and accept new connections.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:03:33