tcp.ew


version 2.21
written by Jason Mirwald : mirwalds@prodigy.net
LAST MODIFIED : 10/09/02

tcp readme.htm written by kat

This library is for asynchronous TCP socket communication. At the current time, it handles Internet style addresses only (the most common in use).

Note: The application is responsible for tracking and closing all socket handles. No internal storage is done for the sockets themselves.

If you were previously using an earlier version of tcp.ew, please see the Version Differences comments at the end of the this file.

 


  Function Index

tcp_startup
tcp_cleanup
tcp_inet_addr
tcp_inet_ntoa
tcp_socket
tcp_close
tcp_listen
tcp_bind
tcp_accept
tcp_connect
tcp_recv
tcp_recv_seq
tcp_send
tcp_send_seq
tcp_gethostbyname
tcp_gethostbyaddr
tcp_host_info
tcp_gethostname
tcp_getsockname
tcp_getpeername
tcp_getlasterror
tcp_setlasterror
tcp_set_window
tcp_ip_list
tcp_packet_size
tcp_cancel_lookup

Pre-defined variables:

FD_CLOSE
FD_CONNECT
FD_ACCEPT
FD_WRITE
FD_READ
INVALID_SOCKET
INADDR_ANY
INADDR_LOOPBACK



( INVALID_SOCKET is sorta self-explanatory )


tcp_startup( atom major, atom minor )

Description: Initialize winsock; this routine must be called before any other tcp routines are called. More than one call to tcp_startup() can be made, but tcp_cleanup() must be called once for each time that tcp_startup() is called.

Returns: Zero if successful, otherwise it will return one of the error values in tcp.ew.


tcp_cleanup()

Description: Cleans up resources used by windows, and cancels any asynchronous routines that are still pending. More than one call to tcp_startup() can be made, but tcp_cleanup() must be called once for each time that tcp_startup() is called.

Note: A call to tcp_cleanup() will not close open sockets. The application is responsible for closing any valid sockets before calling tcp_cleanup()

Returns: This procedure does not return a value.


tcp_inet_addr( sequence in_addr )

Description: This routine converts a character string representing an internet address, expressed in the standard "." notation, into an atom value suitable for use in calls requiring an internet address.

Returns:  This function returns -1 if the character string is invalid, otherwise it returns an atom value representing the address.


tcp_inet_ntoa( atom in_addr )

Description: This routine converts an atom value into a character string representing an internet address, expressed in the standard "." notation.

Returns:  This function returns a null sequence, {}, if the atom is invalid, otherwise it returns a character string representing the address.


tcp_cancel_lookup( atom task )

Description: Cancels a pending call to tcp_gethostbyname() or tcp_gethostbyaddr(), and frees resources used internally.

Returns: This procedure does not return a value.


tcp_host_info( atom task )

Description: This routine gathers information on the data returned from a call to tcp_gethostbyname() or tcp_gethostbyaddr(), and frees resources used internally by tcp.ew.

Note: This routine can cause an exception if there was an error in the lookup function. tcp_cancel_lookup() should be called in the event that an error is returned to prevent this, and allow the resources to be freed.

Returns: This function returns a sequence with the following format:

{ ip_address, host_name }

ip_address = a sequence of 4 bytes representing the host address in network-byte order

host_name = A sequence (character string) representing the domain name on the host computer.


tcp_ip_list( atom task )

Description: This routine is used to retrieve the list of IP addresses that are assigned to the computer specified in a call to tcp_gethostbyname() or tcp_gethostbyaddr().

Note: This routine can cause an exception if there was an error in the lookup function. tcp_cancel_lookup() should be called in the event that an error is returned to prevent this, and allow the resources to be freed.

Returns: This function returns a sequence with the following format:

{ ip_address_1, ip_address_2, ip_address_3, ... }

Each IP address in the sequence that is returned is a sequence of four bytes in network byte order.


tcp_gethostname()

Description: Returns the network name of the computer that the application is running on.

Note: The name returned by tcp_gethostname() and the domain name of the machine may not be the same on a network.

Returns: This routine returns a character string.


tcp_getsockname()

Description: Returns the local ip address and port of a connected or listening socket in network byte order.

Returns: This function returns a sequence with the following format:

{ ip_address, port }

