.Net socket server and chat channels

Im working on ARPG genre game and i have been developing server by using vb.net 2010. Im running login , lobby and game sockets on ports 7111-7113 and each socket has own thread. example : When client connects login thread launch new thread and binds this new client data flow trought that.

here is picture to show logic behind my work :
1199036--47879--$TCPServer.jpg

But now to the my question. How i create channels? I want 16 player limited channels to chat and 8 players to ingame channel.

Think about this: my client send data , packet structure is [ID,DATA] = [0x01 ,“Hello/Channel1”]. Server must check from array or collection where to send this data. We know that i want send it to clients in Channel1. I have thinking about creating multidimensional array OR collection, collection would be nice because i can acces those client’s easily.

AND second question.

How i can store channels to collection? I know how to use collection, so im not asking help for that. But im thinking of multidimensional collection , which inculdes variables Object and String. Object stays one of connected client and string has channel name.

could it work ? tell me ! :smile:

I need some example code. Please help me :slight_smile:

Bump

Still looking for answer :slight_smile:

Hmm… not entirely sure what your problem is. As a suggestion, consider creating a room class like so:

//Written off the top of my head.
//Rough Illustration only

public class Room {


    public static List <Room> rooms = new List <Room>();


    public string Name = "";
    public List <User> users = new List <User>();


    object _lock;
  
    public static Room GetOrAdd(string name) {
        lock(_lock) {
            var room = rooms.FirstOrDefault(r = > r.Name == name)
            if (room == null) rooms.Add(room = new Room {
                    Name = name
                });
            return room;
        }
    }


    public bool Join(User user) {
        lock(_lock)
            if (users.Count > 15) return false;
            else users.Add(user);
        Broadcast(new Message("User Joined: " + user.Name));
        return true;
    }


    public void Leave(User user) {
        lock(_lock)
            if (!users.Contains(user)) return;
            else users.Remove(user);
        Broadcast(new Message("User Left: " + user.Name));
        return true;
    }
  
    public void Broadcast(Message m) {
        lock(_lock)
            foreach(var u in Users) user.Send(m);
    }
}

if (users.Count > 15) return false;

What happens users after that? As i see it just returns false, it should create new room with limit of 16 players. Thanks for code, i still need to figure this by myself because currently that code just handles one room, right?

Yeah, it can do whatever you want it to do - this your code! In the example given I simply assumed each room had a unique string array, and only 16 people could be in a room [though it supports a huge number of rooms - limited only by memory].

Okey but how you gonna send data to these 16 players in room only ? i dont understand :confused:

Then I probably suggest you look rethink what you’re trying to do. Building multi-threaded servers isn’t easy and if you can’t figure it out few are going to be able to help you. Maybe muck around with Photon cloud - implementing chat in that system is fairly easy.

However, to answer the question, I’d assume each player knows what room he is in, so can send his message to said room. That room then knows which players are in it [the list of users] and will loop through them all and send each one the message.

Thanks for answer.
Sorry but i was bit confused :slight_smile: My project is kinda big.

So, i have trying to translate this code to vb.net

Public Class Room
    
    Public Shared rooms As List(Of Room) = New List(Of Room)
    
    Public Name As String = ""
    
    Public users As List(Of User) = New List(Of User)
    
    
    Public Shared Function GetOrAdd(ByVal name As String) As Room
        Dim room As var = rooms.FirstOrDefault(() => {  }, (r.Name = name))
        If (room Is Nothing) Then
            rooms.Add(room=newRoom{Name=nameUnknown)
        End If
        Return room
    End Function
    
    Public Function Join(ByVal user As User) As Boolean
        If (users.Count > 15) Then
            Return false
        Else
            users.Add(user)
        End If
        Broadcast(New Message(("User Joined: " + user.Name)))
        Return true
    End Function
    
    Public Sub Leave(ByVal user As User)
        If Not users.Contains(user) Then
            Return
        Else
            users.Remove(user)
        End If
        Broadcast(New Message(("User Left: " + user.Name)))
        Return true
    End Sub
    
    Public Sub Broadcast(ByVal m As Message)
        For Each u As var In Users
            user.Send(m)
        Next
    End Sub
End Class

And im going to build this class but im gonna modify it a little. I will post here as soon i get some stuff up and running.

Npfs3000 can you check your code, is line " var room = rooms.FirstOrDefault(r = > r.Name == name)" written correctly? Im not familiar with c#

Nevermind anymore. Thanks for class got it working atlast.