Unix Network Programming Episode 96

‘socketpair’ Function

The socketpair function creates two sockets that are then connected together. This function applies only to Unix domain sockets.

#include <sys/socket.h>
int socketpair(int family, int type, int protocol, int sockfd[2]);

POSIX does not require full-duplex pipes. On SVR4, pipe returns two full-duplex descriptors, while Berkeley-derived kernels traditionally return two half-duplex descriptors (Figure 17.31 of TCPv3).

Socket Functions

There are several differences and restrictions in the socket functions when using Unix domain sockets. We list the POSIX requirements when applicable, and note that not all implementations are currently at this level.

1.The default file access permissions for a pathname created by bind should be 0777 (read, write, and execute by user, group, and other), modified by the current umask value.
2.The pathname associated with a Unix domain socket should be an absolute pathname, not a relative pathname. The reason to avoid the latter is that its resolution depends on the current working directory of the caller. That is, if the server binds a relative pathname, then the client must be in the same directory as the server (or must know this directory) for the client’s call to either connect or sendto to succeed.
POSIX says that binding a relative pathname to a Unix domain socket gives unpredictable results.
3.The pathname specified in a call to connect must be a pathname that is currently bound to an open Unix domain socket of the same type (stream or datagram). Errors occur if: (i) the pathname exists but is not a socket; (ii) the pathname exists and is a socket, but no open socket descriptor is associated with the pathname; or (iii) the pathname exists and is an open socket, but is of the wrong type (that is, a Unix domain stream socket cannot connect to a pathname associated with a Unix domain datagram socket, and vice versa).
4.The permission testing associated with the connect of a Unix domain socket is the same as if open had been called for write-only access to the pathname.
5.Unix domain stream sockets are similar to TCP sockets: They provide a byte stream interface to the process with no record boundaries.
6.If a call to connect for a Unix domain stream socket finds that the listening socket’s queue is full (Section 4.5(See 8.2.5)), ECONNREFUSED is returned immediately. This differs from TCP: The TCP listener ignores an arriving SYN if the socket’s queue is full, and the TCP connector retries by sending the SYN several times.
7.Unix domain datagram sockets are similar to UDP sockets: They provide an unreliable datagram service that preserves record boundaries.
8.Unlike UDP sockets, sending a datagram on an unbound Unix domain datagram socket does not bind a pathname to the socket. (Recall that sending a UDP datagram on an unbound UDP socket causes an ephemeral port to be bound to the socket.) This means the receiver of the datagram will be unable to send a reply unless the sender has bound a pathname to its socket. Similarly, unlike TCP and UDP, calling connect for a Unix domain datagram socket does not bind a pathname to the socket.

Unix Domain Stream Client/Server

We now recode our TCP echo client/server from Chapter 5(See 8.3) to use Unix domain sockets. Figure 15.3 shows the server, which is a modification of Figure 5.12(See 8.3.10) to use the Unix domain stream protocol instead of TCP.

#include "unp.h"int main(int argc, char **argv)
{int listenfd, connfd;pid_t childpid;socklen_t clientlen;struct sockaddr_un clientaddress, serveraddress;void sig_child(int);listenfd=Socket(AF_LOCAL,SOCK_STREAM,0);unlink(UNIXSTR_PATH);bzero(&serveraddress,sizeof(serveraddress));serveraddress.sun_family=AF_LOCAL;strcpy(serveraddress.sun_path,UNIXSTR_PATH);Bind(listenfd, (SA *)&serveraddress, sizeof(serveraddress));Listen(listenfd, LISTENQ);Signal(SIGCHLD, sig_child);for(;;){clientlen=sizeof(clientaddress);if((connfd=accept(listenfd, (SA *)&clientaddress, &clientlen))<0){if(errno==EINTR)continue;elseerr_sys("accept error");}if((childpid=Fork())==0){Close(listenfd);str_echo(connfd);return 0;}Close(connfd);}return 0;
}

Unix domain stream protocol echo server

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/37845.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/37845.shtml
英文地址,請注明出處:http://en.pswp.cn/web/37845.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

(十七)如何學習統計學基礎知識(學習路線)

