当前位置:网站首页>Two very simple TCPUDP programs, communication between raspberry pie and PC
Two very simple TCPUDP programs, communication between raspberry pie and PC
2022-07-23 06:07:00 【Blame me for not 1v9】
Catalog
One 、 The experiment purpose
( One ) Master connection oriented (TCP)Socket Communications technology
( Two ) Master connectionless (UDP)Socket Communications technology
( 3、 ... and ) Master the basic programming method of server multi client mode
Two 、 Experimental content
Connection oriented (TCP)Socket signal communication :
Write a server.c, The service program runs on raspberry pie or qemu, Service program Will receive service requests from different clients and respond accordingly ;
To write 3 A client program client1.c,client2.c,client3.c, Respectively to the service The program requests the service and receives the response from the service program .
There is no connection (UDP)Socket signal communication :
Write a server.c, The service program runs on raspberry pie or qemu, The service program will receive service requests from different clients and respond accordingly ;
To write 2 A client program client1.c,client2.c, Respectively request services from the service program and receive the response from the server .
3、 ... and 、 The process and results of the experiment
TCP:
TCP in , Socket is a one-to-one relationship . To 10 Clients provide services , Well, in addition to the socket responsible for listening , You also need to create 10 Socket ( Create subprocesses in the job to achieve , Parent process client Socket direct recycling ). But in UDP in , Both the server side and the client side only need 1 A socket .

Server( Raspberry pie ) End :

Client(PC) End :


UDP:
UDP It's not point to point , There is no request connection and acceptance process , Therefore, there is no need to create sub processes one-to-one correspondence , Therefore, in a sense, it is impossible to clearly distinguish between server and client , Just because it provides services, it is called server side .
Server( Raspberry pie ) End :


Customer (PC) End :


Four 、 Unresolved issues
You want to set a permission instruction, such as ,123, Issuing this specific command can shut down remotely server Terminal tcp service :
But in server In the program ,break After that, the program is still blocked .
The reason is that after the child process is created , Both parent and child processes will execute , Only child processes are processed , The parent process does not handle . At first, the solution was to get the parent process pid, Then end the assignment pid The process of , But in this way, the original child process did not disappear and became an orphan process .