ip_address = a sequence of 4 bytes representing the host address

port = An atom representing the port number that the socket is using on the host computer.


tcp_getpeername()

Description: Returns the ip address of the machine that a socket is connected to in network byte order.

Returns: This function returns a sequence with the following format:

{ ip_address, port }

ip_address = a sequence of 4 bytes representing the host address in network-byte order

port = An atom representing the port number that the socket is using on the host computer.


tcp_gethostbyname( atom hwnd, sequence host_name )

Description: Begins an asynchronous call to retrieve information on a specific host computer identified by host_name.

Note: The application is notified via a WM_HOSTINFO message posted to the window identified by hwnd when the operation is complete. The message parameters will have the following meaning;

hwnd
iMsg
wParam
hi_word(lParam)
window to post the message to
WM_HOSTINFO
task-handle returned by tcp_gethostbyname()
error code, or zero on success

Returns: This function returns a handle to a task that can be used to retrieve the information about the host computer when the operation is complete, or to cancel the call using tcp_cancel_lookup().


tcp_gethostbyaddr( atom hwnd, sequence host_addr )

Description: Begins an asynchronous call to retrieve information on a specific host computer identified by host_addr.

Note: The application is notified via a WM_HOSTINFO message posted to the window identified by hwnd when the operation is complete. The message parameters will have the following meaning;

hwnd
iMsg
wParam
hi_word(lParam)
window to post the message to
WM_HOSTINFO
task-handle returned by tcp_gethostbyname()
error code, or zero on success

Returns: This function returns a handle to a task that can be used to retrieve the information about the host computer after the operation is complete, or to cancel the call using tcp_cancel_lookup().


tcp_getlasterror()

Description: Use tcp_getlasterror() to retrieve the error code for the last network error that occurred.

Returns: Returns an atom representing the last error that occurred.


tcp_setlasterror( atom error )

Description: Use tcp_setlasterror() to set the last error code for any subsequent calls to tcp_getlasterror().

Note: Any subsequent errors that occur will override the value set by calling tcp_setlasterror().

Returns: This procedure does not return a value.


tcp_set_window( atom hwnd, atom socket)

Description: This function sets the window that will receive notification of network events for a specific socket. The type of events that are received by hwnd are not changed.

Returns: This function returns 0 on success, or -1 (INVALID_SOCKET) on failure.


tcp_socket( atom hwnd )

Description: This function is used to create an asynchronous STREAM-type socket. The window identified by the hwnd argument will receive notifications of any events that occur in the form of a WM_SOCKET message. The message posted to the window will have the following format;

hwnd
iMsg
wParam
lo_word(lParam)
hi_word(lParam)
window that receives the message
WM_SOCKET
handle of the socket on which the event occurred
event code (see below)
error code or zero if no error

Note: The application is responsible for closing any sockets that it creates.

Returns: This function returns the handle of an asynchronous STREAM-type socket.

Event Codes:
FD_READ
FD_WRITE
FD_ACCEPT
FD_CONNECT
FD_CLOSE
indicates the socket has data to read
indicates readiness for writing
notification of incoming connections
indicates completed/failed connection
indicates closure of a connection


tcp_close( atom socket )

Description: Closes a socket and releases the resources reserved for it's use. This routine must be called for each socket that is created by an application.

Note: Receiving an FD_CLOSE event on a socket does not indicate that the socket has closed, it merely signifies that the connection has closed. An application must call tcp_close() to properly close the socket and free resources.

Returns: This procedure does not return a value.


tcp_listen( atom socket, object ip, atom port )

Description: This function attempts to associate a socket with a particular ip address and port, and listen for incoming connections. The object ip can be a sequence of four bytes, or a 32-bit atom representing the ip address in network byte order. A socket can be set to listen without specifying a port by passing zero as the port number. The object ip can also be one of the pre-defined variables INADDR_ANY or INADDR_LOOPBACK.

INADDR_ANY INADDR_LOOPBACK #00000000 - matches any ip address #7F000001 - localhost (127.0.0.1)

Note: If two sockets attempt to listen on the same ip address and port, an error will occur. Failure to set a socket to listen for connections does not indicate the closure of the socket. At this time, a socket can have only 5 requests pending at one time. This is a winsock limitation.

