-
Notifications
You must be signed in to change notification settings - Fork 0
/
rowrow.go
79 lines (72 loc) · 1.93 KB
/
rowrow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"net"
"io"
"fmt"
"log"
"os"
"time"
)
func tcpHandling(urlport string) {
// Listen on TCP port 1234
fmt.Println("Listening to %s", urlport)
tcpSock, err := net.Listen("tcp", urlport)
if err != nil {
log.Fatal(err)
}
defer tcpSock.Close()
for {
// Wait for a connection.
conn, err := tcpSock.Accept()
if err != nil {
log.Fatal(err)
}
// Handle the connection in a new goroutine.
// The loop then returns to accepting, so that
// multiple connections may be served concurrently.
go func(c net.Conn) {
// Echo all incoming data.
defer c.Close()
io.Copy(os.Stdout, c)
// Shut down the connection.
c.Close()
}(conn)
}
}
func whatisgoingon(haha string) {
fmt.Println("this is going on=%s", haha)
}
func unixHandling(fileptr string) {
// Read from unix domain socket
fmt.Println("Listening to %s", fileptr)
unixSock, err := net.Listen("unix", fileptr)
if err != nil {
log.Fatal(err)
}
defer unixSock.Close()
for {
// Wait for a connection.
conn, err := unixSock.Accept()
if err != nil {
log.Fatal(err)
}
// Handle the connection in a new goroutine.
// The loop then returns to accepting, so that
// multiple connections may be served concurrently.
go func(c net.Conn) {
// Echo all incoming data.
io.Copy(c, c)
// Shut down the connection.
c.Close()
} (conn)
}
}
func main() {
whatisgoingon("programming")
go tcpHandling("localhost:1234")
// go unixHandling("/tmp/ipc_sock")
whatisgoingon("trolol")
time.Sleep(60 * time.Second)
// Everytime the Unix socket sends a new packet, close the old
// TCP socket, re-open a new one and re-send the data to localhost:1234
}