当前位置:网站首页>Send blocking, receive blocking
Send blocking, receive blocking
2022-07-19 11:38:00 【So_ Band】
Blocking
That is, data cannot be added or deleted , Always drive here , Until the data can be added or deleted ;
If it is Send blocking 【send】, That is, the buffer in the operating system Full of , Unable to continue add to To the buffer in the operating system , Will block in the present , Until the buffer in the operating system is free ;
If it is Receive blocking 【recv】, That is, the buffer in the operating system empty , Unable to continue obtain Data to the buffer in the operating system , Will block in the present , Until the buffer in the operating system has buffered data ;
Example
Server side
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream>
#include <string.h>
int main(int argc, char* argv[])
{
//1. Create a listener socket
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1)
{
std::cout << "create listen socket error." << std::endl;
return -1;
}
//2. Initialize server address
struct sockaddr_in bindaddr;
bindaddr.sin_family = AF_INET;
bindaddr.sin_addr.s_addr = htonl(INADDR_ANY);
bindaddr.sin_port = htons(3000);
if (bind(listenfd, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) == -1)
{
std::cout << "bind listen socket error." << std::endl;
close(listenfd);
return -1;
}
//3. Start listening
if (listen(listenfd, SOMAXCONN) == -1)
{
std::cout << "listen error." << std::endl;
close(listenfd);
return -1;
}
while (true)
{
struct sockaddr_in clientaddr;
socklen_t clientaddrlen = sizeof(clientaddr);
//4. Accept client connections
int clientfd = accept(listenfd, (struct sockaddr *)&clientaddr, &clientaddrlen);
if (clientfd != -1)
{
// Accept connections only , Do not call recv Receive any data
std:: cout << "accept a client connection." << std::endl;
}
}
//7. Turn off listening socket
close(listenfd);
return 0;
}
client
/**
* Verify that in blocking mode send Behavior of functions ,client End
* zhangyl 2018.12.17
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream>
#include <string.h>
#define SERVER_ADDRESS "127.0.0.1"
#define SERVER_PORT 3000
#define SEND_DATA "helloworld"
int main(int argc, char* argv[])
{
//1. Create a socket
int clientfd = socket(AF_INET, SOCK_STREAM, 0);
if (clientfd == -1)
{
std::cout << "create client socket error." << std::endl;
return -1;
}
//2. Connect to server
struct sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);
serveraddr.sin_port = htons(SERVER_PORT);
if (connect(clientfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) == -1)
{
std::cout << "connect socket error." << std::endl;
close(clientfd);
return -1;
}
//3. Constantly send data to the server , Or exit with an error
int count = 0;
while (true)
{
int ret = send(clientfd, SEND_DATA, strlen(SEND_DATA), 0);
if (ret != strlen(SEND_DATA))
{
std::cout << "send data error." << std::endl;
break;
}
else
{
count ++;
std::cout << "send data successfully, count = " << count << std::endl;
}
}
//5. close socket
close(clientfd);
return 0;
}
Finally, when the data in the operating system buffer of the client is full , It's blocking ;
Blocked when full , Until the data is recv
Reference resources :
https://codeantenna.com/a/SdTXxsiy6g
边栏推荐
猜你喜欢

STC8H开发(十四): I2C驱动RX8025T高精度实时时钟芯片

Ppde Q2 welcome | welcome 22 AI developers to join the propeller developer technical expert program!

mysql show processlist 详解

Performance optimization @contented to reduce pseudo sharing

Mysql 自增id、uuid与雪花id

Unity高版本退回低版本报错问题

Today's sleep quality record 79 points

Download of common getshell tools

委派双亲之类加载器

Powercli script performance optimization
随机推荐
Total number of blocking and waiting in jconsole thread panel (RPM)
LeetCode 745. Prefix and suffix search
SQL union operator
Solve the problem that QQ mail and Thunderbird cannot log in to outlook
8.固定收益投资
TCP拥塞控制详解 | 7. 超越TCP
A fastandrobust convolutionalneuralnetwork-based defect detection model inproductqualitycontrol-閱讀筆記
Developing those things: how to solve the problem of long-time encoding and decoding of RK chip video processing?
动态内存分配问题
Transport layer -------- TCP (I)
synchronized锁升级
Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning
Docker install MySQL
TCP congestion control details | 7 Surpass TCP
565. 数组嵌套 : 常规模拟题
设置cmd命令提示符窗口的界面语言为英文
From prediction to decision-making, Chapter 9 Yunji datacanvas launched the ylearn causal learning open source project
The type of MySQL index (single column index, combined index, BTREE index, clustered index, etc.)
Cmake common commands (V)
466-82(3、146、215)