3841 views|2 replies

654

Posts

26

Resources
The OP
 

9. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (TCP Two-Machine Communication) [Copy link]

 

We have previously introduced the communication between sockets on the local computer under Windows and Linux. This is meaningless because it requires two computers to communicate with each other and to send and receive messages continuously.

Next, based on the previous code, we will make a two-machine communication, with Linux (Raspberry Pi) as the server and Windows as the client, connected to the same router.

Coding

socket_TCP_win_client_2.cpp under windows

#include <stdio.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib") //加载 ws2_32.dll
#define BUF_SIZE 100
int main(){
//初始化DLL
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
//创建套接字
SOCKET sock = socket(PF_INET, SOCK_DGRAM, 0);
//服务器地址信息
sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr)); //每个字节都用0填充
servAddr.sin_family = PF_INET;
servAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servAddr.sin_port = htons(1234);
//不断获取用户输入并发送给服务器,然后接受服务器数据
sockaddr fromAddr;
int addrLen = sizeof(fromAddr);
while(1){
char buffer[BUF_SIZE] = {0};
printf("Input a string: ");
gets(buffer);
sendto(sock, buffer, strlen(buffer), 0, (struct sockaddr*)&servAddr, sizeof(servAddr));
int strLen = recvfrom(sock, buffer, BUF_SIZE, 0, &fromAddr, &addrLen);
buffer[strLen] = 0;
printf("Message form server: %s\n", buffer);
}
closesocket(sock);
WSACleanup();
return 0;
}

socket_TCP_linux_client_2.cpp under Linux

#define BUF_SIZE 100
int main(){
//创建套接字
int serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//将套接字和IP、端口绑定
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充
serv_addr.sin_family = AF_INET; //使用IPv4地址
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); //具体的IP地址
serv_addr.sin_port = htons(1234); //端口
bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
//进入监听状态,等待用户发起请求
listen(serv_sock, 20);
//接收客户端请求
struct sockaddr_in clnt_addr;
socklen_t clnt_addr_size = sizeof(clnt_addr);
char buffer[BUF_SIZE] = {0}; //缓冲区
while(1){
memset(&buffer, 0, sizeof(buffer)); //每个字节都用0填充
int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
int strLen = recv(clnt_sock, buffer, BUF_SIZE, 0); //接收客户端发来的数据
//向客户端发送数据
printf("recieve:%s",buffer);
write(clnt_sock, buffer, strLen);
//关闭套接字
close(clnt_sock);
memset(buffer, 0, BUF_SIZE); //重置缓冲区
}
close(serv_sock);
return 0;
}

Key Points

The most important point of communication between two machines is that the client needs to know the IP address of the server, and the server registers the socket with the local address. The following code sets the socket to the local IP address.

servAddr .sin_addr.S_un.S_addr =htonl(INADDR_ANY);

The address setting code in the client is

servAddr .sin_addr.S_un.S_addr = inet_addr("192.168.31.159");

The IP address in the code is the IP address of the Raspberry Pi, which can be viewed by entering the ifconfig command.

How to run

  1. Compile and run the server code in Linux first;
  2. Compile and run the client code in Windows.
  3. Enter the data to be transmitted in the Windows terminal and press Enter. After the Raspberry Pi server receives the data, it will send it to the Windows client.

Operation effect

[attach]575403[/attach ]

As can be seen from the while loop, completing a transmission process is quite cumbersome, requiring frequent connection establishment, disconnection, and multiple handshakes. Is there a more convenient transmission method?

Source code

GitHub:

Linux server

链接已隐藏,如需查看请登录或者注册
http://github.com/wanli-car/Examples/blob/master/C++/Socket/Linux/socket_TCP_linux_server_2.cpp
链接已隐藏,如需查看请登录或者注册

Windows client:

链接已隐藏,如需查看请登录或者注册

Gitee:

Linux server:

链接已隐藏,如需查看请登录或者注册

Windows client:

链接已隐藏,如需查看请登录或者注册

image-20211129010254-1.png (6.05 KB, downloads: 0)

image-20211129010254-1.png
This post is from Innovation Lab

Latest reply

