Sending Game Data
The lines of communication are open, so all that remains is to send and receive data back and forth. You can send the data a number of ways. For example, you could send two integers that represent the x, y coordinate of the piece to which the player is going to move. Although this might work fine for this game, you want to implement a solution that you can easily extend to a more complex game. You’ll find that it isn’t too hard to send entire class instances over the wire from one program to the other.To do this, you first have to serialize the class instance into some type of format that can easily be sent through the TCPClient object. Visual Basic .NET makes it pretty easy to use XML as that format, as shown in Listing 7-9.Listing 7.9: The SendMyTurnToOpponent Method, Found in the NetworkReversiPlayer Class
Public Sub SendMyTurnToOpponent(ByVal aP As ReversiPiece)
Dim oSer As New XmlSerializer(aP.GetType)
Dim oSW As New StringWriter
Dim oWriter As New XmlTextWriter(oSW)
Dim oByte() As Byte
Dim cSend As String
oSer.Serialize(oSW, aP)
oWriter.Close()
cSend = oSW.ToString
cSend = cSend.Replace(Chr(10), ")
cSend = cSend.Replace(Chr(13), ")
cSend &= Microsoft.VisualBasic.vbCrLf
Try
oByte = System.Text.Encoding.ASCII.GetBytes(cSend.ToCharArray())
FStream.Write(oByte, 0, oByte.Length)
Catch oEX As SocketException
MsgBox(oEX.Message)
End Try
End Sub
The SendMyTurnToOpponent method takes a ReversiPiece object as a parameter, converts it to XML format, and then loads that XML into a string variable named cSend. Listing 7-10 shows the contents of this variable.Listing 7.10: XML-Serialized Version of a ReversiPiece Object
<?xml version="1.0" encoding="utf-16"?>
<ReversiPiece xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Location>
<X>192</X>
<Y>240</Y>
</Location>
<Size>
<Width>48</Width>
<H eight>48</Height>
</Size>
<Value />
</ReversiPiece>
As you can see, all of the public properties of the ReversiPiece class and their values are represented as elements in the XML file.One additional problem exists when sending this XML data across to the other computer. The easiest way to send it is all on a single line. This allows the TCPClient class in the receiving program to use the ReadLine method, which retrieves one whole line of text. The XML created by Listing 7-9, however, is broken up into several individual lines. No big deal, though—it isn’t too difficult to remove the carriage returns and line feeds from the XML string and then add one final carriage return/line feed to the end:
cSend = cSend.Replace(Chr(10), ")
cSend = cSend.Replace(Chr(13), ")
cSend &= Microsoft.VisualBasic.vbCrLf
Now the cSend variable contains one XML line that represents one class, and this data can travel over the wire. You do this the same way that the player name traveled over the wire when the communication was established—you encode the string variable to a byte array and then write it to the TCPClient variable FStream:
oByte = System.Text.Encoding.ASCII.GetBytes(cSend.ToCharArray())
FStream.Write(oByte, 0, oByte.Length)