Member Avatar for MonsieurPointer
MonsieurPointer

Hi DaniWeb members,

I am currently designing a multiplayer Tic-Tac-Toe game. As part of a learning process, I am using asynchronous sockets to prevent the GUI from locking up. The asnych sockets work great and through various examples I managed to get it working as required.

Throughout coding the network part of the game, I decided to make the communication module generic. Currently, the communication module is in a class library which is completely separated from the GUI logic. Events are used to notify the GUI module if:

  • A client has connected
  • Data has been sent
  • Data has been received
  • Waiting on connection

Also, the client/server send method only supports sending strings as data. (The string is converted to a byte array, then that byte array is sent, as async methods can only send bytes.) To ensure that all the data has been received, I first send the length of the data, then the actual data. The purpose of this is to determine if all the data has been received in one go, or if it has been split up because the length of the data exceeds the size of the buffer I have specified.

Now that I've explained what I have so far, I would like to hear from all of you how I can manage to re-design my communication module to:

  • Send any type of data (e.g. strings, files, ...)
  • Parse the received data

I fully understand the concept of interfaces, but I am having difficulty deciding what is the best way to accomplish my goal of creating a flexible protocol.

This is what I thought of so far:

  • In the send method of the communication module, call an interface method which retrieves the data to send as a byte array. The interface method should have a byref parameter to a byte array, which is filled by the class implementing the interface.
  • In the receive method of the communication module, call an interface method which will decide if all the data has been received or not, then perform the required action when all the data has been received. The interface method will have two parameters, an integer which indicates the number of bytes received, then an array of bytes containing the data received.

Does this seem like a viable solution? Do you have more experience in this matter? I am open to all suggestions!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.