Flylib.com

Books Software

 
 
 

Cross-Platform GUI Programming with wxWidgets - page 148

team bbl


Summary

Just as giving a cashier two lines to process instead of one won't make her process more customers per hour , multithreading won't make your application go faster (at least without special hardware). However, it can seem faster to the user because the user interface is more responsive , and like a cashier waiting for credit-card clearance on one of the lines while processing the other, multithreading can use available resources more efficiently . It can also be a good way to solve certain problems more elegantly than would be possible if using only a single thread. In this chapter, we've also touched briefly on avoiding multithreading by using timers, idle event processing, and yielding.

There is more to multithreaded programming than this chapter can cover. For further reading, we recommend Programming with POSIX Threads by David R. Butenhof.

Our next chapter looks at using programming with sockets to pass data between processes.

team bbl

team bbl


Chapter 18. Programming with wxSocket

A socket is a conduit for data. A socket doesn't care what kind of data passes through it, where the data is going, or where the data is coming from; its goal is to transport data from point A to point B. Sockets are used every time you surf the web, check your email, or sign on to an instant messenger. One of the neatest aspects of sockets is that they can be used to connect any two devices that support sockets, even if one of them is a computer and the other is a refrigerator!

The socket API was originally a part of the BSD Unix operating system, and because that socket API originated from only one source, it has become the standard. All modern operating systems offer a socket layer, providing the ability to send data over a network (such as the Internet) using common protocols such as TCP or UDP. Using wxWidgets' wxSocket classes, you can reliably communicate any amount of data from one computer to another. This chapter assumes some basic socket terminology knowledge, but socket operations are generally straightforward.

Even though the basic socket features and functions are very similar on Windows, Linux, and Mac OS X, each socket API implementation has its own nuances , usually necessitating platform-specific tweaks. More importantly, event-based sockets have very different APIs from one platform to the next , often making it a significant challenge to use them. wxWidgets provides socket classes that make it easy to use sockets in advanced applications without having to worry about platform-specific implementations or quirks .

Please note that wxWidgets does not, at the time of this writing, support sending and receiving datagrams using the UDP protocol. Future releases of wxWidgets might add UDP capabilities.

team bbl

team bbl


Socket Classes and Functionality Overview

At the core of socket operations is wxSocketBase , which provides the basic socket functionality for sending and receiving data, closing, error reporting, and so on. Establishing a listening socket or connecting to a server requires wxSocketServer or wxSocketClient , respectively. wxSocketEvent is used to notify the application of an event that has occurred on a socket. The abstract class wxSocketBase and its children such as wxIPV4address enable you to specify remote hosts and ports. Lastly, stream classes such as wxSocketInputStream and wxSocketOutputStream can be coupled with other streams to move and transform data over a socket. Streams were discussed in Chapter 14, "Files and Streams."

Sockets in wxWidgets can operate in different ways, as discussed later in the "Socket Flags" section. The traditional threaded socket approach is handled by disabling the socket events and using blocking socket calls. On the other hand, you can enable socket events and eliminate the need for a separate thread; wxWidgets will send an event to your application when processing is required on a socket. By letting the data arrive in the background and processing data only when it is present, you avoid blocking the GUI, and you avoid the complexity of putting each socket in its own thread.

This chapter provides examples of both methods as well as a thorough explanation of the API for wxSocket and related classes. The examples and reference can be read and used independently, although the examples are intended to preface the explanation of the APIs.

team bbl