-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.go
58 lines (55 loc) · 1.33 KB
/
socket.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
package main
import (
"net"
)
func create_main_connection(l *LogManager) (net.Conn, error) {
var connection net.Conn
var err error
var server_available bool = false
for !server_available {
connection, err = net.Dial("tcp", "localhost:5001")
if err != nil {
l.errorLog.Println("Error opening connection to the main socket.")
l.errorLog.Println(err.Error())
return nil, err
} else {
server_available = true
}
}
var connected bool = false
for !connected {
_, err = connection.Write([]byte("[syn]\x00"))
if err != nil {
l.printExecLog("Error writing to the main socket.")
l.errorLog.Println(err.Error())
continue
}
response := make([]byte, 1024)
_, err = connection.Read(response)
if err != nil {
l.printExecLog("Error reading from the main socket.")
l.errorLog.Println(err.Error())
continue
}
if string(response) == "[ack]\x00" {
connected = true
}
}
return connection, nil
}
func get_new_socket(l *LogManager) (net.Conn, error) {
var connection net.Conn
var err error
var server_available bool = false
for !server_available {
connection, err = net.Dial("tcp", "localhost:5001")
if err != nil {
l.errorLog.Println("Error opening connection to the socket.")
l.errorLog.Println(err.Error())
return nil, err
} else {
server_available = true
}
}
return connection, nil
}