Summary of "Wanli" Raspberry Pi car: lb8820265's "Wanli" Raspberry Pi car open source sharing - DIY/Open Source Hardware Zone - Electronic Engineering World - Forum (eeworld.com.cn) Table of contents: "Wanli" Raspberry Pi car launched 1. “Wanli” Raspberry Pi car - Establishing a project warehouse 2. "Wanli" Raspberry Pi car - Python learning (using Thonny) 3. "Wanli" Raspberry Pi car - Python learning (timing task) 4. "Wanli" Raspberry Pi car - C++ learning (compile and run, use geany) 5. "Ten Thousand Miles" Raspberry Pi Car - WiringPi Learning (Delay and Thread Simulation Timer) 6. "Wanli" Raspberry Pi car - wiringPi learning (PWM and external interrupt simulation timer) 7. "Ten Thousand Miles" Raspberry Pi Car——RPi.GPIO Learning (PWM and External Interrupt Simulation Timer) 8. "Wanli" Raspberry Pi car - socket learning (local communication) 9. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (TCP Two-Machine Communication) 10. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (UDP Two-Machine Communication) 11. "Wanli" Raspberry Pi car - socket learning (sent from Android) 12 "Wanli" Raspberry Pi car - socket learning (Android sending and receiving) 13. "Wanli" Raspberry Pi Car - Accessories Preparation 14 "Wanli" Raspberry Pi car - motor drive learning 15 "Wanli" Raspberry Pi car - photoelectric encoder learning (forward and reverse judgment) 16. "Wanli" Raspberry Pi car - photoelectric encoder learning (obtaining speed) 17 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Compiling and Debugging) 18. "Ten Thousand Miles" Raspberry Pi Car——Makefile Learning 19 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Multiple C File Link Debugging) 20 "Million Miles" Raspberry Pi Car - Motor Control Learning (Control Speed) 21. "Wanli" Raspberry Pi car - motor control learning (4-wheel speed control) 22. "Wanli" Raspberry Pi car - mobile phone remote control motor rotation 23 "Wanli" Raspberry Pi car - connected to Raspberry Pi without screen 24 "Millions" Raspberry Pi Car - Bullseye Benchmark Test of Raspberry Pi 64-bit System 25 "Million Miles" Raspberry Pi Car - Nam Wheel Control 26 "Wanli" Raspberry Pi car - program startup 27 "Ten Thousand Miles" Raspberry Pi Car - Fix and Get the Raspberry Pi IP Address 28 "Wanli" Raspberry Pi car - car assembly 29 "Wanli" Raspberry Pi car - straight-driving deviation problem and new control mode 30. "Wanli" Raspberry Pi car - Phase 1 completed demonstration (introduction from scratch)   Details Published on 2022-3-21 13:41
Personal signatureQQ:252669569
 
 

1w

Posts

204

Resources
From 3
 

Summary of "Wanli" Raspberry Pi car:

lb8820265's "Wanli" Raspberry Pi car open source sharing - DIY/Open Source Hardware Zone - Electronic Engineering World - Forum (eeworld.com.cn)

Table of contents:

"Wanli" Raspberry Pi car launched

1. “Wanli” Raspberry Pi car - Establishing a project warehouse

2. "Wanli" Raspberry Pi car - Python learning (using Thonny)

3. "Wanli" Raspberry Pi car - Python learning (timing task)

4. "Wanli" Raspberry Pi car - C++ learning (compile and run, use geany)

5. "Ten Thousand Miles" Raspberry Pi Car - WiringPi Learning (Delay and Thread Simulation Timer)

6. "Wanli" Raspberry Pi car - wiringPi learning (PWM and external interrupt simulation timer)

7. "Ten Thousand Miles" Raspberry Pi Car——RPi.GPIO Learning (PWM and External Interrupt Simulation Timer)

8. "Wanli" Raspberry Pi car - socket learning (local communication)

9. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (TCP Two-Machine Communication)

10. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (UDP Two-Machine Communication)

11. "Wanli" Raspberry Pi car - socket learning (sent from Android)

12 "Wanli" Raspberry Pi car - socket learning (Android sending and receiving)

13. "Wanli" Raspberry Pi Car - Accessories Preparation

14 "Wanli" Raspberry Pi car - motor drive learning

15 "Wanli" Raspberry Pi car - photoelectric encoder learning (forward and reverse judgment)

16. "Wanli" Raspberry Pi car - photoelectric encoder learning (obtaining speed)

17 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Compiling and Debugging)

18. "Ten Thousand Miles" Raspberry Pi Car——Makefile Learning

19 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Multiple C File Link Debugging)

20 "Million Miles" Raspberry Pi Car - Motor Control Learning (Control Speed)

21. "Wanli" Raspberry Pi car - motor control learning (4-wheel speed control)
22. "Wanli" Raspberry Pi car - mobile phone remote control motor rotation

23 "Wanli" Raspberry Pi car - connected to Raspberry Pi without screen

24 "Millions" Raspberry Pi Car - Bullseye Benchmark Test of Raspberry Pi 64-bit System

25 "Million Miles" Raspberry Pi Car - Nam Wheel Control

26 "Wanli" Raspberry Pi car - program startup

27 "Ten Thousand Miles" Raspberry Pi Car - Fix and Get the Raspberry Pi IP Address

28 "Wanli" Raspberry Pi car - car assembly

29 "Wanli" Raspberry Pi car - straight-driving deviation problem and new control mode

30. "Wanli" Raspberry Pi car - Phase 1 completed demonstration (introduction from scratch)

This post is from Innovation Lab
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
Personal signature

玩板看这里:

http://en.eeworld.com/bbs/elecplay.html

EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!

 
 
 

5213

Posts

239

Resources
2
 

Summary (16 articles updated): lb8820265's "Wanli" Raspberry Pi car open source sharing

This post is from Innovation Lab
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list