Please enable JavaScript to view this site.

A-Shell Consolidated Reference

Navigation: Subroutines > TCPX

Advanced Server Connection Options

Scroll Prev Top Next More

Normally the TCPOP_ACCEPT operation will open a listening socket on the port specified by sockport, and then wait for a connection to be accepted. At that point, it closes the listening socket and returns the connection socket in sockport to the caller.

There are two shortcomings to this protocol. One is that while waiting for a connection to be accepted, the server application is suspended; if no client ever connects, the server would be suspended indefinitely, unless it was aborted by Ctrl+C or some other signal. The other is that any attempt by a client to connect to the server while the server is servicing a connection will be rejected. To get around either or both of these shortcomings, you can use the flags TCPXFLG_LISTEN, TCPXFLG_ASYNC, and TCPXFLG_KEEPLISTEN in conjunction with the TCPOP_ACCEPT operation.

If the TCPXFLG_LISTEN flag is specified, then the TCPOP_ACCEPT operation returns immediately, with sockport set to the listening socket rather than the connection socket. The application should then save this returned sockport value in a separate variable, perhaps called LISTENSOCK, as it may be needed later after sockport has been again updated.

Since opening the listening socket is fast (does not require waiting for a client to make a connection request), this eliminates the possibility of the server getting stuck for an indefinite time waiting for a client connection. It can subsequently use the TCPOP_CHECK operation to check if a connection is ready, although it should do so frequently, since the clients do not like to wait very long. Once a connection is ready to accept, or if you decide you do not mind waiting, you can use the TCPOP_ACCEPT opcode again, this time with TCPXFLG_ASYNC (and optionally, TCPXFLG_KEEPLISTEN) to accept a connection on the listening socket specified in sockport. The accepted connection socket is returned in sockport. This would overwrite the saved listening socket number, so if you plan to accept further connections on this same listening socket, you must save the listening socket in another variable.

If the server wants to "stay open for business" while processing the current connection, it should use the TCPXFLG_KEEPLISTEN flag so that the listening socket previously opened—and hopefully saved in a separate variable, i.e. LISTENSOCK—can be left open and reused for the next connection.

As long as the listening socket is left open, any connection requests will queue up, either until the queue fills, or the client gets tired of waiting and cancels the request. The length of the queue is 64, which should be more than adequate for most situations.

Note that if you use TCPXFLG_LISTEN, and then do not accept a connection or accept one with the TCPXFLG_KEEPLISTEN flag set, the listening socket remains open, and must be manually closed (in addition to any connection socket) when your program ends. Use the normal TCPOP_CLOSE operation and specify the listening socket, which you hopefully saved in a variable separate from sockport.)