-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
47 lines (38 loc) · 823 Bytes
/
main.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
// TCP SERVER
package main
import (
"log"
"net"
"time"
)
func do(conn net.Conn) {
buf := make([]byte, 1024)
//skiping the no of byte by writing '_'
_, err := conn.Read(buf)
if err != nil {
log.Fatal(err)
}
//Memeking some fake process
log.Println("⭕processing the request")
time.Sleep(6 * time.Second)
conn.Write([]byte("HTTP/1.1 200 OK\r\n\r\nHello, Golang!\r\n"))
//Close connection
conn.Close()
}
func main() {
//Reserving a port for our tcp server to listen to client request.
listener, err := net.Listen("tcp", ":1756")
if err != nil {
log.Fatal(err)
}
for {
//Waiting for a connection
log.Println("waiting for a client to connrct")
conn, err := listener.Accept()
if err != nil {
log.Fatal(err)
}
log.Println("🤗client connected successfully🌻")
go do(conn)
}
}