Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

C#| how to make a tcp or udp sockets for Mmo or any other use :)


showloco
 Share

Recommended Posts

So this is a tutorial for the basic making of mmo. Im using an OCX file that's will make it easier for you. and yes, you can use the based Tcp and udp that come with your c#. But this OCX it's a little more user friendly.
First, downlaod the OCX:
http://www.mediafire.com/?zg0nnonokf2
Now open a new project which will be our server.
right click on toolbox and then click on "Choose items"
![](http://gyazo.com/c57a1c0ceeae638377e0efebcebe6935.png)
now go to COM COMPONETS
![](http://gyazo.com/53a08e2721d03dd0ce73d23a244c4921.png)
click on Browse and add the OCX.
Click ok.
Now in the toolbox you have a new object called: "Winsock". drag it to your form.
Do the same thing on the new project and call it "Client".
Now go back to the server, click on Winsock then Properties.
![](http://up352.siz.co.il/up1/n2q1nixggnum.png)
Ok, now main things we have it's the IP, Port and Protocol..
You can also check your ip in the following link: http://www.whatismyip.com
Port- choose any port you want, for this tutorial ill use 8080.
and you have Protocol- you can use TCP or UDP.
After this step, create a new label and name it Status.
Double click on the From,
```
  private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                axWinsock1.LocalPort = 8080; // your port
                axWinsock1.Listen(); // we will start to listen, to use this port.
                status.Text = "Online"; // if we sucessed to start lisenning from this port, so we will print "Online" on the status label.
            }
            catch
            {
                status.Text = "Offline"; // if not, it will print Offline (Usually will happened if the port is used, just try another port.
            }
//Code by ShowLoco
        }
```Now we have an open server.
we will add a new Listbox and named it "IDS",
in the Listbox we will print any username that is online.
Now go to Winsock Properties and ConnectionRequest
![](http://gyazo.com/a1724016c00d8d1d2dbde348f2971c06.png)
```
private void axWinsock1_ConnectionRequest(object sender,  AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent e)
        {
      //Code by ShowLoco
      axWinsock1.Accept(e.requestID); // Accept the ID request to join.
      IDS.Items.Add(e.requestID); /Add the id to our list

        }
```Now we will go to the Client.
We will make a button and name it: Connect and a Textbox named message.
Double click on the Connect Button
```
private void connect_Click(object sender, EventArgs e)
        {
            //Code by: ShowLoco
            axWinsock1.Connect("IP", "PORT");//On the ip, write your IP. On the port, write your port :)
            axWinsock1.SendData(message.Text); // here we will send our message from message textbox :)
        }
```
Go back to the server and create a new textbox and name it: "ARRIVE".
Go again to Winsock Properties and go to "DataArrival".
```
private void axWinsock1_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
        //Code by: ShowLoco
          axWinsock1.GetData(message.Text); // here we going to take any message that any client has been sended and we will save it on message textbox.

        }
```
Ok, so we learnt how to send a message and get a message. now lest say we want to create a MMO game right? so we need to update the characters location? Go to your client and make a new timer which will send every a few second the character location:
```
private void axWinsock1_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
        //Code by: ShowLoco
          axWinsock1.SendData("loc/" + pictureBox1.Location.X.ToString() + "/" + pictureBox1.Location.Y.ToString());
    // We gonna send a message with the character.x and character .y
        }
```
Now go back to the server, and make two new Listboxes named: locationx and locationy.
```
//Code by: ShowLoco
          string[] commands = message.Text.Split('/'); // We will split the string by the char "/".
          int count = 0; // We will count the location.
            string com = "";// here we gonna save the command.
          foreach (string command in commands) // For each command we have in the commands list by our split char.
          {
              if (count == 0) // first command.
              {
                  com = command; // Save our command.
              }

                  if (com == "loc") // If the command = to "loc" mean it's the location command.
                  {
                      if (count == 1) // First we got x, so it's 1.
                      {
                          locationx.Items.Add(command); // add Location x to Listbox location x
                      }
                      if(count == 2)// Now it's Y.
                      {

                          locationy.Items.Add(command); //Add location y to Listbox locationy
                  }

                  }

              count = count + 1; // check the next command (next split by our char).
          }
```So now you have a basic TCP or UDP for your mmo :) you can easily add your own commands!
Sorry for my English…
**This is my tutorial so far! i created all the pictures and the codes!! please do not copy this tutorial!**
Link to comment
Share on other sites

This only works with one server to one client. Luckily for VB6, it had design object arrays; .NET, however, does not have this privilege, so a single thread can not be reproduced for each player joining the server (you'd have to deformat their names from **winsock1** and **winsock2** to **winsock + num**).

To simulate a one-to-many object model, you should be able to use the networking interface given to you via the .NET libraries (Berkeley Sockets) . No COM needed.
Link to comment
Share on other sites

Doing networking in .net is asking for bloody difficulties, really.

Even if you can get it to work, you'll have to know a lot of details about network programming, which almost no .net programmer has. That's why I encourage Lidgren. It's the only solution for .net programmers that works, so that they can be ignorant about the matter, as much as they want, like usual.

Yours faithfully
  Stephan.
Link to comment
Share on other sites

@S.J.R.:

> Doing networking in .net is asking for bloody difficulties, really.
>
> Even if you can get it to work, you'll have to know a lot of details about network programming, which almost no .net programmer has. That's why I encourage Lidgren. It's the only solution for .net programmers that works, so that they can be ignorant about the matter, as much as they want, like usual.
>
> Yours faithfully
>   Stephan.

Oh, God, yes!

Listen to Goddie. He knows what he's talking about.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...