Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Custom Server

Discussion in 'Multiplayer' started by Artenio, Jul 8, 2015.

  1. Artenio

    Artenio

    Joined:
    Jun 12, 2015
    Posts:
    8
    Hello everyone,
    I'm developing a custom server in c# with a custom library used in the server project and exported to be compatible with unity (c# scripting)

    Everything is ok, i have set up the connection, database access and communication between cliend and server is working very well.

    But i have some questions that maybe you will answer. (Yeah i know, my english is really bad)
    I'm using, for the Lobby, a TCP protocoll ( I know is slow and UDP is better, but i think for lobby things is not too much bad)
    The procedure of the game will be this:
    - Login using username and password
    -- Server will reply with a success or an error message based on a DB Query (Check credentials)
    -- If all is ok, the server will send another packet containing information about the player, Money, Experience, Items etc..

    - Lobby:
    -- RoomList , Public Chat, FriendList, and some buttons, one of this go to the Item-Shop

    Players can create Rooms, Can select a map, a gamemode, password etc..
    When the game start, i will start an UDP connection between players and the server (I want to make this server-based, so no p2p connections)

    The game is a 2d game and i have just to syncronize all players and an object that collide with players (Already done this part in "single player")

    - I'm making a Player class, where there are stored PlayerInformations, like Username, Money, Experience etc. so make a List of class where all connected players informations will be stored, and every X minutes sync all to database.

    My questions are :
    How i will connect the "sock" of the connected client? I have to add a Sock var to the Player class?
    Is good to use TCP protocol for lobby and UDP for game?

    Thanks in advance,
    Artenio.
     
  2. Artenio

    Artenio

    Joined:
    Jun 12, 2015
    Posts:
    8
    #Bump..
     
  3. Mabenan

    Mabenan

    Joined:
    Feb 7, 2014
    Posts:
    132
    Answer for Question 1/2: You want multiple sockeds on one Server?

    Answer for Question 3: Yes.
     
  4. Artenio

    Artenio

    Joined:
    Jun 12, 2015
    Posts:
    8
    First of all thanks you for the reply :)
    I think multiple sockets are required, one for each player..
    I have a player class where i have put a
    private Socket cSocket;
    private int SessionID = 0;
    // And some other vars

    the PlayerClass Construction accept 2 arguments
    public cPlayer(Socket tSocket, int sID)
    {
    cSocket = tSocket;
    SessionID = sID;
    }

    When someone connect to the server, i check received data to the database, if all is valid generate a session id, store it in the class, and assign the socket to that player
     
  5. Mabenan

    Mabenan

    Joined:
    Feb 7, 2014
    Posts:
    132
    Ah you want to store the remote socket of the client in a Player Class on the Server yes this is the correct way. It is also helpful to have a Dictionary<ClientSocket, Player> and Dictionary<Player, ClientSocket> so you can get from one to the other without to much searching.
     
  6. Artenio

    Artenio

    Joined:
    Jun 12, 2015
    Posts:
    8
    Hmm i think i dont need a dictionary, i have written a Send Method in the Player Class, that use cSocket.Send

    At the moment thanks for all,
    Really appreciated your help :)