当前位置:网站首页>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);
}
边栏推荐
- Basic elements of information collection
- STM32学习—DHT11温湿度传感器采样驱动并用cjson格式上报
- Explain three networking modes under virtual machine in detail
- Information collection research report
- 【基础2】——容器
- 防火墙知识,原理,设备,厂商调研总结报告
- Input a string of characters from the keyboard, output different characters and the number of times each character appears. (the output is not in order) use the common methods of string class to solve
- [untitled]
- Saisissez une chaîne de caractères à partir du clavier et affichez différents caractères et le nombre d'occurrences de chaque caractère. (la sortie n'est pas séquentielle) résoudre le problème en util
- A rough understanding of firewall
猜你喜欢

pytorch中的pad_sequence、pack_padded_sequence和pad_packed_sequence函数

Chapter7 循环神经网络-2

shell基本命令

UNIX编程—网络socket

STM32学习—DHT11温湿度传感器采样驱动并用cjson格式上报

2019_IJCAI_Adapting BERT for Target-Oriented Multimodal Sentiment Classification

從鍵盤輸入一串字符,輸出不同的字符以及每個字符出現的次數。(輸出不按照順序)運用String類的常用方法解題

2019_AAAI_Multi-Interactive Memory Network for Aspect Based Multimodal Sentiment Analysis

利用“HiFolw”快捷制作高校学生返校名单信息生成

Shell basic commands
随机推荐
栈溢出基础练习题——6(字符串漏洞64位下)
STM32学习—DHT11温湿度传感器采样驱动并用cjson格式上报
get请求和post请求的区别
指针学习日记(五)经典抽象数据类型、标准函数库
文件目录权限
Lc: sword finger offer 10- I. Fibonacci series
LC: Sword finger offer 03. repeated numbers in the array
The difference between get request and post request and packet capturing
Confused may
[Research Report on the contents, methods, tools and results of information collection]
机器学习开发应用步骤的理解
Saisissez une chaîne de caractères à partir du clavier et affichez différents caractères et le nombre d'occurrences de chaque caractère. (la sortie n'est pas séquentielle) résoudre le problème en util
STM32 learning - DHT11 temperature and humidity sensor sampling drive and reporting in cjson format
Introduction to programming 3 - seeking the maximum value
Apply collections Sort() implements list sorting
Recent ACM insights and future ideas
ESP IDF vscode configuration from downloading tool chain to creating project, step record
浅谈LoRa,LoRaWAN,NB-IoT三类物联网技术
JMeter regular expression extractor
Lc: sword finger offer 05. replace spaces