\subsection{The network module} The network class encapsulates the network protocoll used for client server communications in SICS. This is the place to hack if SICS needs porting to other protocols. This class implements:\begin{verbatim} typedef struct { int sockid; int iType; struct sockaddr_in adresse; } mkChannel; \end{verbatim} This is the datastructure maintained for each socket. Never deal with it directly but through the functions given below. Wherever a timeout is specified as a parameter below the following rule applies: If timout is greater 0, then timeout means a time in milliseconds to wait with a select system call for data to arrive. If timeout is less or equal then 0 the socket will block on input. \begin{itemize} \item {\bf mkChannel *NETOpenPort(int iPort) }, opens a server port on port number iPort for listening. If successful returns a pointer to a fresh mkChannel datastructure , else NULL. \item {\bf mkChannel *NETAccept(mkChannel *self, int timeout) }, waits timeout microseconds for a client requesting a connection to the server This is done with thke select system call. If a request comes in a new mkChannel structure is created and returned. If not NULL will be returned. \item {\bf mkChannel *NETConnect(char *name, int port) } tries to connect to a server specified by the hostname name and the port number port. Returns a pointer to a new mkChannel structure on success, NULL else. \item {\bf int NETWrite(mkChannel *self, char *buffer, long lLen) } writes lLen bytes from buffer to the socket specified by self. Returns True on succees, else False. \item {\bf long NETRead(mkChannel *self, char *buffer, long lLen, int timeout) } waits timeout microseconds for data to arrive at the socket specified by self. If data is available it will be copied into the buffer, but to a maximum length of lLen. Returns the length of data read, 0 if there is no data and -1 if an EOF occured on the socket (i.e the partner closed the connection). \item {\bf int NETClosePort(mkChannel *self) } If you are done with talking you can use this to close the connection. The socket specified by self can no longer be used after this. You still have to free the mkChannel though, if you want to do it properly. The function returns True on success, False (0) if the call failed. \item {\bf mkChannel *UDPOpen(int iPort) } opens a port for connectionless UDP-connection. \item {\bf mkChannel UDPConnect(int iPort) } connects a client to a UDP port for Data transfer. \item {\bf long UDPRead(mkChannel self, char *buffer, long lLen, int timeout) } reads data from a UDP port inot buffer, but maximum lLen bytes. Waits maximum timeout microseconds for data. Returns the number of bytes read or -1 for end of file. \item {\bf int UDPWrite(mkChannel *self, char *buffer, long lLen); } writes lLen bytes from buffer into an UDP channel. \end{itemize} SICS needs I/O multiplexing, that is it needs to be able to handle request from many clients. Instead of spawning more processes or using threads with all the synchronisation problems associated with both of the above, I/O multiplexing is done with the select() system call here. Select uses a funny mask as input which specifies the id of the stream it is interested in. It requires the (stream-number (number, not FILE *) + 1 ) bit of the mask to be set for interest in stream number. This mask is set using macros defined in the apropriate header files.