({{, (RefLink[SocketOpen, paclet:ref/SocketOpen])[port]
opens a socket that accepts TCP connections to localhost:port and returns a (RefLink[SocketObject, paclet:ref/SocketObject]) representing the socket.}, {, (RefLink[SocketOpen, paclet:ref/SocketOpen])[address] 
opens a socket that accepts TCP connections to the specified local address. }, {, (RefLink[SocketOpen, paclet:ref/SocketOpen])[address,"protocol"]
opens a socket that accepts connections with the specified protocol.}})

OpenersNotesSection(Spacer1)Details

Supported protocols include:

For ZMQ sockets, all possible non-draft types are supported.

With no ZMQ protocol type specified, the underlying socket is a "ZMQ_PAIR" socket.

(RefLink[SocketOpen, paclet:ref/SocketOpen]) opens sockets only on your local machine.

(RefLink[SocketOpen, paclet:ref/SocketOpen]) can use local addresses only. Possible forms include:

Hostnames have forms like "www.wolfram.com".

IP addresses can be given as "140.177.0.0" or (RefLink[IPAddress, paclet:ref/IPAddress])[string], where string is an IPv4 or IPv6 IP address.

(RefLink[SocketConnect, paclet:ref/SocketConnect]) can be used to connect to sockets opened by (RefLink[SocketOpen, paclet:ref/SocketOpen]). The fact that a socket is open on one machine does not guarantee that another machine can connect to it.

For a connection to be established, the protocol must be consistent, and there must be no firewalls etc. blocking it.

(RefLink[Close, paclet:ref/Close]) can be used to close a connection opened by (RefLink[SocketOpen, paclet:ref/SocketOpen]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (6)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Open a socket to allow client connections to it:

server=SocketOpen[Automatic]

Connect a client to it:

client=SocketConnect[server]

Close the sockets:

Close/@{server,client}

Open a ZMQ socket:

zmqserver=SocketOpen[Automatic,"ZMQ"]

Connect a client to the server:

zmqclient=SocketConnect[zmqserver]

Write a message from the client to the server:

Write[zmqclient,"hello world"]

Read the message on the server:

Read[zmqserver]

Close the sockets:

Close/@{zmqclient,zmqserver}

OpenersExampleSection1(Spacer1)Scope  (2)

After clients have connected to an opened TCP socket, the clients can each be accessed as a (RefLink[SocketObject, paclet:ref/SocketObject]) with the property "ConnectedClients".

Open a server socket to allow connections, and connect a client to the socket:

server=SocketOpen[Automatic];
clients=Table[SocketConnect[server],2];

List the client sockets from the server's perspective:

server["ConnectedClients"]

These sockets also show up as remote sockets with (RefLink[Sockets, paclet:ref/Sockets]):

SubsetQ[Sockets["Remote"],server["ConnectedClients"]]

Close the sockets:

Close/@Append[clients,server]

Open a server socket on a specified IP address:

server1=SocketOpen[{"127.0.0.1",Automatic}]

Open a server socket on a specified port:

server2=SocketOpen[{"127.0.0.1",8000}]

Close the sockets:

Close/@{server1,server2}

OpenersExampleSection3(Spacer1)Applications  (1)

Implement a web server in the Wolfram Language with (RefLink[SocketOpen, paclet:ref/SocketOpen]).

Start listening on a TCP port:

s=SocketOpen[Automatic,"TCP"]

Initially there are no connected clients:

s["ConnectedClients"]

Connect to a the server from a web browser:

SystemOpen["http://"<>First[s["DestinationIPAddress"]]<>":"<>ToString[s["DestinationPort"]]]

Now there is at least one client from the web browser:

s["ConnectedClients"]

Write a generic response to all clients:

With[{html="<html>
<body>
<h1>Hello from the Wolfram Language</h1>
<p>
Today's date is : "<>DateString[]<>"
<p>
</body>
</html>"},WriteString[#,
"HTTP/1.1 200 OK
Date: "<>DateString[]<>" CDT
Content-Type: text/html
Content-Length: "<>ToString[StringLength[html]]<>"\n\n"<>html<>"\n\n\n"]]&/@s["ConnectedClients"]

Finally, close the connection to the clients:

Close/@s["ConnectedClients"]

OpenersExampleSection4(Spacer1)Possible Issues  (1)

Writing on a socket returned from (RefLink[SocketOpen, paclet:ref/SocketOpen]) only works for ZMQ sockets; for TCP sockets, you must use the "ConnectedClients" property.

Open a TCP and a ZMQ socket and connect clients:

tcpServer=SocketOpen[Automatic,"TCP"];
zmqServer=SocketOpen[Automatic,"ZMQ"];
{tcpClient,zmqClient}=SocketConnect/@{tcpServer,zmqServer};

Writing on the TCP socket fails because you have to write to a specific client:

WriteString[tcpServer,"hello world"]

However, writing to a ZMQ socket directly works:

WriteString[zmqServer,"hello world"];
ByteArrayToString@SocketReadMessage[zmqClient]

Close all the sockets:

Close/@{zmqClient,tcpClient,zmqServer,tcpServer}

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[SocketConnect, paclet:ref/SocketConnect])  ▪  (RefLink[SocketObject, paclet:ref/SocketObject])  ▪  (RefLink[SocketListen, paclet:ref/SocketListen])  ▪  (RefLink[CloudDeploy, paclet:ref/CloudDeploy])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Network Programming, paclet:guide/NetworkProgramming])}, {▪(RefLinkPlain[External Language Interfaces, paclet:guide/ExternalLanguageInterfaces])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(Spacer1)▪(WebLink[(Spacer1)ZMQ Socket Type Documentation, http://api.zeromq.org/4-2:zmq-socket])}})}})