<?xml version="1.0" encoding="utf-8" ?> <!-- for emacs: -*- coding: utf-8 -*- --> <!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" > <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><title>using sockets</title> <link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/> </head> <body> <table class="buttons"> <tr> <td><div><a href="_packages.html">next</a> | <a href="_communicating_spwith_spprograms.html">previous</a> | <a href="_packages.html">forward</a> | <a href="_communicating_spwith_spprograms.html">backward</a> | <a href="___The_sp__Macaulay2_splanguage.html">up</a> | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div> </td> </tr> </table> <div><a href="index.html" title="">Macaulay2Doc</a> > <a href="___The_sp__Macaulay2_splanguage.html" title="">The Macaulay2 language</a> > <a href="_using_spsockets.html" title="">using sockets</a></div> <hr/> <div><h1>using sockets</h1> <div><p>It's easy to use sockets as though they were files. Simply replace the file name by a string of the form <tt>$host:service</tt> where <tt>host</tt> is the name of IP number of host to contact, and <tt>service</tt> is the port number or name to use. If <tt>service</tt> is omitted, then port 2500 is used. If <tt>host</tt> is omitted, then an incoming connection will be listened for.</p> <p>The following code will illustrate two-way communication using sockets similar to the interaction used by web servers, and you may try it out on your machine, unless a firewall prevents it.</p> <pre>if (pid = fork()) == 0 then ( try "$:7500" << "hi there" << close; exit 0; ) sleep 2 get "$localhost:7500" wait pid</pre> <p>The code uses <a href="_fork.html" title="fork the process">fork</a> to create a separate process that will listen for a connection on port 7500 and then send us a message. The <a href="_sleep.html" title="sleep for a while">sleep</a> command pauses for a while to make sure the child process has had time to start listening. Then we use an ordinary input command, namely <a href="_get.html" title="get the contents of a file">get</a>, to obtain the message. Finally, we <a href="_wait.html" title="wait for child process">wait</a> for the child process to finish, as we should.</p> </div> <div class="single"><h2>See also</h2> <ul><li><span><a href="_open__In__Out.html" title="open an input output file">openInOut</a> -- open an input output file</span></li> <li><span><a href="_open__Listener_lp__String_rp.html" title="open a port for listening">openListener</a> -- open a port for listening</span></li> <li><span><a href="_get__W__W__W.html" title="get a web page">getWWW</a> -- get a web page</span></li> </ul> </div> </div> </body> </html>