From the course: Network Programming in C: Develop Reliable Client/Server Applications

Traps and pitfalls

- [Instructor] Programming can be toilsome and rewarding, which is probably why we keep at it. With network programming, you add into the mix lots of potential for unexpected and unwelcome circumstances. Here's my list of what to unexpect when network programming. The first thing to be aware of is your computer's firewall, it shows a warning when an unknown program attempts network access. When you test your network code, you will see this warning a lot. You must grant access through the firewall for your program to access the network. Some firewalls may not display a warning. In this instance, you must disable the firewall to confirm that it's not the problem and that your code works. Don't forget to re-enable the firewall when you're done testing. It's common to use the localhost to test network programs, this address is 127.0.0.1 in IPv4, or ::1 in IPv6. This port is consistent on all modern networked computers and it's good for testing. But on a computer system with multiple network adapters, using the localhost may cause WOE. If you experience odd behavior when running your network programs, use an assigned IP address instead of localhost. In network programming, the getaddressinfo function is used to configure a socket, it's an important step. Traditionally, the memset function is used to initialize a structure for this function, it zeros out the structure's data which is important. However, the memset function has issues. I use it in this course's sample code where it works okay, but if your compiler has access to the memset_s function, please use it instead. If memset_s isn't available, write a function that manually initializes the structure to zero, especially if you're coding for a program you plan on releasing. Network Programming is socket programming. A socket is like a file descriptor, though it acts as a communications endpoint connecting two systems over a network. Some sockets may remain open for a while after a program quits. If you attempt to run the program again, you may see a socket busy or in use message, just wait 30 or so seconds and the system closes the socket. Speaking of lingering things, if you test run a server and it hangs, use the Control + C keyboard shortcut to terminate it. If the keyboard is unavailable, open another terminal window to kill the server process, closing the server's terminal window might also work. If the port you're using enters a wait state due to the server hanging, you can try to kill the process. If this doesn't work, you might have to restart the system. Other less drastic methods are available to clear a port stuck in a wait state, though these are operating system specific. The code presented in this course is written to teach, so some precautions are tossed to the wind. For example, no validation is done to confirm that the received data is properly formed. Buffer overflow isn't checked. These items must be addressed if you plan on creating your own network code, always validate the incoming data and test for potential overflows. I know that you're excited to start writing network programs in C, but I strongly recommend that you review the terms and functions mentioned in chapters one and two of this course. Network programming can be overwhelming, it involves a lot of steps and a lot of setup, knowing important background information can help ease you into this otherwise challenging process. With this review of the various traps and pitfalls, you're ready to start your C network programming adventure.

Contents