49 UDP
HardCangjie also supports UDP transmision protocol. For more information see the official docs
Set the server port. Function to create UDP server
Initilize UDP socket
Bind the socket to our IP.
Read the data transmited, we also accquire information about who sent use the messages.
Run the server asynchronously
Bind the socket to our IP. Send messages to the IP address (local host) on the specified port.
UDP.cj
import std.socket.*
import std.time.*
import std.sync.*
let SERVER_PORT: UInt16 = 8080
func runUdpServer() {
try (serverSocket = UdpSocket(bindAt: SERVER_PORT)) {
serverSocket.bind()
let buf = Array<Byte>(3, item: 0)
let (clientAddr, count) = serverSocket.receiveFrom(buf)
let sender = clientAddr.hostAddress
println("Server receive ${count} bytes: ${buf} from ${sender}")
}
}
main() {
let future = spawn {
runUdpServer()
}
sleep(Duration.second)
try (udpSocket = UdpSocket(bindAt: 0)) {
udpSocket.sendTimeout = Duration.second * 2
udpSocket.bind()
udpSocket.sendTo(
SocketAddress("127.0.0.1", SERVER_PORT),
Array<Byte>([1, 2, 69])
)
}
future.get()
}