unix domain socket
unix domain socket 是在socket架構(gòu)上發(fā)展起來的用于同一臺主機的進程間通訊(IPC: Inter-Process Communication)勺爱,它不需要經(jīng)過網(wǎng)絡協(xié)議棧,不需要打包拆包屯伞、計算校驗和、維護序號和應答等,只是將應用層數(shù)據(jù)從一個進程拷貝到另一個進程光戈。UNIX Domain Socket有SOCK_DGRAM或SOCK_STREAM兩種工作模式枷颊,類似于UDP和TCP暇仲,但是面向消息的UNIX Domain Socket也是可靠的,消息既不會丟失也不會順序錯亂疚漆。
UNIX Domain Socket可用于兩個沒有親緣關(guān)系的進程酣胀,是全雙工的,是目前使用最廣泛的IPC機制娶聘,比如X Window服務器和GUI程序之間就是通過UNIX Domain Socket通訊的闻镶。
UNIX Domain socket與網(wǎng)絡socket類似,可以與網(wǎng)絡socket對比應用丸升。
上述二者編程的不同如下:
- address family為AF_UNIX
- 因為應用于IPC铆农,所以UNIXDomain socket不需要IP和端口,取而代之的是文件路徑來表示“網(wǎng)絡地址”狡耻。這點體現(xiàn)在下面兩個方面墩剖。
- 地址格式不同,UNIXDomain socket用結(jié)構(gòu)體sockaddr_un表示夷狰,是一個socket類型的文件在文件系統(tǒng)中的路徑岭皂,這個socket文件由bind()調(diào)用創(chuàng)建,如果調(diào)用bind()時該文件已存在沼头,則bind()錯誤返回爷绘。
- UNIX Domain Socket客戶端一般要顯式調(diào)用bind函數(shù),而不象網(wǎng)絡socket一樣依賴系統(tǒng)自動分配的地址瘫证∪嘌郑客戶端bind的socket文件名可以包含客戶端的pid,這樣服務器就可以區(qū)分不同的客戶端背捌。
下面用python代碼演示uds的使用
Python代碼演示
服務端
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 12/11/17 11:55 AM
@author: Chen Liang
@function: socket_echo_server_uds
"""
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import socket
import os
server_address = './uds_socket'
# Make sure the socket does not already exist
try:
os.unlink(server_address)
except OSError:
if os.path.exists(server_address):
raise
# Create a UDS socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# Bind the socket to the address
print('starting up on {}'.format(server_address))
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
while True:
# Wait for a connection
print('waiting for a connection')
connection, client_address = sock.accept()
try:
print('connection from', client_address)
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(16)
print('received {!r}'.format(data))
if data:
print('sending data back to the client')
connection.sendall(data)
else:
print('no data from', client_address)
break
finally:
# Clean up the connection
connection.close()
客戶端
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 12/11/17 11:55 AM
@author: Chen Liang
@function: socket_echo_client_uds
"""
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import socket
import sys
# Create a UDS socket
sock = socket.socket(family=socket.AF_UNIX, type=socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = './uds_socket'
print('connecting to {}'.format(server_address))
try:
sock.connect(server_address)
except socket.error as msg:
print(msg)
sys.exit(1)
try:
# Send data
message = b'This is the message. It will be repeated.'
print('sending {!r}'.format(message))
sock.sendall(message)
amount_received = 0
amount_expected = len(message)
while amount_received < amount_expected:
data = sock.recv(16)
amount_received += len(data)
print('received {!r}'.format(data))
finally:
print('closing socket')
sock.close()
客戶端一次發(fā)送毙籽,服務端分批返回。
服務端輸出結(jié)果如下
root@ubuntu:~/PycharmProjects/python_scripts# python socket_echo_server_uds.py
starting up on ./uds_socket
waiting for a connection
('connection from', '')
received 'This is the mess'
sending data back to the client
received 'age. It will be'
sending data back to the client
received ' repeated.'
sending data back to the client
received ''
('no data from', '')
waiting for a connection
客戶端輸出結(jié)果如下
root@ubuntu:~/PycharmProjects/python_scripts# python socket_echo_client_uds.py
connecting to ./uds_socket
sending 'This is the message. It will be repeated.'
received 'This is the mess'
received 'age. It will be'
received ' repeated.'
closing socket
查看套接字文件的類型如下
root@ubuntu:~/PycharmProjects/python_scripts# ls -l ./uds_socket
srwxr-xr-x 1 root root 0 Dec 11 13:45 ./uds_socket
可見uds文件是socket類型毡庆。具體的linux文件類型有以下幾種:
Linux的文件類型有以下幾種:
文件類型 |
ls -l 顯示 |
---|---|
普通文件 | - |
目錄 | d |
符號鏈接 | l |
字符設(shè)備 | c |
塊設(shè)備 | b |
套接字 | s |
命名管道 | p |
參考:
念念不忘坑赡,必有回響烙如,小伙伴們幫我點個贊吧,非常感謝毅否。
我是職場亮哥亚铁,YY高級軟件工程師、四年工作經(jīng)驗螟加,拒絕咸魚爭當龍頭的斜杠程序員徘溢。
聽我說,進步多捆探,程序人生一把梭
如果有幸能幫到你然爆,請幫我點個【贊】,給個關(guān)注黍图,如果能順帶評論給個鼓勵曾雕,將不勝感激。
職場亮哥文章列表:更多文章
本人所有文章助被、回答都與版權(quán)保護平臺有合作剖张,著作權(quán)歸職場亮哥所有,未經(jīng)授權(quán)揩环,轉(zhuǎn)載必究搔弄!