当前位置: 首页 > news >正文

佛山自助建站系统汽车用品网站建设策划书

佛山自助建站系统,汽车用品网站建设策划书,wordpress 会议主题,钱追得回吗UNIX域套接字(UNIX domain socket)为我们提供了一种在进程之间建立通信通道的便捷方法#xff0c;具有许多有用的内置功能。它支持面向流(TCP)和面向数据报(UDP)协议作为TCP/IP互联网套接字。我们还可以在阻塞和非阻塞模式之间进行选择。 首先需要创建套接字并在套接字函…      UNIX域套接字(UNIX domain socket)为我们提供了一种在进程之间建立通信通道的便捷方法具有许多有用的内置功能。它支持面向流(TCP)和面向数据报(UDP)协议作为TCP/IP互联网套接字。我们还可以在阻塞和非阻塞模式之间进行选择。       首先需要创建套接字并在套接字函数中指定AF_UNIX作为域套接字。创建套接字后必须使用绑定函数将套接字绑定到唯一的文件路径。与AF_INET域中的Internet套接字绑定到唯一的IP地址和端口号不同UNIX域套接字绑定到文件路径。文件系统中创建的此文件当程序关闭且不再需要该文件时你必须手动将其删除。       UNIX域套接字与server/client网络套接字通信没有太大不同但它旨在供本地文件系统使用。server/client网络套接字介绍参考https://blog.csdn.net/fengbingchun/article/details/107848160 UNIX域套接字总结       (1).同步       (2).极高的吞吐量存储设备速度限制       (3).双向通信       (4).以线性方式读写       (5).自动内存管理。 注以上内容主要来自网络整理。 测试代码如下 #include unistd.h #include stdlib.h #include sys/socket.h #include sys/un.h #include sys/wait.h #include string.h #include errno.h #include iostreamint main() {// reference: https://biendltb.github.io/tech/inter-process-communication-ipc-in-cpp/const char* server_sock_path /tmp/unix_sock.server;const char* client_sock_path /tmp/unix_sock.client;pid_t pid fork(); // create two processes of client and serverif (pid 0) {fprintf(stderr, fail to fork\n);return -1;}if (pid ! 0) { // server process(parent process)auto server_sock socket(AF_UNIX, SOCK_STREAM, 0); // open the server socket with the SOCK_STREAM typeif (server_sock -1) {fprintf(stderr, SERVER: fail to socket: %s\n, strerror(errno));exit(1);}// bind to an address on file system// similar to other IPC methods, domain socket needs to bind to a file system, so that client know the address of the server to connect tostruct sockaddr_un server_addr;memset(server_addr, 0, sizeof(server_addr));server_addr.sun_family AF_UNIX;strcpy(server_addr.sun_path, server_sock_path);unlink(server_sock_path); // unlink the file before bind, unless it cant bind: error info: Address already in useauto rc bind(server_sock, (struct sockaddr *)server_addr, sizeof(server_addr));if (rc -1) {fprintf(stderr, SERVER: fail to bind: %s\n, strerror(errno));exit(1);}// listen and accept client connection// set the server in the listen mode and maximum pending connected clients in queuerc listen(server_sock, 10);if (rc -1) {fprintf(stderr, SERVER: fail to listen: %s\n, strerror(errno));exit(1);}fprintf(stdout, SERVER: Socket listening...\n);struct sockaddr_un client_addr;auto len sizeof(client_addr);int client_fd accept(server_sock, (struct sockaddr *)client_addr, (socklen_t*)len);if (client_fd -1) {fprintf(stderr, SERVER: fail to accept: %s\n, strerror(errno));exit(1);}fprintf(stdout, SERVER: Connected to client at: %s\n, client_addr.sun_path);fprintf(stdout, SERVER: Wating for message...\n);const int buf_len 256;char buf[buf_len];memset(buf, 0, buf_len);int byte_recv recv(client_fd, buf, buf_len, 0);if (byte_recv -1) {fprintf(stderr, SERVER: fail to recv: %s\n, strerror(errno));exit(1);}elsefprintf(stdout, SERVER: Server received message: %s.\n, buf);fprintf(stdout, SERVER: Respond to the client...\n);memset(buf, 0, buf_len);strcpy(buf, hello from server);rc send(client_fd, buf, buf_len, 0);if (rc -1) {fprintf(stderr, SERVER: fail to send:%s\n, strerror(errno));exit(1);}fprintf(stdout, SERVER: Done!\n);close(server_sock);close(client_fd);remove(server_sock_path); // remove access to a file namedint status;auto pid2 wait(status); // system call suspends execution of the calling thread until one of its children terminatesfprintf(stdout, process ID of the terminated child: %d\n, pid2);if (WIFEXITED(status)) { // returns true if the child terminated normallyfprintf(stdout, child process ended with: exit(%d)\n, WEXITSTATUS(status));}if (WIFSIGNALED(status)) { // returns true if the child process was terminated by a signalfprintf(stderr, child process ended with: kill -%d\n, WTERMSIG(status));}}if (pid 0) { // client process(child process)int client_sock socket(AF_UNIX, SOCK_STREAM, 0);if (client_sock -1) {fprintf(stderr, CLIENT: fail to socket: %s\n, strerror(errno));exit(1);}// bind client to an address on file system// Note: this binding could be skip if we want only send data to server without receivingstruct sockaddr_un client_addr;memset(client_addr, 0, sizeof(client_addr));client_addr.sun_family AF_UNIX;strcpy(client_addr.sun_path, client_sock_path);unlink (client_sock_path);auto rc bind(client_sock, (struct sockaddr *)client_addr, sizeof(client_addr));if (rc -1) {fprintf(stderr, CLIENT: fail to bind: %s\n, strerror(errno));exit(1);}// Set server address and connect to itstruct sockaddr_un server_addr;server_addr.sun_family AF_UNIX;strcpy(server_addr.sun_path, server_sock_path);rc connect(client_sock, (struct sockaddr*)server_addr, sizeof(server_addr));if (rc -1) {fprintf(stderr, CLIENT: fail to connect: %s\n, strerror(errno));exit(1);}fprintf(stdout, CLIENT: Connected to server.\n);// Send message to serverconst int buf_len 256;char buf[buf_len];memset(buf, 0, buf_len);strcpy(buf, hello from client);rc send(client_sock, buf, buf_len, 0);if (rc -1) {fprintf(stderr, CLIENT: fail to send: %s\n, strerror(errno));exit(1);}fprintf(stdout, CLIENT: Sent a message to server.\n);fprintf(stdout, CLIENT: Wait for respond from server...\n);memset(buf, 0, buf_len);rc recv(client_sock, buf, buf_len, 0);if (rc -1) {fprintf(stderr, CLIENT: fail to recv: %s\n, strerror(errno));exit(1);}elsefprintf(stdout, CLIENT: Message received: %s\n, buf);fprintf(stdout, CLIENT: Done!\n);close(client_sock);remove(client_sock_path);exit(0);}fprintf(stdout, test finish \n);return 0; } 编译脚本build.sh如下 #! /bin/bashif [ -d build ]; thenecho build directory already exists, it does not need to be created again elsemkdir -p build ficd build cmake .. makerc$? if [[ ${rc} ! 0 ]];thenecho #### ERROR: please check ####exit ${rc} fiecho build finish CMakeLists.txt内容如下 cmake_minimum_required(VERSION 3.22) project(samples_multi_process)set(CMAKE_BUILD_TYPE Release) # only works under linux set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -Wall -O2 -stdc17)file(GLOB samples ${PROJECT_SOURCE_DIR}/test_*.cpp) #message(STATUS samples: ${samples})foreach(sample ${samples})string(REGEX MATCH [^/]$ name ${sample})string(REPLACE .cpp exec_name ${name})#message(STATUS exec name: ${exec_name})add_executable(${exec_name} ${sample})target_link_libraries(${exec_name} rt) endforeach() 执行结果如下所示 GitHubhttps://github.com/fengbingchun/Linux_Code_Test
http://www.zqtcl.cn/news/549420/

