63 lines
991 B
Odin
63 lines
991 B
Odin
package main
|
|
|
|
import "core:fmt"
|
|
import "core:net"
|
|
//import "core:os"
|
|
|
|
main :: proc() {
|
|
|
|
//args := os.args
|
|
//for arg in args {
|
|
// fmt.println(arg)
|
|
//}
|
|
//fmt.printf("hello! %s\n", "world")
|
|
server("127.0.0.1", 5500)
|
|
}
|
|
|
|
server :: proc(ip: string, port: int) {
|
|
|
|
addr, ok := net.parse_ip4_address(ip)
|
|
if !ok {
|
|
fmt.println("could not parse address")
|
|
return
|
|
}
|
|
|
|
endpoint := net.Endpoint {
|
|
address = addr,
|
|
port = port,
|
|
}
|
|
|
|
sock, err := net.listen_tcp(endpoint)
|
|
if err != nil {
|
|
fmt.printf("error creating sock fd: %s\n", err)
|
|
return
|
|
}
|
|
|
|
defer net.close(sock)
|
|
|
|
for {
|
|
client, src, err := net.accept_tcp(sock)
|
|
if err != nil {
|
|
fmt.println("error accepting client", err)
|
|
continue
|
|
}
|
|
fmt.println("src: ", src)
|
|
handleClient(client)
|
|
}
|
|
}
|
|
|
|
handleClient :: proc(sock: net.TCP_Socket) {
|
|
defer net.close(sock)
|
|
|
|
|
|
buffer: [2048]u8
|
|
|
|
bytes, err := net.recv_tcp(sock, buffer[:])
|
|
if err != nil {
|
|
fmt.println("error recv", err)
|
|
return
|
|
}
|
|
|
|
fmt.printf("%s\n", buffer)
|
|
}
|