Making a TCP Connection
At last, we’re ready to make a TCP connection. To do it, we’ll write a helper function that takes a port and an address and performs all the necessary calls to getaddrinfo(), socket(), and connect(), in that order. We’ll call the function makeConnection(). If we get errors at any point, we can raise exceptions, just like in an ordinary Scala program, and return the socket as an Int otherwise.
| def makeConnection(address:CString, port:CString):Int = { |
| val hints = stackalloc[addrinfo] |
| string.memset(hints.asInstanceOf[Ptr[Byte]], 0, sizeof[addrinfo]) |
| hints.ai_family = AF_UNSPEC |
| hints.ai_socktype = SOCK_STREAM |
| |
| val addrInfo_ptr:Ptr[Ptr[ ... |
Get Modern Systems Programming with Scala Native now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.