Returns: Returns 0 on success, or -1 (INVALID_SOCKET) on failure. Any error that occurred can be retrieved using tcp_getlasterror().


tcp_accept( atom socket )

Description: This function retrieves the first available connection request from the queue of a listening socket, and attempts to establish a connection.

Note: The newly created socket will be set to notify the same window of any network events that occur, that the listening socket was set to notify.

Returns: If the function fails, the return value will be -1, or INVALID_SOCKET. If the function succeeds, the handle to an asynchronous STREAM-type socket will be returned.


tcp_connect( atom socket, sequence ip, atom port )

Description: This procedure attempts to connect a socket to the ip address and port specified. Success or failure are indicated by a WM_SOCKET message being posted to the window specified when the socket was created.

Note: The ip address should be a sequence of four bytes in network byte order, such as a call to tcp_host_info() might return.

Returns: This procedure does not return a value. Success or failure of a connection attempt will be indicated by the error code in the hi_word(lParam) when the application's window receives the FD_CONNECT message.


tcp_send( atom socket, atom buffer, atom length_buffer )

Description: This function sends data through the specified socket. The argument buffer is a pointer to the memory address where the data to be sent is stored, and length_buffer specifies the number of bytes to send.

Returns: This function returns and integer indicating the number of bytes that were successfully sent.


tcp_recv( atom socket, atom buffer, atom length_buffer )

Description: This function retrieves pending data from the queue of the specified socket. The atom buffer points to an address in memory where the data should be written to, and the atom buffer_length specifies the maximum number of bytes to write.

Note: When a socket has data pending in it's queue, it must call tcp_recv() or tcp_recv_seq() to clear the data from the queue.

Returns: This function returns the number of bytes written to the buffer specified.


tcp_packet_size( integer size_buffer )

Description: This routine is used to set a maximum packet size for use with calls to tcp_send_seq() and tcp_recv_seq(). Any data sent or received will not be allowed to exceed the size indicated by size_buffer. Instead, it will be divided into "packets" of information of size_buffer or less when it is sent or received.

Returns: This procedure does not return a value.


tcp_send_seq( atom socket, sequence data )

Description: This function sends a sequence of bytes through the indicated socket. If the number of bytes is larger than the maximum packet size set by tcp_packet_size(), the sequence is broken into packet-size slices for sending.

Note: If the length of the sequence is less than the size of a packet, as set by tcp_packet_size(), the sequence is not modified, only the number of bytes provided in the argument data is sent.

Returns: This function returns the number of bytes successfully sent.


tcp_recv_seq( atom socket )

Description: This function retrieves data from the queue of the indicated socket.

Note: If the amount of data in the queue of the socket is more than the maximum packet-size, the extra data will remain in the queue, and another FD_READ message will be generated.

Returns: This function returns a sequence of bytes up to the number of bytes set by a call to tcp_packet_size(). On error, a sequence of length-zero will be returned, and tcp_getlasterror() should be called to retrieve the error-code.


tcp_bind( atom hsock, object ip, atom port )

Description: This function attempts to associate a socket with a specific IP address and port. The object IP can be a sequence of four bytes, or a 32-bit atom representing the IP address in network byte order. The object IP can also be one of the pre-defined variables INADDR_ANY or INADDR_LOOPBACK.

Note: If two sockets attempt to use the same IP address and port, an error will occur. Failure to associate a socket with an IP address and port does not indicate the closure of the socket.

Returns: Returns 0 on success, -1 on failure. The specific error that occurred can be retrieved using tcp_getlasterror().

 


Version Differences

V2.21:

Fixed an error in the calling of the C routines internally, and changed tcp_set_window() to allow a user to specify the window message that will be sent to a window in addition to the window events will be sent to.

V2.10:

Renamed the routines get_host_into(), get_ip_list() and set_packet_size() to be prefixed "with tcp_"; tcp_host_info(), tcp_ip_list() and tcp_packet_size() respectively.

tcp_listen() now returns 0 (zero) on success and -1 on failure. This routine was the only routine in version 2.00 that did not return a zero on success, and was changed to provide continuity throughout the library.

 V2.00:

tcp_getpeername() and tcp_getsockname() now return sequences containing the ip address as well as the port number of the socket or it's peer.