Common Errors

As you saw in Chapters 6 through 14, an application can encounter quite a few Winsock errors. We won't go into all of them here. However, in the following two sections we will discuss the errors that are most commonly encountered by applications using the Winsock control: "Local address in use" and "Invalid operation at current state."

Local Address In Use

The "Local address in use" error occurs when you bind to a local port, either through the Bind method or the Connect method, but find that the port is already in use. This is most often encountered in the TCP server that always binds to a specific port so clients can locate the service. If a socket is not properly closed before an application using that socket exits, the socket goes into the TIME_WAIT state for a short period of time to ensure that all data has been sent or received on that port. If an attempt is made to bind to that port, the "Local address in use" error is generated. A common mistake on the client side also results in this error. If the LocalPort property is set to 0 and a connection is established, LocalPort is updated to the port number on which the client connection was made locally. If you plan to reuse the same control to make a subsequent connection, be sure you reset the LocalPort value to 0. Otherwise, if the previous connection was not properly shut down, you might run into this error.

Invalid Operation at Current State Run-Time Error

The "Invalid operation at current state" error is probably the most frequently seen error. It occurs when a Winsock control method is called but the current state of the control prohibits that action. Take a look at Figures 15-3 and 15-6 for the state diagrams for UDP and TCP sockets. To write robust code, always check the socket's state before calling a method.

Winsock errors will be generated through the Error event. These are the same errors as the errors from straight Winsock programming. For a more detailed description of Winsock errors, refer to Chapter 7, which covers the most common errors encountered, or consult Appendix C, which lists all possible Winsock error codes.



Network Programming for Microsoft Windows
Linux Server Hacks, Volume Two: Tips & Tools for Connecting, Monitoring, and Troubleshooting
ISBN: 735615799
EAN: 2147483647
Year: 1998
Pages: 159

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net