Created
April 27, 2020 04:16
-
-
Save kemingy/8be9c48b8e0b7cf359db7d2e7bb62e11 to your computer and use it in GitHub Desktop.
Unix Domain Socket Communication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net" | |
"time" | |
) | |
func main() { | |
conn, err := net.Dial("unix", "../uds_go.sock") | |
if err != nil { | |
log.Fatal("Dial error", err) | |
} | |
defer conn.Close() | |
go func (conn net.Conn) { | |
buf := make([]byte, 1024) | |
for { | |
len, err := conn.Read(buf[:]) | |
if err != nil { | |
return | |
} | |
log.Println("<<", string(buf[0:len])) | |
} | |
}(conn) | |
for { | |
msg := "hello world" | |
_, err := conn.Write([]byte(msg)) | |
if err != nil { | |
log.Fatal("send to server error", err) | |
break | |
} | |
log.Println("<", msg) | |
time.Sleep(1e9) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import time | |
socket_path = '../uds_socket_py' | |
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock: | |
sock.connect(socket_path) | |
while True: | |
msg = b'Message from client.' | |
print('client sending {!r}'.format(msg)) | |
sock.sendall(msg) | |
amount_recv = 0 | |
amount_expect = len(msg) | |
while amount_recv < amount_expect: | |
data = sock.recv(8) | |
amount_recv += len(data) | |
print('received: {!r}'.format(data)) | |
print('...') | |
time.sleep(1) | |
print('close client socket') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net" | |
"os" | |
) | |
const sockAddr = "../uds_go.sock" | |
func main() { | |
if _, err := os.Stat(sockAddr); err == nil { | |
if err := os.Remove(sockAddr); err != nil { | |
log.Printf("Remove socket file error: %v", err) | |
} | |
} | |
sock, err := net.Listen("unix", sockAddr) | |
if err != nil { | |
log.Fatal("Listen error: ", err) | |
} | |
defer sock.Close() | |
for { | |
conn, err := sock.Accept() | |
if err != nil { | |
log.Fatal("Accept error: ", err) | |
} | |
go func(conn net.Conn) { | |
for { | |
buf := make([]byte, 1024) | |
len, err := conn.Read(buf[:]) | |
if err != nil { | |
return | |
} | |
data := buf[0:len] | |
log.Println(">", string(data)) | |
data = append(data, "@@@"...) | |
_, err = conn.Write(data) | |
if err != nil { | |
log.Fatal("Send to client error: ", err) | |
} | |
} | |
}(conn) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import pathlib | |
socket_path = '../uds_socket_py' | |
path = pathlib.Path(socket_path) | |
if path.exists(): | |
print('delete socket file') | |
path.unlink() | |
# UDS | |
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock: | |
sock.bind(socket_path) | |
sock.listen(1) | |
while True: | |
print('waiting for connection') | |
conn, client = sock.accept() | |
with conn: | |
print('connection from', client) | |
while True: | |
data = conn.recv(16) | |
print('received {!r}'.format(data)) | |
if data: | |
print('send back') | |
conn.sendall(data) | |
else: | |
print('EOF') | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment