<head><title>ColdC: Networking</title></head> <body> <h1 align=center><a href="/ColdC/">ColdC</a>: Networking</h1> <hr> ColdC gives the ability to handle network sockets with the following functions: <blockquote> <pre> <a href="Functions/bind_port.html">bind_port()</a> <a href="Functions/close_connection.html">close_connection()</a> <a href="Functions/connection.html">connection()</a> <a href="Functions/cwrite.html">cwrite()</a> <a href="Functions/cwritef.html">cwritef()</a> <a href="Functions/open_connection.html">open_connection()</a> <a href="Functions/reassign_connection.html">reassign_connection()</a> <a href="Functions/unbind_port.html">unbind_port()</a> </pre> </blockquote> Connections are bound to a <i>connection object</i>. The driver will use the following methods on a connection object: <blockquote> <pre> .connect() .disconnect() .failed() .parse() </pre> </blockquote> There are two types of connections which can be created. The first is a server connection, or a passive connection. To do this the driver listens on a network port and waits for incoming connections. The second type is an client connection, or a active connection. This type of connection is established by the driver to another network host. <h2>Server Connection</h2> <p>To establish a server connection first call the function <code>bind_port()</code>. The argument to this function is the network port to listen on. Note: most operating systems will restrict low numbered ports (usually anything below 1024 is restricted, and can only be opened with special privelages). If there are no errors, the current object is listening as a server on the specified port. <p>When an external client opens a connection the driver will call the method <code>.connect()</code> with two arguments. The first argument is a STRING, specifying the remote IP address of the client. The second argument is an INTEGER, specifying the socket where the connection was established. In general this number can be ignored. <p>Data received on the connection will be sent as a buffer to the method <code>.parse()</code>, after the method <code>.connect()</code> is called. Data is sent to the connection using the functions <code>cwrite()</code> and <code>cwritef()</code>. <p>If the client terminates the connection, the method <code>.disconnect()</code> is called. Note: this method will not be called if you terminated the connection. <h3>Multiple Server Connections</h3> <p>If multiple connections will be received on the network port, it is suggested that when a connection is started the connection object either reassigns the connection to another connection object (using the function <code>reassign_connection()</code>), or it notifies a new connection object to re-bind the port to itself. If this is not done, new connections will preempt and close the older connection (as only one connection can be on an object at a time). <h2>Client Connection</h2> <p>To establish a client connection call the function <code>open_connection()</code> with the first argument as a STRING specifing the IP address of the host, and the second argument an INTEGER specifying the network port to connect on. This function is not a blocking function. Calling it will simply start the process of opening a connection. If there are no immediate errors the function will return normally. <p>When a connection is opened the driver will call the method <code>.connect()</code> on the current object, with the argument being an INTEGER task_id for the task which called <code>open_connection()</code>. If you wish, you may <code>suspend()</code> after calling <code>open_connection()</code>, then have the method <code>.connect()</code> resume the task when it is received. <p>If the connection could not be established, the method <code>.failed()</code> is called instead. The first argument is once again the task id, the second argument is an ERROR representing why the connection could not be established. <p>Once the connection is established, input and output is handled the same as on a server connection. <p> <hr size=4><p align=center><i>Last Modified on March 23 1996</i> <br><i>Copyright © 1995, 1996, Brandon Gillespie</i> </body>