-
Notifications
You must be signed in to change notification settings - Fork 14
/
serverConn.go
56 lines (41 loc) · 1.27 KB
/
serverConn.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
// Interfaces for the server's establish tcp connection with a client
package main
import (
"log"
"syscall"
)
// A client/server connection accepted by TFOServer
type TFOServerConn struct {
sockaddr *syscall.SockaddrInet4
fd int
}
// Read the data from the client and immediately close the connection
func (cxn *TFOServerConn) Handle() {
defer cxn.Close()
log.Printf("Server Conn: Connection received from remote addr: %v, remote port: %d\n",
cxn.sockaddr.Addr, cxn.sockaddr.Port)
// Create a small buffer to store data from client
buf := make([]byte, 24)
// Read from the socket, assign to buffer
n, err := syscall.Read(cxn.fd, buf)
if err != nil {
log.Println("Failed to read() client:", err)
return
}
// Do nothing in particular with the response, just print it
log.Printf("Server Conn: Read %d bytes: %#v", n, string(buf[:n]))
// The defer will close the connection now
}
// Gracefully close the connection to a client
func (cxn *TFOServerConn) Close() {
// Gracefull close the connection
err := syscall.Shutdown(cxn.fd, syscall.SHUT_RDWR)
if err != nil {
log.Println("Failed to shutdown() connection:", err)
}
// Close the file descriptor
err = syscall.Close(cxn.fd)
if err != nil {
log.Println("Failed to close() connection:", err)
}
}