Toby Opferman http://www.opferman.net programming@opferman.net TCP/IP INTRODUCTION This is a basic tutorial of how TCP/IP works and it will get you ready for the next tutorial, SOCKETS programming. The idea of TCP/IP networking from a high level point of view is extremely simple. One computer has runs a server program. This program accepts connections from other computer (Or the same computer) via a client program. Now, how does an outside client program reach the correct server program? Simple. First, you need the computers IP address of course. Then, you need the port number. That is it. The port number can be a range of 0-65535 (Some are reserved for well known protocols, example you can't use 0-1023 for general use). A "port" is just a number. Here is a simple way to think of it: int Hello[65536]; An integer array in C called 'Hello'. It has 65536 elements. If the programmer wants put a number in a certain position in the array, he needs to know 2 things. #1, the name of the variable, "Hello". And, #2, the element number he wants to put the data at. Hello[4] = 10; This is the same with TCP/IP. The client needs to know the IP and needs to know the port number. Very simple. There are 2 different kinds of connections. UDP and TCP. TCP is streaming, meaning there is constant communication from the client - server and if you send data it will get to the server else the connection closes. UDP is not streaming. It is basically broadcast. You do not know if the packet got to the server. This should be used with any program that doesn't care wether the information gets there (Streaming audio for example, if you ever have heard static parts and skipped parts it is because it's UDP. It is faster to send the data than to send with error detection when you need to be fast in real time and the information isn't quite that important that loss isn't a real issue.). If you do care, use TCP or you'll be forced to reinvent it. PROTOCOLS Protocols are nothing more than parcing strings. You give a defintion of your protocol, example: SEND <name> [CNTROL CODE] You may seperate things by spaces and end each transmission with a control code. the first word is the "command" of the protocol. Next comes the data in whatever format is expected, if sending a binary file you will have to send the # of bytes to count since your ending [CNTROL CODE] may be apart of the file and try to end your transmission prematurely. TALK <message> [CNTROL CODE] Like the above, make sure your program does not allow the end control code to be sent. The parcing loop gets the "TALK" command, sends the rest to recieve the message and stops when it hits the [CNTROL CODE]. You can even maybe have a special \ character instead where \\ = \ and \[CNTROL CODE] = data control code and not ending control code. CLIENT/SERVERS There can only be 1 server per port per computer, but there may be any number of clients on any computer even the server itself. It is in your best interest to make the port configurable so it's easier to set up in case something else is using a specific port number. SERVER To start a server, you create a socket and bind a port to it. Then, you just listen for connections from clients. Once a connection is established, you perform your interactions and when you are ready you close the connection. CLIENT To start a client, you make a socket and you connect to an IP and Port number. Then once you establish a conection, you perform your interactions and when you are ready you close the connection. This was a basic overview on TCP/IP, you should be ready now to learn sockets.