The Windows CE Winsock Control

The Visual Basic Toolkit for Windows CE (VBCE) contains a Winsock control that provides many of the same capabilities offered by the "regular" Visual Basic Winsock control. The major difference is that while UDP is unsupported, the Windows CE Winsock control offers the IrDA protocol. Also, some minor differences between the two controls require some programmatic changes from what you have seen with the non_Windows CE Winsock control.

As you read in Chapter 7, Windows CE does not offer the asynchronous Winsock model. The Windows CE Winsock control is no exception. The main difference in programming is that the Connect method is blocking. There is no Connect event. Once you attempt a connection by calling Connect, the call will block until a connection is made or an error is returned.

Additionally, the VBCE 1.0 does not support control arrays, which means you will have to change your server design from that presented in Figure 15-5. As a result, the only way to handle multiple connections is to place a number of Windows CE Winsock controls onto the form. Realistically, this limits the maximum number of concurrent client connections that you can handle because this solution does not scale at all.

Finally, the ConnectionRequest event doesn't have a RequestID parameter, which might seem a bit strange. The end result is that you must call the Accept method on the control to which the connection will be handed off. The connection request that triggers the ConnectionRequest event is handled by the control that receives the connection request.

Windows CE Winsock Example

In this section, we'll briefly introduce a sample application using the Windows CE Winsock control. The same principles apply to the Windows CE control as to the desktop Winsock control except for the differences noted above. Figure 15-7 shows the code behind the Windows CE Winsock control.

Figure 15-7. The Windows CE Winsock example

 Option Explicit ' This global variable is used to retain the current value ' of the radio buttons. 0 corresponds to TCP, while 2 means ' IrDA (infrared). Note that UDP is not supported by the ' control currently. Public SocketType Private Sub cmdCloseListen_Click() ' Close the listening socket, and set the other buttons ' back to the start state WinSock1.Close cmdConnect.Enabled = True cmdListen.Enabled = True cmdDisconnect.Enabled = False cmdSendData.Enabled = False cmdCloseListen.Enabled = False End Sub Private Sub cmdConnect_Click() ' Check which type of socket type was chosen, and initiate ' the given connection If SocketType = 0 Then ' Set the protocol and the remote host name and port ' WinSock1.Protocol = 0 WinSock1.RemoteHost = txtServerName.Text WinSock1.RemotePort = CInt(txtPort.Text) WinSock1.LocalPort = 0 WinSock1.Connect ElseIf SocketType = 2 Then ' Set the protocol to IrDA, and set the service name ' WinSock1.Protocol = 2 'WinSock1.LocalPort = 0 'WinSock1.ServiceName = txtServerName.Text WinSock1.RemoteHost = txtServerName.Text WinSock1.Connect End If ' Make sure the connection was successful; if so, ' enable/disable some commands ' MsgBox WinSock1.State If (WinSock1.State = 7) Then cmdConnect.Enabled = False cmdListen.Enabled = False cmdDisconnect.Enabled = True cmdSendData.Enabled = True Else MsgBox "Connect failed" WinSock1.Close End If End Sub Private Sub cmdDisconnect_Click() ' Close the current client connection, and reset the ' buttons to the start state WinSock1.Close cmdConnect.Enabled = True cmdListen.Enabled = True cmdDisconnect.Enabled = False cmdSendData.Enabled = False cmdCloseListen.Enabled = False End Sub Private Sub cmdListen_Click() ' Set the socket to listening mode for the given protocol ' type ' If SocketType = 0 Then WinSock1.Protocol = 0 WinSock1.LocalPort = CInt(txtLocalPort.Text) WinSock1.Listen ElseIf SocketType = 2 Then WinSock1.Protocol = 2 WinSock1.ServiceName = txtServerName.Text WinSock1.Listen End If ' If we're not in listening mode now, something ' went wrong ' If (WinSock1.State = 2) Then cmdConnect.Enabled = False cmdListen.Enabled = False cmdCloseListen.Enabled = True Else MsgBox "Unable to listen!" End If End Sub Private Sub cmdSendData_Click() ' Send the data in the box on the current connection ' WinSock1.SendData txtSendData.Text End Sub Private Sub Form_Load() ' Set the initial values for the buttons, the timer, etc. ' optTCP.Value = True SocketType = 0 Timer1.Interval = 750 Timer1.Enabled = True cmdConnect.Enabled = True cmdListen.Enabled = True cmdDisconnect.Enabled = False cmdSendData.Enabled = False cmdCloseListen.Enabled = False lblLocalIP.Caption = WinSock1.LocalIP End Sub Private Sub optIRDA_Click() ' Set the socket type to IrDA ' optIRDA.Value = True SocketType = 2 End Sub Private Sub optTCP_Click() ' Set the socket type to TCP ' optTCP.Value = True SocketType = 0 cmdConnect.Caption = "Connect" End Sub Private Sub Timer1_Timer() ' This is the event that gets called each time the ' timer expires. Update the socket state label. ' Select Case WinSock1.State Case 0 lblState.Caption = "sckClosed" Case 1 lblState.Caption = "sckOpen" Case 2 lblState.Caption = "sckListening" Case 3 lblState.Caption = "sckConnectionPending" Case 4 lblState.Caption = "sckResolvingHost" Case 5 lblState.Caption = "sckHostResolved" Case 6 lblState.Caption = "sckConnecting" Case 7 lblState.Caption = "sckConnected" Case 8 lblState.Caption = "sckClosing" Case 9 lblState.Caption = "sckError" End Select End Sub Private Sub WinSock1_Close() ' The other side initiated a close, so we'll close our end. ' Reset the buttons to their initial state. ' WinSock1.Close cmdConnect.Enabled = True cmdListen.Enabled = True cmdDisconnect.Enabled = False cmdSendData.Enabled = False cmdCloseListen.Enabled = False End Sub Private Sub WinSock1_ConnectionRequest() ' We got a client connection; accept it on the listening ' socket ' WinSock1.Accept End Sub Private Sub WinSock1_DataArrival(ByVal bytesTotal) ' This is the event for data arrival. Get the data, and ' add it to the list box. ' Dim rdata WinSock1.GetData rdata List1.AddItem rdata End Sub Private Sub WinSock1_Error(ByVal number, ByVal description) ' An error occurred; display the message, and close the socket ' MsgBox description Call WinSock1_Close End Sub 