相关文章:

  • 为您打造高端品牌网站pageadmin wordpress
  • 中小型网站建设的基本流程简约网站欣赏
  • 设备上哪个网站做外贸推广网络服务类型及其所采用的网络协议
  • 学习前端开发的网站动漫设计属于什么大类
  • 十堰秦楚网 十堰新闻门户网站报修网站模板
  • 家居小程序源码下载自动seo系统
  • 动态效果的网站建设技术老闵行是指哪里
  • 电商网站开发面临的技术问题做闪图的网站
  • 怎么查看网站开发语言的类型东莞哪些地方是风险区
  • 不用购买域名做网站广州网站建设培训学校
  • 城市轨道建设规范下载网站古网站典模板
  • 关于实验室建设的英文网站深圳企业网站制作公司怎样
  • wordpress全站背景音乐中山网站搜索排名
  • 搭建网站的过程透明主题wordpress
  • 丰台网站建设公司电话深圳微信商城网站设计公司
  • 做淘宝要用的网站吗上海微信网站
  • 佛山高端网站制作公司wordpress 发送邮件插件
  • 类似站酷的设计类网站网站建设需要待摊吗
  • 用php做视频网站在学做网站还不知道买什么好
  • wordpress培训类网站网站建设 好
  • 网站开发需要2个月吗网站建设案例精粹
  • 网站建设项目职责营销型网站建设五大内容
  • 建设工程监理招标网站W做网站
  • 网站建设与维护教学课件网站上线前做环境部署
  • 信誉好的网站建设做网站成为首富的外国人
  • 常州网站制作市场湖北省荆门市城乡建设网站
  • 泉州网站制作运营商专业北京软件公司招聘信息查询
  • 车床加工东莞网站建设网站建设教学改进
  • 深圳专业做网站建设西安网站建设有限公司
  • wordpress 一键建站wordpress子主题style