-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_gpt.v
45 lines (37 loc) · 963 Bytes
/
tcp_gpt.v
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
import net
fn main() {
// Create a TCP listener
listener := net.listen_tcp('localhost:12345')
defer {
listener.close()
}
println('Listening on port 12345')
for {
// Accept incoming connections
conn, err := listener.accept()
if err != null {
panic(err)
}
// Handle the connection in a separate goroutine
go handle_connection(conn)
}
}
fn handle_connection(conn net.TcPConn) {
defer {
conn.close()
}
// Read data from the client
buffer := make([]byte{}, 1024)
n, err := conn.read(buffer)
if err != null {
panic(err)
}
// Print the received data
println('Received data:', buffer.slice(n))
// Send a response to the client
response := 'Hello, client!'
_, err = conn.write(response.bytes())
if err != null {
panic(err)
}
}