Making a TCP echo server with TCP-IP sockets
In this section, we will set up some foundational packages, then build a basic echo server, followed by a more sophisticated version.
The first version of an echo server
The Sockets package defines all the types (such as IPAddr
, IPv4
, IPv6
, TCPSocket
, and UDPSocket
) and methods (such as connect
, listen
, accept
, bind
, send
, and recv
) for doing basic network communication in Julia.
Use the Sockets package to make IP addresses like this (see Chapter2\tcp_echoserver\ip_addr_uri.jl
):
using Sockets addr = ip"185.43.124.6" # 1 typeof(addr) # 2
Line 1 uses the ip"..."
string literal, and line 2 displays IPv4
. Let’s connect to a website and get its IP address:
connect("julialang.org", 80) # 3 getaddrinfo("julialang.org") ...