First commit

This commit is contained in:
Melon 2021-12-19 22:28:26 +00:00
commit 3c06ac4b8d
Signed by: melon
GPG Key ID: B0ADD5395BCDAAB6
9 changed files with 152 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
unix-sockets-test-go

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

7
.idea/discord.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="PROJECT_FILES" />
<option name="description" value="" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/unix-sockets-test-go.iml" filepath="$PROJECT_DIR$/.idea/unix-sockets-test-go.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

36
client.go Normal file
View File

@ -0,0 +1,36 @@
package main
import (
"io"
"log"
"net"
"time"
)
func reader(r io.Reader) {
buf := make([]byte, 1024)
for {
n, err := r.Read(buf)
if err != nil {
return
}
log.Println("<", string(buf[:n]))
}
}
func connectClient(addr *net.UnixAddr) {
unixSock, err := net.DialUnix("unix", nil, addr)
if err != nil {
log.Panic(err)
}
defer unixSock.Close()
go reader(unixSock)
for {
_, err := unixSock.Write([]byte("hi"))
if err != nil {
log.Fatalln("Write error:", err)
}
time.Sleep(10 * time.Second)
}
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module unix-sockets-test-go
go 1.17

40
main.go Normal file
View File

@ -0,0 +1,40 @@
package main
import (
"flag"
"log"
"net"
)
func main() {
var clientAddrStr string
var serverAddrStr string
flag.StringVar(&clientAddrStr, "c", "", "Connect to unix socket as client")
flag.StringVar(&serverAddrStr, "s", "", "Connect to unix socket as server")
flag.Parse()
var addrStr string
var isClient bool
if clientAddrStr != "" && serverAddrStr != "" {
log.Panicf("Can't connect as client and server at the same time")
} else if clientAddrStr != "" {
isClient = true
addrStr = clientAddrStr
} else if serverAddrStr != "" {
isClient = false
addrStr = serverAddrStr
} else {
flag.Usage()
return
}
unixAddr, err := net.ResolveUnixAddr("unix", addrStr)
if err != nil {
log.Panic(err)
}
if isClient {
connectClient(unixAddr)
} else {
connectServer(unixAddr)
}
}

40
server.go Normal file
View File

@ -0,0 +1,40 @@
package main
import (
"log"
"net"
)
func echoServer(c *net.UnixConn) {
for {
buf := make([]byte, 1024)
nr, err := c.Read(buf)
if err != nil {
return
}
data := buf[:nr]
println("<", string(data))
_, err = c.Write(data)
if err != nil {
log.Fatalln("Write error:", err)
}
}
}
func connectServer(addr *net.UnixAddr) {
unixSock, err := net.ListenUnix("unix", addr)
if err != nil {
log.Panic(err)
}
defer unixSock.Close()
for {
fd, err := unixSock.AcceptUnix()
if err != nil {
log.Fatalln("Accept error:", err)
}
go echoServer(fd)
}
}