Wednesday, November 25, 2009

8.6 Connecting with UNIX and Other TCP/IP Machines



< BACK  NEXT >

[oR]


8.6
Connecting with UNIX and Other TCP/IP Machines


In the previous sections, you learned about network communications using Windows' native communication protocol. Mailslots and named pipes work well in cases where you want to communicate with other Windows machines on a homogeneous Windows network.



Windows also supports the TCP/IP protocol. This is the protocol used by UNIX machines and the Internet. If you load the TCP/IP drivers in the Network applet of the Control Panel, a number of TCP/IP commands (ftp, telnet, finger, etc.) become available on the command line. You can also create code that can read and write packets on the network using the TCP/IP protocol. There are at least three reasons for creating code using this protocol:




  1. You want to write code that connects to an existing network service on a UNIX machine.




  2. You want to create a network service (see Chapter 12) on a Windows 2000 machine that UNIX machines will be able to access.




  3. You want to create programs that allow widely-separated Windows machines on the Internet to communicate with one another, and you want to make sure the packets will route correctly on the Internet.





For example, if you want to create a Finger service that allows UNIX users to finger your Windows 2000 machine, you must use the TCP/IP communications functions when you write the code.



Windows supplies a complete library of functions for creating TCP/IP code. The library is called the Windows Sockets API, or "winsock." All winsock functions are documented in the Win32 help file. UNIX programmers will find that the functions are very similar to the functions they are accustomed to for writing socket code on UNIX machines. There is also another API, called the Microsoft Windows Sockets Asynchronous Extensions, that supplies TCP/IP support in a cooperative multi-tasking (non-threaded) environment such as Windows 3.1. These functions work in a manner similar to overlapped I/O. Because Windows 2000 and Windows 98 are multi-threaded, the extension functions are not necessarily needed here, but they can be useful in some situations.



There are several vocabulary words and concepts that you need to understand in order to use the TCP/IP functions:




  1. Any machine, whether on the world-wide Internet or on an isolated TCP/IP network, has both an

    IP address and a fully-qualified domain name.

    An IP address is of the form 152.1.45.17, while a name has the form server.iftech.com. Both are unique to a particular machine network-wide. In your code you use IP addresses to connect to a machine, but there are functions that let you convert names to addresses. A typical small TCP/IP network consisting of several Windows machines and a UNIX machine is shown in Figure 8.5.




  2. The Berkeley flavor of UNIX popularized the concept of a

    socket. In a sense, a socket is a bi-directional communication device. In a sense a socket is similar to a file handle or a pipe. When you open a file handle to an existing file, you create an entity that you can read from and write to. When you create a socket, you can also read from it and write to it, but it is associated with the network rather than with the file system.




  3. You can do one of two things with a socket: You can bind it to a

    port on the local machine, or, in TCP mode, you can connect it to a port on another machine. Ports are referred to by positive integer numbers. When you bind a socket to a port, you are claiming a port with a specific number on the local machine. Programs on other machines can connect to that port. Figure 8.6 shows a simplified representation of the different pieces.





    Figure 8.6. TCP connections using ports and sockets









  4. A port, when used in TCP mode, has the ability to

    accept connections. First you give it the ability to

    listen, and then you let it start accepting connections. Allowing a port to listen enables the port. Other machines can now start connecting to the port. Each time a program accepts a connection, data can start to flow across the network. A port has a queue that holds connections waiting to be accepted. You define the maximum size of the queue at the time you tell the port to start listening.





TCP/IP networks support both mailslot-like (unreliable) communications with the potential for broadcasting and point-to-point connections used in client/server architectures.
Point-to-point connections, like named pipes, have guaranteed delivery, error correction, and sequencing. Point-to-point connections are referred to as TCP connections. UDP connections, on the other hand, are unreliable but permit broadcasting. TCP connections are far more common in the UNIX world than UDP connections are.



To be a server using the TCP protocol, a program must create a socket, bind it to a port, let the port begin listening, and then accept connections. Each time it accepts a connection, the server can communicate over the new connection with a different client. A client must create a socket and connect it to a port on another machine. Once it has a connection, it can communicate. See Figure 8.7.





Figure 8.7. The steps involved in connecting a TCP client to a RCP server








TCP/IP networks support three different types of network packets.




  1. IP packets are raw packets and are rarely seen. Raw packets are directed to a specific machine or are broadcast, but they bypass the port system and therefore must be handled as generic packets. You will never use IP packets yourself.




  2. UDP packets are very similar in concept to mailslot packets. They can be either directed to a port on a machine or broadcast, just like a mailslot. UDP packets are always sent to a specific port number. UDP packets are not very common in the UNIX world because they cannot travel farther than the local network segment and because their use tends to lead to large traffic loads on that segment.




  3. TCP packets are similar in concept to named-pipe packets, and are used for point-to-point or client/server communications. They are sent to a specific port on a specific machine. TCP communications are properly-sequenced, reliable, and error corrected, just like named pipes. Almost all UNIX network communications use TCP packets.





One of the most significant advantages of TCP/IP is its routability. A
router is a device that sits between two separate network segments, as shown in Figure 8.8. It examines the TCP/IP header in each packet and decides whether or not a packet on one of the segments should be copied to the other segment. In the diagram below, an administrator has created two separate subnets (128.57.49.xxx and 128.57.50.xxx) and would configure the router to recognize and route packets appropriately. If a machine on network segment one sends a packet to another machine on network segment one, the router ignores it because both machines have the same subnet number. On the other hand, the router would detect a packet on network segment one intended for network segment two because of the difference in the subnet number. It would echo the packet onto network segment two so that the appropriate machine would receive it.





Figure 8.8. A router routing packets between two net-work segments








The following sections demonstrate how to write both UDP and TCP code.





< BACK  NEXT >


No comments: