38 lines
633 B
Plaintext
38 lines
633 B
Plaintext
// Open server and client in 2 terminals:
|
|
// [1] q run examples/server
|
|
// [2] curl http://127.0.0.1:8080
|
|
import io
|
|
import net
|
|
import sys
|
|
|
|
main() {
|
|
socket := sys.socket(2, 1, 0)
|
|
|
|
if socket < 0 {
|
|
io.error("socket error\n")
|
|
sys.exit(1)
|
|
}
|
|
|
|
if net.bind(socket, 8080) != 0 {
|
|
io.error("bind error\n")
|
|
sys.exit(1)
|
|
}
|
|
|
|
if sys.listen(socket, 128) != 0 {
|
|
io.error("listen error\n")
|
|
sys.exit(1)
|
|
}
|
|
|
|
io.out("listening...\n")
|
|
|
|
loop {
|
|
conn := sys.accept(socket, 0, 0)
|
|
|
|
if conn >= 0 {
|
|
io.write(conn, "HTTP/1.0 200 OK\r\nContent-Length: 6\r\n\r\nHello\n")
|
|
sys.close(conn)
|
|
} else {
|
|
io.error("accept error\n")
|
|
}
|
|
}
|
|
} |