home *** CD-ROM | disk | FTP | other *** search
- /* Socket.h */
-
- /* ExtrasLib by Peter Hartley 1995-96
- * (K) All Rites Reversed - Copy What You Like
- */
-
- typedef struct Socket *Socket; /* abstract type */
-
- typedef enum {
- socket_STREAM = 1, /* not implemented in InternetA */
- socket_DATAGRAM,
- socket_RAW
- } socket_type;
-
- typedef struct {
- unsigned short family;
- unsigned short port;
- unsigned int address;
- char _zero[8];
- } socket_description;
-
- #define address_BROADCAST 0xFFFFFFFF
-
- os_error *Socket_Create( Socket *s, socket_type type );
- os_error *Socket_SendTo( Socket s, void *message, int messagelen,
- unsigned int ipaddress, int port );
- int Socket_RecvFrom( Socket s, void *buffer, int buflen,
- unsigned int *wasfrom );
-
- /* these options are off by default, which with NonBlocking is very
- unlikely to be what you want */
- os_error *Socket_NonBlocking( Socket s, BOOL nonblocking );
- os_error *Socket_Asynchronous( Socket s, BOOL asynchronous );
- os_error *Socket_AllowBroadcast( Socket s, BOOL allowed );
-
- os_error *Socket_Describe( Socket s, socket_description *desc );
- os_error *Socket_Bind( Socket s, int port );
- os_error *Socket_Close( Socket *s );
-
- /* Typical code which reads from a socket:
-
- Socket s;
- unsigned int sender;
-
- Socket_Create( &s );
- Socket_Bind( s, <port number> );
- Socket_NonBlocking( s, TRUE ); |* RecvFrom stiffs otherwise *|
- etc
- for (;;)
- {
- Wimp_Poll()
- etc
- if ( Socket_RecvFrom( s, buffer, sizeof(buffer), &sender ) )
- {
- process information in buffer
- ('sender' has been filled in with the sender's address)
- }
- }
- Socket_Close( s );
-
- Writing to a socket:
-
- Socket_Create( &s );
- |* Socket_AllowBroadcast( s, TRUE ); *| (optionally)
- etc
- Socket_Sendto( s, buffer, sizeof(buffer), <address>, <port number> );
- etc
- Socket_Close( s );
-
- The same socket can be used for reading and writing.
- If your program terminates without calling Socket_Close, the
- socket remains open, and any further attempts to connect to the same
- port number will fail. Moral: *don't* call Socket_Close at the end of
- your program, as in the fragments above! - put it in an atexit()
- handler instead.
-
- An asynchronous socket is one which generates Internet events when
- packets arrive. Note that the PRM is WRONG - your event handler is called
- with event_INTERNET = 19 in R0 sure enough, and the socket number in
- R2, but R1 contains internetevent_URGENT = 1.
-
- To read your own internet address, read the system variable
- Inet$LocalAddr, which is a string containing the address as 8 hex digits.
-
- Port numbers must be supplied with the bytes the wrong way round -
- eg, to connect to 'talk' (port number 0x205) set sd.port = 0x502.
- Internet addresses must also be supplied with the bytes the wrong
- way round - i.e. station number in most significant byte etc. - but
- this is the way round Inet$LocalAddr has it anyway.
-
- Rather than use your own broadcast messages and dedicated port, a
- good way to announce the availability of a service is to obtain any
- unallocated port, and then use Freeway to distribute the port number
- and your internet address to anyone who might be interested.
- */
-