Working with Sockets


Sockets are the de facto standard for communicating with other computers on both local area networks (LANs) and wide-area networks (WANs), such as the Internet. Two computers communicate to each other by using sockets that follow a general protocol where one computer is expecting to receive a connection, and the other makes the initial connection.

  • The computer expecting to receive a connection, the host or server , listens for incoming connections on a specific port . The computer has a unique IP address , such as 172.68.112.34, and thousands of port numbers available, making it possible for many programs to listen for connections, each using a different port.

  • The computer that makes the initial connection, the client , determines the IP address of the computer expecting the connection. The easiest way to find the IP address is to know it ahead of time. Alternately, if the computer making the initial connection knows the name of the computer, such as www.mycomputer.org , it can use DNS looku p to determine the IP address associated with the name.

  • The client decides what port to connect to on the host. Usually, this is decided ahead of time. For example, Web servers always listen on port 80, so a computer that wants to connect to another computer's Web server process always knows that it needs to connect on port 80. A custom application usually uses a very large port number that is unlikely to be used by anyone else, such as 10998. The range of port numbers that custom applications can use varies by the operating system. Some operating systems reserve special port numbers, especially those below 1024. It is almost always safe to choose a port number between 2000 and 60000.

  • The client connects to the IP address and port number. The host receives the connection. There is now a socket connection between the two computers.

  • The client and host send data packets back and forth. Then one of the parties closes the connection.

The process of making socket connections is special because it is operating system agnostic ”any two computers with socket support can communicate with each other. The pitfall is that the programming interfaces for socket programming are unique to each platform. The goal of this chapter is to learn how to manipulate socket connections in managed programs under the .NET Compact Framework.

Choosing a Protocol: TCP/IP versus UDP

In general, socket programming uses the Internet Protocol interface to send packets between two computers. There are two kinds of packets in common use for sending data through the Internet Protocol:

TCP packets This kind of packet is by far the most commonly used on the Internet ”so commonly used, the protocol of using TCP packets on the Internet Protocol is called TCP/IP networking . The most important feature of a TCP packet is a guaranteed error-free delivery. If a computer sends a TCP packet over a socket connection, the data in the packet is guaranteed to arrive at the destination without errors. If the packet arrives but errors are detected , then the data is re-sent. If the packet doesn't arrive within a timeout period, then the function call used to send the packet reports an error. The way to check for errors varies by the platform, but we will examine this process in detail for the .NET Compact Framework.

UDP packets These packets differ from TCP packets because it is not guaranteed that a UDP packet will ever arrive at its destination or that its data will be error free. However, the lack of error checking means that using UDP packets incurs less overhead, so programs can transmit data more quickly. A good application for UDP packets is Internet telephony. If a packet gets lost or mangled, it means that there might be a crackle in the phone conversation. Conversations are in real time, so there is no use in correcting bad data, because it is too late by the time the errors are detected. You might as well just get the better throughput available with UDP packets.

Dealing with UDP packets, considered an advanced topic, is treated in the section "Using UDP Packets." Because it is the overwhelmingly more common scenario, the rest of the nuts-and-bolts discussion about socket programming in this chapter assumes the use of TCP packets.

Understanding IP Implementations: IPv4 versus IPv6

The process of a client's connecting to a host involves determining the IP address of the host and then making contact. The intricacies of dealing with addresses and forwarding Internet traffic to the correct address is one of the fundamental responsibilities of the Internet Protocol. This protocol has undergone a variety of revisions over the years . Version 4 of the Internet Protocol, IPv4, is the most commonly used on the Internet. An IPv4 address is composed of four 8-bit numbers. In human-readable form, the address looks like four numbers between 0 and 255 separated by dots, such as 172.68.112.34 .

In today's connected world, IPv4 does not provide enough addresses to give a unique address to each computer. There are tricks in widespread use to recycle IPv4 addresses so that they can be applied to more than one computer; the details of how this works is beyond the scope of this book, though.

The most recent version of the IP protocol is version 6, commonly referred to as IPv6. It is not yet in widespread use. IPv6 includes enhancements in security and addressing such that there will be enough unique IP addresses for the foreseeable future.

The .NET Compact Framework contains deep support for the older, far more common IPv4 protocol. It does not support the IPv6 protocol. Thus, this chapter relates all discussion to the IPv4 protocol.



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

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