統計學是數據科學的基本支柱。統計學的目的是幫助你理解數據并從中得出有意義的結論。在數據科學中&#xff0c;統計學在理解數據模式和趨勢、做出預測和檢驗假設方面起著至關重要的作用。 (一) 數據科學統計學習路線圖 本文為學習統計學并將其應用于數據科學提供了清晰、結構化…

如何使用 SPM 插件從 Pkl 配置文件生成 Swift 接口

文章目錄 前言示例展示 Pkl 配置生成 Swift 綁定手動安裝和使用 pkl-gen-swift創建 SPM 命令插件加載 Pkl 配置總結前言 Pkl(全稱為 Pickle)是蘋果推出的一種全新的專用于配置的編程語言。它允許開發人員通過類型和內置驗證安全、直觀地設計數據模型。 作為蘋果語言,Pkl 有…

Python容器 之 列表--下標和切片

列表的切片 得到是 新的列表字符串的切片 得到是 新的字符串 如果下標 不存在會報錯 list1 [1, 3.14, "hello", False] print(list1)# 獲取 列表中 第一個數據 print(list1[0]) # 1# 獲取列表中的最后一個數據 print(list1[-1]) # [False]# 獲取中間兩個數 即 3.1…

3.2ui功能講解之graph頁面

本節重點介紹 : graph頁面target頁面flags頁面status頁面tsdb-status頁面 訪問地址 $ip:9090 graph頁面 autocomplete 可以補全metrics tag信息或者 內置的關鍵字 &#xff0c;如sum聚合函數table查詢 instante查詢&#xff0c; 一個點的查詢graph查詢調整分辨率 resolutio…

記錄:有趣的C#多元運算符 ? : 表達式寫法

有時候用 if //...Whatre you she wanna go else if //...do do do else //...and i know something just like this... 感覺代碼太多了怎么優雅的、高端的替換&#xff1f; 看個高端的栗子菊&#xff1a; LedCOM["parity"] ledData[4] "N" ? …

Study--Oracle-05-Oracler體系結構

一、oracle 體系概覽 Oracle數據庫的體系結構通常包括以下主要組件&#xff1a; 1、實例&#xff08;Instance&#xff09;&#xff1a;運行數據庫的軟件環境&#xff0c;包括內存結構&#xff08;SGA&#xff09;和進程結構&#xff08;Background Processes and User Proces…

Django 一對多關系

1&#xff0c;創建 Django 應用 Test/app9 django-admin startapp app9 2&#xff0c;注冊應用 Test/Test/settings.py 3&#xff0c;添加應用路由 Test/Test/urls.py from django.contrib import admin from django.urls import path, includeurlpatterns [path(admin/,…

《每天5分鐘用Flask搭建一個管理系統》 第10章:前端集成

第10章&#xff1a;前端集成 10.1 前端技術概述 前端技術指的是構建Web應用用戶界面所使用的技術&#xff0c;包括HTML、CSS和JavaScript。現代Web開發中&#xff0c;前端框架如React、Vue.js和Angular等被廣泛使用。 10.2 AJAX與Flask的集成 AJAX&#xff08;Asynchronous…

數據資產安全策略的定制化之道:深入了解各企業獨特需求,量身打造個性化的數據資產保護方案,確保數據安全無虞,助力企業穩健發展

目錄 一、引言 二、企業數據資產安全現狀分析 &#xff08;一&#xff09;數據安全風險多樣化 &#xff08;二&#xff09;傳統安全措施難以滿足需求 &#xff08;三&#xff09;企業數據資產安全意識亟待提高 三、定制化數據資產安全策略的重要性 &#xff08;一&#…

natvicat為什么連不上linux上的mysql?

老規矩&#xff0c;廢話不多說&#xff0c;直接上教程。 號外&#xff0c;數據庫管理工具領域的知名品牌Navicat&#xff0c;推出其免費版本——Navicat Premium Lite&#xff0c;用戶可從Navicat官網下載體驗這款軟件。 https://www.navicat.com.cn/download/navicat-premium-…

【HALCON】如何實現hw窗口自適應相機拍照成像的大小

前言 在開發一個噴碼檢測軟件的時候碰到相機成像和hw窗體的大小不一致&#xff0c;hw太小顯示不完全成像的圖片&#xff0c;這使得成像不均勻&#xff0c;現場辨別起來比較不直觀&#xff0c;因此需要對其進行一個調整。 解決 省略掉讀取圖片的環節&#xff0c;我們只需要將…

別再用this.$forceUpdate()了!—性能優化篇

文章目錄 別再用this.$forceUpdate()了&#xff01;—性能優化篇&#x1f388;介紹&#x1f9e8;弊端注意事項 &#x1f386;解決實例 別再用this.$forceUpdate()了&#xff01;—性能優化篇 起因是接手公司之前外包的項目做項目優化&#xff0c;代碼看著一言難盡&#xff0c;…

CGI面試題及參考答案

什么是CGI?它在Web服務器與應用程序之間扮演什么角色? CGI(Common Gateway Interface) 是一種標準協議,它定義了Web服務器與運行在服務器上的外部程序(通常是腳本或應用程序)之間的通信方式。簡單來說,CGI充當了一個橋梁,使得Web服務器能夠將用戶的請求傳遞給后端程序…

ruoyi—cloud 新建模塊+生成代碼

1.復制一個模塊——修改名字 2.打開模塊下的yml文件&#xff0c;修改端口號和名字 &#xff08;1&#xff09;修改一個名字 &#xff08;2&#xff09;打開yml文件 &#xff08;3&#xff09;修改端口號&#xff0c;不要重復 &#xff08;4&#xff09;改名字和模塊一致 3.…

41、web基礎和http協議

web基礎與http協議 一、web web&#xff1a;就是我們所說得頁面&#xff0c;打開網頁展示得頁面。&#xff08;全球廣域網&#xff0c;萬維網&#xff09; world wide webwww 分布式圖形信息系統 http&#xff1a;超文本傳輸協議 https&#xff1a;加密的超文本傳輸協議…

貓凍干可以天天喂嗎?喂凍干前要了解的必入主食凍干榜單

近年來&#xff0c;凍干貓糧因其高品質而備受喜愛&#xff0c;吸引了無數貓主人的目光&#xff0c;對于像我這樣的養貓達人來說&#xff0c;早已嘗試并認可了凍干喂養。然而&#xff0c;對于初入養貓行列的新手們來說&#xff0c;可能會有疑問&#xff1a;什么是凍干貓糧&#…

Qt——界面優化

目錄 QSS 基本語法 QSS 設置方式 指定控件樣式設置 全局樣式設置 文件加載樣式表 Qt Designer 編輯樣式 選擇器 子控件選擇器 偽類選擇器 樣式屬性 盒模型 控件樣式 按鈕 復選框 單選框 輸入框 列表 菜單欄 登錄界面 繪圖 概念 繪制形狀 繪制線段 繪制…

微信換手機號了怎么綁定新手機號?

微信換手機號了怎么綁定新手機號&#xff1f; 1、在手機上找到并打開微信&#xff1b; 2、打開微信后&#xff0c;點擊底部我的&#xff0c;并進入微信設置&#xff1b; 3、在微信設置賬號與安全內&#xff0c;找到手機號并點擊進入&#xff1b; 4、選擇更換手機號&#xff0c…

【代碼隨想錄算法訓練Day52】LeetCode 647. 回文子串、LeetCode 516.最長回文子串

Day51 動態規劃第十三天 LeetCode 647. 回文子串 dp數組的含義&#xff1a;i到j的子串是否是回文的&#xff0c;是的話dp[i][j]1 遞推公式&#xff1a;if(s[i]s[j]) i j 一個元素 是回文的 |i-j|1 兩個元素 是回文的 j-i>1 判斷dp[i1][j-1] 初始化&#xff1a;全部初始化成…

在代理服務器環境中配置pip源的全面指南

引言 Python的包管理工具pip是開發者和系統管理員常用的工具之一&#xff0c;用于安裝和管理Python庫。然而&#xff0c;在某些網絡環境下&#xff0c;如公司內網或某些國家&#xff0c;直接訪問pip默認源可能會受到限制。此外&#xff0c;通過代理服務器訪問可以提高訪問速度…