Question

I work in IT and I am learning about network security administration. I want to learn how to use any TCP/IP socket API to design and implement basic client/server applications. Do I need to know an object oriented programming language or are there already a wide variety of pre-made methods/functions for TCP/IP programming?

Was it helpful?

Solution

You need to have a solid understanding of stream IO, much like file IO. Once things are set up, it's effectively exactly like stream IO for many use cases. This includes the concept of stream operations, as well as proper error handling of the system calls.

You will need to understand text or binary protocols, and notably when to STOP reading. For some protocols, the stream remains open from event to event, but if you read beyond the end of a data packet, the other end of the socket may not be sending you anything, and your code will block, and this is likely not what you want. Being able to note when you should stop reading is very important. Some protocols use a marker, other use a length sent previously. This is a common cause of problems for beginning network programmers.

For binary protocols you will need to understand the byte arrangement of numbers, notably integers. Specifically big-endian vs little-endian vs "network order". Will also need to understand how to build larger constructs from bytes (again, notably, integers from bytes). If you do this improperly, your numbers will be quite wrong.

For a client, this is pretty much all you need. Once you create the socket, you will perform stream operations upon the resulting socket, sending and receiving bytes and blocks of information. When you are done, you will close the socket. Unless the protocol specifies, you should not rely on the server to close its socket to indicate to you that it is done. Make sure you clean up your resources on exiting.

If you wish to create a server and process more than one request at a time, you will need knowledge of threading, forking, or asynchronous IO in order to handle the multiple requests simultaneously. After that it becomes much like the client side, save for the initial creation of the listening socket is a little different.

There are certainly nuances and intricacies to network programming, but for the your basic connectivity, much of this can be ignored.

The scripting languages, Java, and .NET make network programming much, much easier than C. C exposes the raw sockets and leaves all of the detail, yet routine, operations to you. You will very likely have much more success starting out with a scripting language. I can not speak to any C or C++ libraries that make network programming easier. I'm sure they exist, I can not cite any.

You will also need patience to deal with the initial frustration, a sense of HURRAH! when it finally works, and a comfy chair to sit back in at the end when you go "Gee, now that wasn't so bad, this is pretty neat" and then peacefully reflect on what's next.

Finally, as I always suggest, do not CUT and PASTE sample code. Whatever you may find, type it in and try to understand each line as you type it in. You will have a much better grasp of how this works than if you just download something and watch it run.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top