mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-06 07:53:41 +00:00
8642886f73
According to man page accept(2), accept returns new client sockfd and remote peer ip:port. This patch corrects the Accept() prototype in the netdever interface to not take in an ip:port arg, but rather return an ip:port for remote peer. Tested with examples/net/tcpecho on wioterminal and nano-rp2040. Here's a run with wioterminal: SERVER ============ sfeldma@nuc:~/work/drivers$ tinygo flash -monitor -target wioterminal -size short -stack-size=8kb ./examples/net/tcpecho code data bss | flash ram 110876 2552 11212 | 113428 13764 Connected to /dev/ttyACM2. Press Ctrl-C to exit. Realtek rtl8720dn Wifi network device driver (rtl8720dn) Driver version : 0.0.1 RTL8720 firmware version : 2.1.2 MAC address : 2c:f7:f1:1c:9b:2f Connecting to Wifi SSID 'test'...CONNECTED DHCP-assigned IP : 10.0.0.140 DHCP-assigned subnet : 255.255.255.0 DHCP-assigned gateway : 10.0.0.1 Starting TCP server listening on :8080 Client 10.0.0.190:50000 connected Client 10.0.0.190:50000 closed CLIENT ============= nc -p 50000 10.0.0.140 8080
71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
// This example listens on port :8080 for client connections. Bytes
|
|
// received from the client are echo'ed back to the client. Multiple
|
|
// clients can connect as the same time, each consuming a client socket,
|
|
// and being serviced by it's own go func.
|
|
//
|
|
// Example test using nc as client to copy file:
|
|
//
|
|
// $ nc 10.0.0.2 8080 <file >copy ; cmp file copy
|
|
|
|
//go:build pyportal || arduino_nano33 || nano_rp2040 || metro_m4_airlift || arduino_mkrwifi1010 || matrixportal_m4 || wioterminal
|
|
|
|
package main
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
"net"
|
|
"time"
|
|
|
|
"tinygo.org/x/drivers/netlink"
|
|
"tinygo.org/x/drivers/netlink/probe"
|
|
)
|
|
|
|
var (
|
|
ssid string
|
|
pass string
|
|
port string = ":8080"
|
|
)
|
|
|
|
var buf [1024]byte
|
|
|
|
func echo(conn net.Conn) {
|
|
println("Client", conn.RemoteAddr(), "connected")
|
|
defer conn.Close()
|
|
_, err := io.CopyBuffer(conn, conn, buf[:])
|
|
if err != nil && err != io.EOF {
|
|
log.Fatal(err.Error())
|
|
}
|
|
println("Client", conn.RemoteAddr(), "closed")
|
|
}
|
|
|
|
func main() {
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
link, _ := probe.Probe()
|
|
|
|
err := link.NetConnect(&netlink.ConnectParams{
|
|
Ssid: ssid,
|
|
Passphrase: pass,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
println("Starting TCP server listening on", port)
|
|
l, err := net.Listen("tcp", port)
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
defer l.Close()
|
|
|
|
for {
|
|
conn, err := l.Accept()
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
go echo(conn)
|
|
}
|
|
}
|