SocketConnect

SocketConnect[address]

makes a socket connection to the TCP host at the specified address and returns a SocketObject representing the connection.

SocketConnect[address,"protocol"]

makes a connection to the host at address with the specified protocol.

SocketConnect[socket]

makes a connection to a local socket opened in the current session.

Details

  • Possible socket addresses include:
  • {host,port}hostname or IP address and port number
    {host,"service"}hostname or IP address and service name
    "host:port"hostname or IP address with port
    portlocal socket localhost:port
    URL["url"] or "url"complete URL (e.g. http://www.wolfram.com)
  • Supported protocols include:
  • "TCP"raw TCP stream protocol
    "ZMQ"ZeroMQ one-to-one message protocol
    "ZMQ_type"ZeroMQ protocol of the specified type
  • For ZMQ sockets, all possible non-draft types are supported.
  • With no ZMQ protocol type specified, the underlying socket is a "Pair" socket.
  • Hostnames have forms like "www.wolfram.com".
  • IP addresses can be given as "140.177.0.0" or IPAddress[string], where string is an IPv4 or IPv6 IP address.
  • When the address is given as a URL with no port number specified, the port number is deduced from the URL scheme (e.g. port 80 for http).
  • SocketConnect[SocketObject[]] can be used to connect to a local socket opened by SocketOpen.
  • Close can be used to close a connection opened by SocketConnect.

Examples

open allclose all

Basic Examples  (2)

Open a connection to a server specified by a URL:

In[1]:=
Click for copyable input
Out[1]=

Write a basic HTTP request to the socket:

In[2]:=
Click for copyable input

Read the entire response as a string:

In[3]:=
Click for copyable input

Display the result, without extra newlines:

In[4]:=
Click for copyable input
Out[4]=

Close the socket connection:

In[5]:=
Click for copyable input
Out[5]=

Open a ZMQ socket:

In[1]:=
Click for copyable input
Out[1]=

Connect to the socket:

In[2]:=
Click for copyable input
Out[2]=

Send a string to the server socket:

In[3]:=
Click for copyable input

Read the string from the client socket:

In[4]:=
Click for copyable input
Out[4]=

Convert the ByteArray to a string:

In[5]:=
Click for copyable input
Out[5]=

Close the sockets:

In[6]:=
Click for copyable input
Out[6]=

Scope  (1)

Possible Issues  (2)

See Also

SocketOpen  SocketObject  Sockets  SocketListen  URLRead  HostLookup  IPAddress  URL

Introduced in 2015
(10.3)
| Updated in 2017
(11.2)