Creating An Aplication That Take Users In Server And Making Server Lobby , Create User to Bind Server Lobby And Sending Request for Adding Friend or Sending Message Application Using Visual Studio Part 1 - Server Part

Repository https://github.com/github/VisualStudio
my project repository https://github.com/aftersteem/server-loby

What will i learn?

  • The concept of how computer make connection with each other.
  • The tcp concept on software.
  • how we can comminicate with any computer and send string concept or make order any computer what ever you want.
  • how can you send string or anything to another computer
  • broadcasting
  • buffer method
  • string to bits concept for making computer understand your command

Requirements

visual studio https://visualstudio.microsoft.com/tr/downloads/?rr=https%3A%2F%2Fwww.google.com.tr%2F

Difficulty

  • intermediate

Tutorial Content

We are going to learn how comminucation is feasible with server and user interface . This is usable for chatting , broadcasting , playing game , the loby of game that user can comminicate with each other , or give order to the computer to decide with each user to give order to server computer which is listening connections.There will be lots of users who connect our server and commincate or request to game for playing something etc.

Outline

  • Connection with each computer
  • Make chatting and requesting like friend or game
  • Understanding of tcp and internet concept
  • Broadcast information with server computer

How to open :

file -> new project -> windows forms applications
for adding button need to open Tool Box
for changing text of button or textfield or richtextbox need to open proporties (with right click) or changing any concept of object whatever you want.

Design Part:

Adding button,

4.png
Adding textbox and label,

5.png

And final part of design for this part ,additionally rich text box

6.png

İmportation

Firstly,we need too make importation of library for using the function of it

using System.Text;   
using System.Threading.Tasks; 
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;

Constraction Our Variable

Firstly, we need to think what we will do on connection

  1. Need to listen incomming connections
  2. Need to check if there is a problem or not
  3. Need to choose which port will be lintening
  4. Need to constract socket
  5. Need to save socket list for identifying who bind us

for all of this we need to make variable:

-for check listening in construction it need to be false on startupping program,for checking if connection is terminate or not (there can be any problem or user who connect our server,quit),socket for binding in our local network whose protocol tcp, in local network protocol is tcp,the list of our socket means that our users .

static bool listening = false; 
static bool terminating = false;
static bool accept = true;
static Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
 static List<Socket> socketList = new List<Socket>();

START TO LİSTENİNG İNCOMİNG CONNECTİONS

This is the button whose text is listen.We need to broadcast and accept connection in the port which server decide , it can be anything in general use 8080 but it depends on what ever server want or your decissions.I made thread for asynchronous action for avoiding crash on multiple part working.For example if we need to make two loop in same time we need to use thread if not program will crash.We take port number on textbox which take string and we need to parse it integger.In this case we need to use try catch method for avoiding carshes , it can be any problem for example connection fail , any exception.ipaddress.any means that any of ip adress which want to connect our server and serverport , in this method means make our broadcast for this port(YOUR DECİSSION) which we decided. RichTextBox1.AppendText is for making inform to user interface , richtextbox is an object wich we can write something but not user can do , text box is an object which user can write something in it.Listen equal true is aiming if there will be no problem until this line listening equal true because of understanding our server is listening or not.

   private void Listen_Click(object sender, EventArgs e)
        {
            int serverPort;
            Thread thrAccept;

            //this port will be used by clients to connect
            serverPort=Int32.Parse(textBox2.Text);

            try
            {
                server.Bind(new IPEndPoint(IPAddress.Any, serverPort));
                richTextBox1.AppendText("Started listening for incoming connections.");

                server.Listen(3); //the parameter here is maximum length of the pending connections queue
                thrAccept = new Thread(new ThreadStart(Accept));
                thrAccept.Start();
                listening = true;
                infiniteServerInput(); //broadcasting function


            }
            catch
            {
                richTextBox1.AppendText("Cannot create a server with the specified port number\n Check the port number and try again.");
                richTextBox1.AppendText("terminating...");
            }
        }

ACCEPTİNG CONNECTİON

This function for accepting our incoming connection to connect our server.If acceptation is true make our loop.With socketlist.add we add the socket (user) which want to connect our server for identifying users.In the catch part, if there was a problem accept equal to false and trying it to add when there will be no problem for connection.

 private void Accept()
       {
           while (accept)
           {
               try
               {
                   socketList.Add(server.Accept());
                   richTextBox1.AppendText("New client connected...\n");
                   Thread thrReceive;
// thrReceive = new Thread(new ThreadStart(Receive));
                  // thrReceive.Start();
               }
               catch
               {
                   if (terminating)
                       accept = false;
                   else
                       richTextBox1.AppendText("Listening socket has stopped working...\n");
               }
           }
       }
H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now