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:
Confirm Network Connectivity First
- SSH into your BBB and run
ping 192.168.1.20to 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.
- SSH into your BBB and run
Transfer the Client Code to BBB
- Use
scpfrom your PC to send theclient.cppfile to the BBB. Assuming you’re using the default Debian usernamedebian, 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.
- Use
Fix Critical Gaps in Client Code
- Your
client.cppis missing essential header files and theerrorfunction that’s defined in your server code. Add these at the top ofclient.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); }
- Your
Compile the Client on BBB
- In your SSH session, navigate to the directory where
client.cppis stored, then compile it with:g++ client.cpp -o Client -Wall - The
-Wallflag enables warning messages to catch small issues you might miss otherwise.
- In your SSH session, navigate to the directory where
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.
- Run the compiled client, passing your PC’s IP address (
二、代码中的潜在问题
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 withgetaddrinfo()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()andread(), but you don’t handle cases where these functions return0(which means the remote end closed the connection gracefully).
Server.cpp Issues
- Redundant Port Macro: You defined
#define PORT 20001but never use it — the port is fetched fromargv[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_SIZEis 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 awhile(1)loop to keep the server running and accept new connections.
内容的提问来源于stack exchange,提问作者ClaudiaGR
相关产品推荐
相关产品推荐

