Chapter 8: Client Sockets


Overview

Many developers have avoided network programming using the Windows Sockets (Winsock) API because they think it s too difficult to learn and implement. Often, these developers relied on simplified interfaces such as Microsoft ActiveX network controls, which provide simplicity but often offer poor performance. At some point, the developer usually needs fine control over the communication, which isn t typically exposed through the simple interface. The Winsock API is large and complicated, which is why developers have looked elsewhere. However, the Microsoft Windows .NET Framework offers several classes, such as Socket , TcpListener , TcpClient , and UdpClient , that offer a simplified interface to get up and running with network programming while also exposing more complicated and powerful functionality.

The bulk of this chapter introduces the System.Net.Sockets class, which provides the ability for applications to communicate with one another over different protocols installed on the system. A number of transport protocols are available for Windows, such as IPv4, IPv6, IPX/SPX, and AppleTalk. However, this chapter and the next will focus only on IPv4 and IPv6, and more specifically , the TCP and UDP protocols over IPv4 and IPv6.

We introduce the Socket class first to introduce the concepts and underlying behavior of network programming that are sometimes hidden in the simplified TcpListener , TcpClient , and UdpClient classes. Also, for classes derived from these simplified classes, you can always obtain the underlying Socket object to perform the advanced functionality.

Additionally, our discussion of programming with the Socket class will be divided between the client and server operation. This chapter will cover the basics of the Socket class such as socket creation, setting socket options, establishing a connection, and sending and receiving data. Advanced topics such as IP multicasting and raw sockets will also be introduced. The next chapter will focus on the server-side aspects of socket programming, such as setting up a listening socket, accepting connections, and managing multiple clients . As such, the focus of the discussion in this chapter will be from the client s perspective, but many of the concepts covered apply to server-side programming as well.

Many socket operations can be invoked either synchronously or asynchronously. This chapter will focus mainly on the synchronous method calls to keep the chapter simple. Each section will mention if a particular method also has an asynchronous counterpart using the .NET Framework asynchronous pattern as described in Chapter 3, but code samples will not be given. However, Chapter 9 will fully discuss asynchronous operations because servers typically require asynchronous calls for performance and scalability reasons, although this certainly isn t always the case.




Network Programming for the Microsoft. NET Framework
Network Programming for the MicrosoftВ® .NET Framework (Pro-Developer)
ISBN: 073561959X
EAN: 2147483647
Year: 2003
Pages: 121

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