Files

44 lines
985 B
Go
Raw Permalink Normal View History

2024-07-12 17:08:15 +08:00
package main
import (
"unsafe"
2025-04-03 15:52:18 +08:00
"github.com/goplus/lib/c"
"github.com/goplus/lib/c/net"
"github.com/goplus/lib/c/os"
2024-07-12 17:08:15 +08:00
)
func main() {
var buffer [256]c.Char
2024-07-18 10:28:14 +08:00
sockfd := net.Socket(net.AF_INET, net.SOCK_STREAM, 0)
2024-07-12 17:08:15 +08:00
defer os.Close(sockfd)
2024-07-18 10:28:14 +08:00
servAddr := &net.SockaddrIn{
Family: net.AF_INET,
Port: net.Htons(uint16(1234)),
Addr: net.InAddr{Addr: 0x00000000},
2024-07-12 17:08:15 +08:00
Zero: [8]c.Char{0, 0, 0, 0, 0, 0, 0, 0},
}
2024-07-18 10:28:14 +08:00
if res := net.Bind(sockfd, servAddr, c.Uint(unsafe.Sizeof(*servAddr))); res < 0 {
2024-07-12 17:08:15 +08:00
c.Perror(c.Str("bind error"))
return
}
2024-07-18 10:28:14 +08:00
if net.Listen(sockfd, 5) < 0 {
2024-07-12 17:08:15 +08:00
c.Printf(c.Str("listen error"))
return
}
c.Printf(c.Str("Listening on port 1234...\n"))
2024-07-18 10:28:14 +08:00
cliAddr, clilen := &net.SockaddrIn{}, c.Uint(unsafe.Sizeof(servAddr))
2024-07-12 17:08:15 +08:00
2024-07-18 10:28:14 +08:00
newsockfd := net.Accept(sockfd, cliAddr, &clilen)
2024-07-12 17:08:15 +08:00
defer os.Close(newsockfd)
c.Printf(c.Str("Connection accepted."))
os.Read(newsockfd, unsafe.Pointer(unsafe.SliceData(buffer[:])), 256)
c.Printf(c.Str("Received: %s"), &buffer[0])
}