Welcome
Hey guys, in this tutorial I'll be teaching u how to make a Chat program with a Server-Client Architecture, At first, u need to download the "WickedNetworking.dll" file from HERE and add it to the References folder i ur project.
Once ur done, I must know that this WickedNetworking is a simplified version of Winsock, WickedNetworking uses TCP as a protocol.
Now for the fun!! Before anything type:
using System.Net.Sockets;
Then to make a server u must create a class to receive the events that gets called on the Server,
this class must be public, and must have 3 public functions, the first must be named "clientForceDisconnect" and have only one parameter with type "Socket", this function gets called when a client gets disconnected from the server without asking to, like this:
public void clientForceDisconnect(Socket mySocketName) { //A client left without telling me!! }
Next function gets called when a client connects, this function must have a name of "clientConnect" and have only one parameter which is of type "Socket", like this:
public void clientConnect(Socket socket) { //A client just connected... }
The third and last and the most important function gets called when the server receives a packet, a packet is an object containing some text, this function must have only two parameters, the first must be with type string, and the second must be with type Socket, this function must have a name of "recvPacket" like this:
public void recvPacket(string text, Socket socket) { //just received some text }
Now the class should be done and ready to use, it should be like this:
public class myEventsClass { public ServerEvents() { } public void clientForceDisconnect(Socket socket) { //A client left without telling me!! } public void clientConnect(Socket socket) { //A client just connected... } public void recvPacket(string text, Socket socket) { //just received some text } }
Now, lets go to the code of our Server, before everything we wanna import this too:
using System.Net.Sockets;
Then create an object of the class we just created to receive the events:
myEventsClass myEvents = new MyEventsClass();
Then construct a WickedServerTCP object:
WickedServerTCP serverTCP = new WickedServerTCP(2040, 12345, 5, myEvents);
The first parameter specifies the size of the buffer thais used to send data, I usually set that to 2040, the second parameter is the port number that the server will be working on, the port number specified on the client must be the same that's specified on the server, port numbers range from 0 to 65536, but only port numbers 0 to 1024 are reserved for privileged services, so use a number between 1024 and 65536, the next parameter is the backlog, the backlog is how much packets the server can receive at one frame, e.g. if the backlog is set to 1 and 2 packets are sent to the server at the same time, only one will be received, otherwise if the backlog is set to 5 and 5 packets is sent to the server at the same time it'll receive the 5, but not more than five at the same frame etc. ,the last parameter should be the object that we created to get the events.
When ending the program, we should call the function serverTCP.KILLServer()
to close the server, as long as the server program is still open, it'll call any event that happens as a function in our events object we just created, for a console application we will call Console.Readline ()
to prevent the program from closing, that should be the code for now:
class Server { static void Main(string[] args) { ServerEvents events = new ServerEvents(); WickedServerTCP serverTCP = new WickedServerTCP(2040, 12345, 5, events); Console.WriteLine("Server Setup Done!"); Console.ReadLine(); serverTCP.KILLServer(); } } public class myEventsClass { public ServerEvents() { } public void clientForceDisconnect(Socket socket) { //A client left without telling me!! } public void clientConnect(Socket socket) { //A client just connected... } public void recvPacket(string text, Socket socket) { //just received some text } }
As for a Console program, we'll use Console.WriteLine ()
to post on the console every event, this should be the code now:
class Server { static void Main(string[] args) { ServerEvents events = new ServerEvents(); WickedServerTCP serverTCP = new WickedServerTCP(2040, 12345, 5, events); Console.WriteLine("Server Setup Done!"); Console.ReadLine(); serverTCP.KILLServer(); } } public class myEventsClass { public ServerEvents() { } public void clientForceDisconnect(Socket socket) { //A client left without telling me!! Console.WriteLine("Client Forcefully disconnected"); } public void clientConnect(Socket socket) { //A client just connected... Console.WriteLine("Client connected"); } public void recvPacket(string text, Socket socket) { //just received some text Console.WriteLine(text); } }
As for a chat program, we want the server to resend every packet it receives to all clients that's not the one it received from, because it's already typed in the sender's console, we need another WickedServerTCP
object on the myEventsClass
class, so we made a public WickedServerTCP
object on that class ,and we set it's value to the value of "serverTCP" object after initialising it, we use serverTCP.getClients()
, we want the clients to be able to leave peacefully too, we're going to use the "#Exit#" keyword to make the client exit, so if the client sends "#Exit#" the server will disconnect it by calling serverTCP.closeClientSocket (socket)
, the code should look like this though:
class Server { static void Main(string[] args) { ServerEvents events = new ServerEvents(); WickedServerTCP serverTCP = new WickedServerTCP(2040, 12345, 5, events); events.serverTCP = serverTCP; Console.WriteLine("Server Setup Done!"); Console.ReadLine(); serverTCP.KILLServer(); } } public class myEventsClass { public WickedServerTCP serverTCP; public ServerEvents() { } public void clientForceDisconnect(Socket socket) { //A client left without telling me!! Console.WriteLine("Client Forcefully disconnected"); } public void clientConnect(Socket socket) { //A client just connected... Console.WriteLine("Client connected"); } public void recvPacket(string text, Socket socket) { //just received some text Console.WriteLine(text); if (text != "#Exit#") { foreach (Socket S in serverTCP.getClients()) { if (S != socket) { serverTCP.SendString(text, S); } } } else { serverTCP.closeClientSocket (socket); } } }
Now we have the server done!!! for the client we'll make another public class, this class must have a public function named "recvPacket", and having only one parameter with type string, this is called when the client receives a packet from the server, as for a chat program and a console application, we'll use Console.WriteLine(text)
to print any received messages, that class should look like this then:
public class myClientEvents { public void recvPacket(string text) { Console.WriteLine(text); } }
At the beggining of the program we want to create a "myClientEvents" object to detect events, then make and construct a "WickedClientTCP" object, the first parameter is the ipAddress that I should connect to, this should be set to loopBack as for testing purposes, as LoopBack works in the localhost, when you're ready to go live, the server ip must be the public IP of the computer, add your IP as a parameter by using new IPAddress("192.168.1.1")
and replacing "192.168.1.1" with your public IP, know your public IP from HERE, the second parameter is the port number, as I said before the port specifies the program on your computer, this has to be the same as in the client, to go live you have to do port forwarding, learn how to port forward from HERE, the third and last parameter is the events object we just created to detect events, that's what it's going to look like:
ClientEvents events = new ClientEvents(); WickedClientTCP clientTCP = new WickedClientTCP (IPAddress.Loopback, 12345, events);
The overall code should look like this:
class Client { static void Main(string[] args) { ClientEvents events = new ClientEvents(); WickedClientTCP clientTCP = new WickedClientTCP (IPAddress.Loopback, 12345, events); } } public class ClientEvents { public void recvPacket(string text) { Console.WriteLine(text); } }
Then we want the client to be able to send messages, we'll do this by making a boolean and set it to false, and make a while loop that loops if this boolean is set to false, in the loop we'll use Console.Readline()
to detect the string typed by the user and send it to the server, if this string is set to "#Exit#" the boolean will be set true and the loop will exit, because the server will disconnect the client, after the loop has ended we call clientTCP.stopListen()
to stop listening for packets, the overall code should look like that:
class Client { static void Main(string[] args) { ClientEvents events = new ClientEvents(); WickedClientTCP clientTCP = new WickedClientTCP (IPAddress.Loopback, 12345, events); Console.WriteLine("Enter your message, enter '#Exit#' to exit:"); bool exit = false; while (exit == false) { string mes = Console.ReadLine(); if (mes == "#Exit#") { exit = true; } clientTCP.SendString(mes); } clientTCP.stopListen(); } } public class ClientEvents { public void recvPacket(string text) { Console.WriteLine(text); } }
This way we finished the chat client and the chat server, you can download the source code of this program from HERE and build it, thanks for your time!!
SHARE if you like it!!