Building a Server
You will now build a Windows application that acts as the server and receives delivery information from clients. Using Visual Studio 2005, create a new Windows application project and name it SyncServer.
Your server will display the signature of the recipient for each delivery, thus you will reuse the code for the signature control developed earlier.
Add the Signature.bmp image to the project by right-clicking on the project name in Solution Explorer and then selecting Add | Existing Item…. After the image is added to the project, select it in Solution Explorer and then change the value of the "Copy to Output Directory" property to "Copy if newer."
Add a new Class file to the project (right-click on project name in Solution Explorer and select Add | New Item…). Name the class file Signature.vb. This is the signature control that you will use on the server side. Populate it as shown in Listing 1.
Note that because the signature control used on the server side is used for displaying signatures (and not capturing them), you can omit the three main eventsOnMouseDown, OnMouseMove, and OnMouseUp.
Next add a new Class file to the project (right-click on project name in Solution Explorer and select Add | New Item…) for receiving connections from clients. Name the class file as WMClient.vb. Populate it as shown in Listing 2.
Back in Form1, populate it with the following controls (see also Figure 8):

Figure 8. Populate Form1 (server) with the Label and ListBox controls as shown.
|
|

Figure 9. Testing the application. |
For simplicity, all the delivery information sent by the clients will be added to the ListBox control. In the real world, you should persist all delivery information to a database.
Switch to the code-behind of Form1 and import the following namespaces:
Imports System.Net.Sockets
Imports System.Threading
Declare the following member variables:
Public Class Form1
Dim WithEvents user As WMClient
Dim sig As New Signature
'---storing signature for each user---
Private Signatures As New ArrayList
Define the
ListenForConnections() subroutine to listen for incoming connections from clients:
Private Sub ListenForConnections()
Const portNo As Integer = 3456
Dim localAdd As System.Net.IPAddress = _
System.Net.IPAddress.Parse("10.0.1.2")
Dim listener As New TcpListener(localAdd, portNo)
listener.Start()
While True
user = New WMClient(listener.AcceptTcpClient)
End While
End Sub
Author's Note: Substitute "10.0.1.2" with the IP address of your own server. |
In the Form1_Load event, spin off a thread to listen for incoming connections and add a new signature control to the form:
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'---listen for incoming connections---
Dim t1 As New Thread(AddressOf ListenForConnections)
t1.Start()
'---add a signature control---
With sig
.Location = New Point(294, 25)
.Size = New Size(233, 73)
.InitSignature()
End With
Me.Controls.Add(sig)
Me.Refresh()
End Sub
Define the
InformationReceived() subroutine to handle the
DataReceived event defined in the WMClient class. The
DataReceived event is fired whenever the server receives the data sent by the client. Here, you will update the information on the ListBox control as well as display the signature on the signature control. All of these tasks are done via a delegate:
Private Sub InformationReceived( _
ByVal ID As Integer, _
ByVal OrderID As String, _
ByVal DateTime As String, _
ByVal signature As String) Handles user.DataReceived
'---display the received information---
Me.BeginInvoke(New _
mydelegate(AddressOf DisplayInformation), _
New Object() {ID, OrderID, DateTime, signature})
End Sub
Private Delegate Sub mydelegate( _
ByVal ID As Integer, _
ByVal OrderID As String, _
ByVal DateTime As String, _
ByVal signature As String)
The
DisplayInformation() subroutine adds the delivery information to the ListBox control and then displays the signature received. Note that the signature for each item in the ListBox control is saved in an ArrayList object.
Private Sub DisplayInformation( _
ByVal ID As Integer, _
ByVal OrderID As String, _
ByVal DateTime As String, _
ByVal signature As String)
Dim str As String = ID & vbTab & _
OrderID & vbTab & _
DateTime
ListBox1.Items.Add(str)
Signatures.Add(signature)
With sig
.InitSignature()
.Signature = signature
End With
End Sub
Finally, service the SelectedIndexChanged event for the ListBox control so that when each item in the ListBox control is clicked, the signature for each delivery will be shown:
Private Sub ListBox1_SelectedIndexChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedIndex < 0 Then Exit Sub
sig.InitSignature()
sig.Signature = Signatures.Item(ListBox1.SelectedIndex)
End Sub
That's it! You can now press F5 to test the server. On the Pocket PC, select an order and then sign on the signature control (see
Figure 9). Click on the Send menu item to send the delivery information as well as the signature to the server.