We won't go into the specifics of the sample code in Figure 15-7 because it is similar to the SockTCP example in Figure 15-5. The only differences between the two are the known limitations mentioned in the previous section. One thing you will notice is that the Windows CE Winsock control is a bare-bones control. That is, it isn't as well polished as the desktop version. The type libraries aren't fully implemented: you must differentiate the protocol type with a simple integer as opposed to an enumerated type. In addition, there is the problem with the socket state enumerated type mentioned in the next section, "Known Problems."

Handling infrared connections is not that different from handling TCP connections. The one exception occurs when a listening socket is established over the infrared port. An infrared server is known by its service name, which is discussed in detail in the IrDA addressing section of Chapter 6. The Windows CE Winsock control has an additional property named ServiceName. You set this property to the text string that clients attempt to connect to. For example, the following code snippet puts the Windows CE Winsock control named CeWinsock into listening mode under the name "MyServer."

 CeWinsock.Protocol = 2 ' Protocol 2 is IrSock CeWinsock.ServiceName = "MyServer" CeWinsock.Listen 

There are no other requirements for publishing a service under infrared sockets. You need to specify only the service name.

Known Problems

The one rather strange problem we encountered with the VBCE Winsock control is the use of the enumerated values for the Winsock states. For some odd reason, these values are defined in the development environment, but on the remote device the following error message pops up every time you reference an sck enumerated value in your code: "An error was encountered while running this program." If you replace the enumerations with their constant equivalents, these errors go away. This has been marked as a bug and will be corrected in a future release of the toolkit.



Network Programming for Microsoft Windows
Linux Server Hacks, Volume Two: Tips & Tools for Connecting, Monitoring, and Troubleshooting
ISBN: 735615799
EAN: 2147483647
Year: 1998
Pages: 159

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