Methods

Methods

The Winsock control has only a handful of methods. With a couple of exceptions, most of the method names mirror their Winsock equivalents. The method to read pending data is named GetData. Usually, you would call the GetData method once the DataArrival event is triggered, notifying you that data has arrived. The method for sending data is named SendData. In addition, a method named PeekData is similar to calling the Winsock recv function with the MSG_PEEK option. As always, message peeking is evil and should be avoided at all costs. Table 14-3 lists the available methods with their parameters. The methods will be discussed in more detail in the client and server example sections later in this chapter.

Table 14-3 Winsock Control Methods

Method

Parameters

Return Value

Description

Accept

RequestID

Void

For TCP connections only. Use this method to accept incoming connections when handling a ConnectionRequest event.

Bind

LocalPort

LocalIP

Void

Binds the socket to the given local port and IP. Use Bind if you have multiple network adapters. Bind must be called before Listen.

Close

None

Void

Closes the connection or the listening socket.

Connect

RemoteHost

RemotePort

Void

Establishes a TCP connection to the given RemoteHost on the given RemotePort number.

GetData

Data

Type

MaxLen

Void

Retrieves the current data pending. The Type and MaxLen parameters are optional. The Type parameter defines the type of data to be read. The MaxLen parameter specifies how many bytes or characters to retrieve. GetData ignores the MaxLen parameter for types other than byte array and string.

Listen

None

Void

Creates a socket and places it in listen mode. Listen is used for TCP connections only.

PeekData

Data

Type

MaxLen

Void

Behaves exactly like GetData except that the data is not removed from the system's buffer.

SendData

Data

Void

Sends data to the remote computer. If a UNICODE string is passed, it will be converted to an ANSI string first. Always use a byte array for binary data.



Network Programming for Microsoft Windows
Network Programming for Microsoft Windows (Microsoft Professional Series)
ISBN: 0735605602
EAN: 2147483647
Year: 2001
Pages: 172
Authors: Anthony Jones

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