Attached with experiment code :
TCP
server0.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <signal.h>
int main()
{
int data,datalen; //data For buffering
int sockfd_s,sockfd_c,address_len;
struct sockaddr_in address_s,address_c;
// establish socket
sockfd_s = socket(AF_INET, SOCK_STREAM, 0);
address_s.sin_family = AF_INET; // Address family , Set to AF_INET
address_s.sin_addr.s_addr = inet_addr("192.168.137.251"); //IP Address , Set as host address
address_s.sin_port = htons(9999); // port , Use bind Function, you need to set sin_port Convert to high byte first
// Socket and IP、 Port binding
bind(sockfd_s,(struct sockaddr*)&address_s,sizeof(address_s));
// monitor , The maximum number of waits is 20
listen(sockfd_s,20);
// receive client request
address_len = sizeof(address_c);
datalen = sizeof(data);
while(1){
sockfd_c = accept(sockfd_s,(struct sockaddr*)&address_c,&address_len);
printf(" Now proceed to accpet\n");
sleep(1);
pid_t pid = fork();
if(pid == -1){
perror("fail to create fork!\n");
exit(EXIT_FAILURE);
}else if(pid == 0){
recv(sockfd_c,&data,datalen,0);
printf("server:data from client = %d\n",data);
data+=100;
send(sockfd_c,&data,datalen,0);
if(data == 223){
//kill(getppid(),SIGINT);
printf(" Accept to data123, End of communication .\n");
exit(0);
//break;
}
//exit(EXIT_SUCCESS);
}else{
recv(sockfd_c,&data,datalen,0);
close(sockfd_c);
printf("sockfd_c has closed,sockfd_s:%d.\n",sockfd_s);
}
printf("data:%d\n",data);
}
close(sockfd_c);
close(sockfd_s);
return 0;
}
client1.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
int main(){
int sockfd, datalen, data = 100;
struct sockaddr_in address;
sockfd = socket(AF_INET,SOCK_STREAM,0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("192.168.137.251");// Raspberry pie address
address.sin_port = htons(9999);
if(connect(sockfd,(struct sockaddr*)&address,sizeof(address))==-1){
perror("client1:failed to connect!\n");
exit(1);
}
while(1){
datalen = sizeof(data);
send(sockfd, &data, datalen, 0);
recv(sockfd, &data, datalen, 0);
printf("client1:data from server = %d\n",data);
exit(1);
}
close(sockfd);
}
UDP
server0.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <signal.h>
int main()
{
int flag = 0;
int data,datalen; //data For buffering
int sockfd_s,sockfd_c,address_len;
struct sockaddr_in address_s,address_c;
// establish socket
sockfd_s = socket(AF_INET, SOCK_DGRAM, 0);
address_s.sin_family = AF_INET; // Address family , Set to AF_INET
address_s.sin_addr.s_addr = inet_addr("192.168.137.251"); //IP Address , Set as host address
address_s.sin_port = htons(9999); // port , Use bind Function, you need to set sin_port Convert to high byte first
// Socket and IP、 Port binding
bind(sockfd_s,(struct sockaddr*)&address_s,sizeof(address_s));
// receive client request
address_len = sizeof(address_c);
datalen = sizeof(data);
while(1){
int adlen = sizeof(address_c);
recvfrom(sockfd_s,&data,datalen,0,(struct sockaddr*)&address_c, &adlen);
printf("server:data from client = %d\n",data);
data+=100;
sendto(sockfd_s,&data,datalen,0,(struct sockaddr*)&address_c,sizeof(address_c));
}
close(sockfd_c);
close(sockfd_s);
return 0;
}
client0.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
int main(){
int sockfd, datalen, data = 0;
struct sockaddr_in address,address_s;
sockfd = socket(AF_INET,SOCK_DGRAM,0);
//address is the client address.
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(9999);
//address_s is the server address.
address_s.sin_family = AF_INET;
address_s.sin_addr.s_addr = inet_addr("192.168.137.251");
address_s.sin_port = htons(9999);
datalen = sizeof(data);
int adlen = sizeof(address_s);
sendto(sockfd, &data, datalen, 0, (struct sockaddr*)&address_s, sizeof(address_s));
recvfrom(sockfd, &data, datalen, 0, (struct sockaddr*)&address_s, &adlen);
printf("client0:data from server = %d\n",data);
exit(1);
close(sockfd);
}
边栏推荐
- Configure the private chirpstack of lorawan in the LAN
- Firewall knowledge, principle, equipment, manufacturer research summary report
- 【基础7】——异常,捕获、自定义异常
- 记一个无显示屏无线连接树莓派,查找不到ip的办法
- 栈溢出基础练习题——6(字符串漏洞64位下)
- 【基础4】——文件读写、模块
- 资产测绘流程
- STM32 learning - DHT11 temperature and humidity sensor sampling drive and reporting in cjson format
- Recent ACM insights and future ideas
- 逻辑卷管理
猜你喜欢

第四次作业:关于cat,grep,cut,sort,uniq,vim,tr等命令的用法

Implementing IO multiplexing in UNIX using epoll function to realize network socket server

STM32 learning - DHT11 temperature and humidity sensor sampling drive and reporting in cjson format

Tencent cloud is connected to lorawan and debugged
![[Research Report on the contents, methods, tools and results of information collection]](/img/e2/37606fbd488e55a82c7e6174b892de.jpg)
[Research Report on the contents, methods, tools and results of information collection]

推荐系统基础架构以及项目介绍
![[untitled]](/img/cc/890e190893d099a3efbcccc10a6a3d.png)
[untitled]

The difference between get request and post request and packet capturing

堆基础练习题 —— 1

2019_AAAI_Multi-Interactive Memory Network for Aspect Based Multimodal Sentiment Analysis
随机推荐
【基础4】——文件读写、模块
【基础3】——结构与函数
Understanding the application steps of machine learning development
JMeter regular expression extractor
栈溢出基础练习题——3 (内有32和64位区别的对比)
编码器-解码器(seq2seq)
pwn栈溢出基础练习题——2
UNIX实现IO多路复用之使用poll函数实现网络socket服务端
[jmeter] solution to Chinese garbled response content
Implementing IO multiplexing in UNIX using poll function to realize network socket server
Contains method to check whether the sequence contains an element
Solution of cross domain problems
On Lora, lorawan and Nb IOT Internet of things technologies
跨域问题的解决
信息收集的基础要素
Common problems of multiple processes - how to lock the same parent thread variable (critical resource) when creating multiple threads so that the shared parent thread variable is not repeatedly modif
LC:剑指 Offer 03. 数组中重复的数字
Using "hifolw" to quickly create the information generation of College Students' return list
Mobile application classification
Regular expression II