Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Sapphire Soltuions

Writing a 2 player Tic-Tac-Toe game in eVB for your Pocket PC

Written by Pete Vickers  [author's bio]  [read 60104 times]
Edited by Derek

Download the code

Page 1  Page 2  Page 3  Page 4 

When we get a connection we send back to the client our name. Every packet we send will be prefixed by 'OX' and terminated with the '|' character. This will be explained shortly.

The code for our client requesting a connection is:-

Private Sub cmdConnect_Click()
WinSock1.RemoteHost = txtServer.Text
If IRDA Then
WinSock1.Protocol = sckIRDAProtocol
WinSock1.RemotePort = 0
WinSock1.ServiceName = "7890"
Else
WinSock1.Protocol = sckTCPProtocol
WinSock1.RemotePort = 7890
WinSock1.ServiceName = ""
End If
lstStatus.AddItem _
"Conecting to " & txtServer.Text & "..."
Frame1.Visible = False
On Error Resume Next
WinSock1.Connect
End Sub

This illustrates the differences between IRDA socket connections, and TCP/IP socket connections. In TCP/IP, we connect by setting the remoteport property to our chosen port of 7890. In IRDA, we set the servicename property to the port number. This is where some confusion exists, and is not explained well in the eVB help file. The winsock1.connect statements attempts to make the connection. I use 'On Error Resume Next' so that I can use the 'error' event of the Winsock control.

Private Sub WinSock1_Error_
(ByVal number As Long, _
ByVal description As String)
On Error GoTo 0
MsgBox "Error in Winsock " _&
&number & ":" & description, _&
vbInformation, "Error in Winsock"
Err.Clear
End Sub

So now, we hopefully have the 2 devices talking. As mentioned earlier, when the server gets a connection, it sends its name to the client. This is handled in the Dataarival event, and is where most of the work in the program is done. There are a number of 'features' in the Winsock control that make it tricky to work with. If you send 20 bytes from the client, there is no guarantee that the server will receive those 20 bytes all at once. In fact, with the Winsock control in eVB, this is highly unlikely. Because of this we send an end of data character. When the client connects to the server, the server will send back to the client "OXNAMEPete|". In the dataarrival code, we deal with this by only processing data when we get a | character in the string. It may not be pretty, but it works!

Private Sub WinSock1_DataArrival _
(ByVal bytesTotal As Long)
Dim NewData As String
Dim Eod As Integer
WinSock1.GetData NewData
Eod = InStr(NewData, "|")
If Eod > 0 Then
Wdata = Wdata & Mid(NewData, 1, Eod - 1)
Wdata = Replace(Wdata, "|", "")
Process_Data
Wdata = Mid(NewData, Eod + 1, 200)
Eod = InStr(Wdata, "|")
If Eod > 0 Then
Wdata = Replace(Wdata, "|", "")
Process_Data
End If
Exit Sub
Else
Wdata = Wdata + NewData
End If
Wdata = Replace(Wdata, "|", "")
End Sub

When we have a string we are happy with, we call the Process_Data procedure. When we get OXNAME we simply extract the name, and put it in the screen caption.

If Mid(Wdata, 1, 6) = "OXNAME" Then
opponentName = Right(Wdata, Len(Wdata) - 6)
Me.Caption = myName & " v " & opponentName
End If

Previous Page  Next Page