Sunday, October 17, 2010

C# \ VB .net Multi-user communication library (TCP)

Introduction
The NetComm library allows you to connect multiple clients to the same server. This allows you to do the following:
- Transfer personal messages to each other
- Send a message to all of the users in the same server (public messages)
- Communicate with each client using only ID (a unique string), without the need of knowing each other IP, the clients only need to know the host IP address.
The NetComm library handles all of the complicated work, it works in a separate thread from your application UI - the NetComm library will raise an event when a new message arrived.
Why is this useful? Well, this allows you to create a multi-user chat easily - or any other kind of application that needs to handle multiple users communication.
With the use of this library you can create this kind of complicated communication easily.
This article is written with C# but I attached a VB .net example to VB users (my favorite language), this library was built in VB .net.
Background
I developed this library for my school project - I created a server that keeps track of 2 robots which are being remotely controlled by 2 remote PC's. This project simulates a robot-battle between 2 teams.
I connected the clients (4 clients) to the same server. The server handled all the game rules, each robot life points, bullets, starting ammo, weapons. The main role of the server is making both of the teams follow the same rules. The server is a place where game calculations were taken. It's pretty much the "center" of the game, viewers could look at the server UI and see the scoreboard, each team's robot life points, bullets remaining and more... The NetComm library made it a lot more easier for both of the teams to communicate.
The Concept (or, how it works? for more advanced users)
I built this library with VB .net. I used the TCPClient and TCPListener (System.Net.Sockets) which Microsoft created as a base for this library. The whole library relies on these 2 classes.
Read more: Codeproject