Page 1 of 1

Advanced P2P Networking - Chatrooms [Part 2] Rate Topic: -----

#1 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Posted 20 January 2010 - 02:14 PM

Link to Part 1

Server Wrapper - BB_Server.vb
Imports RTS_Listener
Imports System.Net

''' <summary>
''' This is the BB server, fill in properties for full effect 
''' </summary>
''' <remarks></remarks>
Public Class BB_Server
	Inherits Object
	Private WithEvents Server As New RTS_Listener

#Region " Properties "
	Private esAdminUsername As String = "Admin"
	Public Property AdminUsername() As String
		Get
			Return esAdminUsername
		End Get
		Set(ByVal value As String)
			esAdminUsername = value
		End Set
	End Property

	Private esPort As Integer = 50000
	Public Property Port() As Integer
		Get
			Return esPort
		End Get
		Set(ByVal value As Integer)
			esPort = value
		End Set
	End Property

	Private esLog As String = String.Empty
	Public ReadOnly Property Log() As String
		Get
			Return esLog
		End Get
	End Property

	Private esMaxConnections As Integer = 500 '(499 as zero counts as a connection)
	Public Property MaxConnections() As Integer
		Get
			Return esMaxConnections
		End Get
		Set(ByVal value As Integer)
			esMaxConnections = value
		End Set
	End Property

	Private esCurrentConnections As Integer = 0
	Public ReadOnly Property CurrentConnections() As Integer
		Get
			Return esCurrentConnections
		End Get
	End Property

	Private esMaxMessageLength As Integer = 200
	Public Property MaxMessagelength() As Integer
		Get
			Return esMaxMessageLength
		End Get
		Set(ByVal value As Integer)
			esMaxMessageLength = value
		End Set
	End Property

	Private esLogInToTalk As Boolean = True
	Public Property LogInToTalk() As Boolean
		Get
			Return esLogInToTalk
		End Get
		Set(ByVal value As Boolean)
			esLogInToTalk = value
		End Set
	End Property

	Private esServerStatus As _ServerStatus
	Public Property ServerStatus() As _ServerStatus
		Get
			Return esServerStatus
		End Get
		Set(ByVal value As _ServerStatus)
			esServerStatus = value
		End Set
	End Property
#End Region

#Region " Server Commands "
	''' <summary>
	''' Starts the server on the dedicated port defualt is, 50000
	''' </summary>
	''' <remarks></remarks>
	Public Sub StartServer()
		Server.Listen(Port)
		ServerStatus = _ServerStatus.Running
	End Sub

	''' <summary>
	''' Stops and disposes of the current server
	''' </summary>
	''' <remarks></remarks>
	Public Sub StopServer()
		ServerStatus = _ServerStatus.Stopping
		Server.Broadcast(Contact.Shutdown)
		Server.Close()
		ServerStatus = _ServerStatus.Stoped
	End Sub

	''' <summary>
	''' Sends A message on behalf of you to all clients
	''' </summary>
	''' <param name="Message"></param>
	''' <remarks></remarks>
	Public Sub SendMessage(ByVal Message As String)
		Server.Broadcast(Contact.Normal, AdminUsername & " : " & Message)
	End Sub

	''' <summary>
	''' Resets the events log
	''' </summary>
	''' <remarks></remarks>
	Public Sub ResetLog()
		esLog = String.Empty
	End Sub

	''' <summary>
	''' Return the entire log. (Or up till it was last cleared)
	''' </summary>
	''' <returns></returns>
	''' <remarks></remarks>
	Public Function ReadLog()
		Return Log
	End Function

	Public Sub BootUser(ByVal Username As String)
		Server.BootUser(Username)
	End Sub
#End Region

#Region " Enums "
	Private Enum Contact As Byte
		Normal = 1
		AdminMessage = 2
		ServerMessage = 3
		Shutdown = 4
		Login = 5
		LoginRejected = 6
	End Enum

	Public Enum _ServerStatus
		Running = 1
		Stoped = 2
		Stopping = 3
	End Enum
#End Region

#Region " Server Defiened Events "


	Private Sub Server_ClientLoggedIn(ByVal Username As String) Handles Server.ClientLoggedIn
		RaiseEvent OnUserLoggedIn(Username)
		Server.Broadcast(Contact.Normal, Username & " has joined the chat.")
	End Sub
	' We only need these three unless you want to fileshare, which may soon loose
	' you your internet connection, PERNEMENTLY!!!!!

	Private Sub Server_ConnectionRequest(ByVal Requestor As System.Net.Sockets.TcpClient, ByRef AllowConnection As Boolean) Handles Server.ConnectionRequest
		' to prevent a buffer overflow or a major blue screen because of lack 
		' of ram I introduced this fail safe system that will block all 
		' connections over the specified limit.

		If CurrentConnections >= MaxConnections Then
			AllowConnection = False
		Else
			AllowConnection = True
			RaiseEvent OnConnectionRequest()
			esCurrentConnections = esCurrentConnections + 1
		End If
	End Sub


	Private Sub Server_Disconnect(ByVal Client As RTS_SocketClient) Handles Server.Disconnect
		' We must remember to remove a connection or our server will get clogged!
		esCurrentConnections = esCurrentConnections - 1
		If Client.Username = "" Then
			RaiseEvent OnClientDisconect()
		Else
			RaiseEvent OnUserDisconnect(Client.Username)
		End If
		Server.Broadcast(Contact.Normal, Client.Username & " has just left the chat.")
	End Sub

	Private Sub Server_MessageReceived(ByVal Client As RTS_SocketClient, ByVal message As Byte) Handles Server.MessageReceived
		RaiseEvent OnMessageRecived(message)
	End Sub

	Private Sub Server_StringReceived(ByVal Client As RTS_SocketClient, ByVal msgTag As Byte, ByVal message As String) Handles Server.StringReceived
		If msgTag = Contact.Normal Then
			If message.Count > MaxMessagelength Then
				Client.Send(Contact.ServerMessage, "Message must not be longer than " & MaxMessagelength & " characters. Yours was " & message.Count & " characters long.")
			Else
				Dim b As String = Server.ReturnUsername(Client)
				If Not b = "" Then
					Dim c As String = (b & ": " & message)
					Server.Broadcast(Contact.Normal, c)
					RaiseEvent OnMessageRecived(c)
				Else
					If LogInToTalk = False Then
						Dim c As String = (b & ": " & message)
						Server.Broadcast(Contact.Normal, c)
						RaiseEvent OnMessageRecived(c)
					Else
						Client.Send(Contact.ServerMessage, "You must login before you can talk.")
					End If
				End If
			End If
		ElseIf msgTag = Contact.Login Then
			If Server.ValidateLogin(message, AdminUsername, Client) = True Then
				Server.Login(Client, message)
				RaiseEvent OnUserLoggedIn(message)
				Server.Broadcast(Contact.Normal, message & " has joined the chat.")
			Else
				Client.Send(Contact.LoginRejected, "Your username was rejected.")
			End If
		Else
			' We should only be getting messages with the normal tag, otherwise
			' Our server may of been comprimised / someone has got a rouge app
			' in the conversation.
			Exit Sub
		End If
	End Sub
#End Region

#Region " Events "
	Public Event OnConnectionRequest()
	Public Event OnClientDisconect()
	Public Event OnMessageRecived(ByVal Message As String)
	Public Event OnUserLoggedIn(ByVal Username As String)
	Public Event OnUserDisconnect(ByVal Username As String)
#End Region
End Class


This code is fairly simply, but it saves you having to type it out again.

Client - BB_Client.vb
''' <summary>
''' This is the BB client, fill in properties for full effect.
''' </summary>
''' <remarks></remarks>
Public Class BB_Client
	Inherits Object
	Private WithEvents Client As New RTS_SocketClient

#Region " Properties "
	Private esPort As Integer = 50000
	Public Property Port() As Integer
		Get
			Return esPort
		End Get
		Set(ByVal value As Integer)
			esPort = value
		End Set
	End Property

	Private esNickName As String = ""
	Public Property NickName() As String
		Get
			Return esNickName
		End Get
		Set(ByVal value As String)
			esNickName = value
		End Set
	End Property

	Private esConnected As Boolean = False
	Public ReadOnly Property Conncected() As Boolean
		Get
			Return esConnected
		End Get
	End Property

	Private esConversationLog As String = ""
	Public ReadOnly Property ConverstationLog() As String
		Get
			Return esConversationLog
		End Get
	End Property
#End Region

#Region " Enums "
	Private Enum Contact As Byte
		Normal = 1
		AdminMessage = 2
		ServerMessage = 3
		Shutdown = 4
		Login = 5
		LoginRejected = 6
	End Enum
#End Region

#Region " Public Code "
	''' <summary>
	''' Sends your message to the server, it will be rejected if it is too long.
	''' </summary>
	''' <param name="Message"></param>
	''' <remarks></remarks>
	Public Sub SendMessage(ByVal Message As String)
		Try
			Client.Send(Contact.Normal, Message)
		Catch ex As Exception
			RaiseEvent onerror(ex)
		End Try
	End Sub

	''' <summary>
	''' Connects you to a remote host
	''' </summary>
	''' <param name="IP"></param>
	''' <remarks></remarks>
	Public Sub Connect(ByVal IP As String)
		Try
			Client.ConnectToHost(IP, Port)
		Catch ex As Exception
			RaiseEvent onerror(ex)
		End Try
	End Sub

	''' <summary>
	''' Disconnect from your remote host.
	''' </summary>
	''' <remarks></remarks>
	Public Sub Disconnect()
		Try
			Client.DisconnectFromHost()
		Catch ex As Exception
			RaiseEvent onerror(ex)
		End Try
	End Sub

	''' <summary>
	''' Loggs a Username.
	''' </summary>
	''' <param name="Username"></param>
	''' <remarks></remarks>
	Public Sub Login(ByVal Username As String)
		If Not Me.Conncected = True Then
			Throw New ApplicationException("The client must be connected to a host before logging in")
			Exit Sub
		End If
		Client.Send(Contact.Login, Username)
		'//////////////////////////////////////////////////
		MsgBox("Login Sent")
	End Sub
#End Region

#Region " Events "
	Public Event OnConnect()
	Public Event OnDisconnect()
	Public Event OnServerMessage(ByVal Message As String)
	Public Event OnNewMessage(ByVal Message As String)
	Public Event onerror(ByVal _Error As Exception)
	Public Event OnLoginRejected(ByVal RejectMessage As String)

	Private Sub Client_Connect(ByVal sender As RTS_SocketClient) Handles Client.Connect
		esConnected = True
		RaiseEvent OnConnect()
	End Sub

	Private Sub Client_Disconnect(ByVal sender As RTS_SocketClient) Handles Client.Disconnect
		esConnected = False
		RaiseEvent OnDisconnect()
	End Sub

	Private Sub Client_StringReceived(ByVal Sender As RTS_SocketClient, ByVal msgTag As Byte, ByVal message As String) Handles Client.StringReceived
		' Just checks to ensure the right events are raised.
		' As a rule of thumb you ownly add server messages to your message display
		' just to save confusion.
		If msgTag = Contact.ServerMessage Then
			RaiseEvent OnServerMessage(message)
		ElseIf msgTag = Contact.Normal Then
			RaiseEvent OnNewMessage(message)
		ElseIf msgTag = Contact.LoginRejected Then
			RaiseEvent OnLoginRejected(message)
		End If
	End Sub
#End Region
End Class



The only thing that I can recommend is adding Password Support here.

Other Notes:
Like I have said before there probably are some bugs here that I haven't seen or found. And if you do find one please tell me and I will attempt to fix it. The reason why this tutorial requires pretty advanced VB coders isnt because the code is particularly hard but the concepts behind the code.

Enjoy your new found P2P knowledge :D !

Is This A Good Question/Topic? 1
  • +

Replies To: Advanced P2P Networking - Chatrooms [Part 2]

#2 irfan_4f  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 4
  • Joined: 12-January 10

Posted 08 March 2010 - 12:43 PM

wow thank u


i want to do partially clientserver chat application

i have a idea that if user login to server his username and ip address shold detected and send to all the clients and in client list only username should display , ip address is kept hide , when user clicks any client it should connect directly to that client instead of server so that server load will be less
only for conference chat we should use server if u have any code just mail to <Removed>

thank u in advance

if u have voice chat code jst send it to same id

This post has been edited by macosxnerd101: 02 March 2011 - 03:57 PM
Reason for edit:: Email removed per site policies.

Was This Post Helpful? -5
  • +
  • -

#3 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Posted 10 March 2010 - 08:10 AM

Okay if you actually read this tutorial then you would know exactly how to do what you are asking... Also NEVER post your email on-line without 'cloaking' it. Ie using <at> instead of @.
Was This Post Helpful? 0
  • +
  • -

#4 tinhvn83  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 1
  • Joined: 20-April 10

Posted 20 April 2010 - 07:33 PM

Dear asscotte

At moment, I am building a the same project,I cannot use your code because without some function,events.I want to reference your project.
pls send to me source code, my email: vvtinhtvn@yahoo.com
thanks.
Was This Post Helpful? -1
  • +
  • -

#5 pborner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-June 10

Posted 02 March 2011 - 03:56 PM

Ascotte,

Awesome tutorial! Thanks for your hard work. I am trying to set this up @ work, but im a little confused exactly how its supposed to look/work.
If you could show an example of what the Server/Client should/could look like id really appreciate it! Keep up the good work sir!
Was This Post Helpful? 0
  • +
  • -

#6 MarkC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-March 11

Posted 27 March 2011 - 10:07 AM

Hi Asscotte,


I've read your tutorial and im also trying to create a server<->multiclient chat application. please correct me if im wrong, are the bb_server and bb_client the 'connections' needed for a form to materialize? so the project is like this?:

Server App:
->rts_listener.vb(class)
->rts_socketclient.vb(class)
->bb_server.vb(class)
->form1.vb(form)

Client App:
->rts_socketclient.vb(class)
->bb_client.vb(class)
->form1.vb(form)

cheers for an awesome tutorial^^

Thanks & Best Regards,
Was This Post Helpful? 0
  • +
  • -

#7 MarkC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-March 11

Posted 31 March 2011 - 07:46 PM

Hi Asscotte,


I have a question here, where do i find the connecting client's username in the server? can you show me the flow of it from the stream down to the clients list? thank you very much for your help :)
Was This Post Helpful? 0
  • +
  • -

#8 MarkC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-March 11

Posted 02 April 2011 - 10:20 PM

Hi again Asscotte,


I've finished creating a program using your classes indicated in the tutorial and there's one last thing that still bothers me. when i try the client to disconnect and click the connect button again, or tried exiting the client form and starting it again without restarting the server, i get an error in the network stream saying it cant connect to non connected sockets. any ideas as to how to properly close and disconnect the client and remove it from the existing connections in the server's connected clients?

Thanks!

MarkC :)
Was This Post Helpful? 0
  • +
  • -

#9 MarkC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-March 11

Posted 05 April 2011 - 11:23 AM

Hi Asscotte,


Thanks for the awesome tutorial! I have learned a lot from it. I've managed to finish my project with it:) just dropped by to say thanks, really^^


Best Regards,


MarkC
Was This Post Helpful? 0
  • +
  • -

#10 theotherguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 05-April 11

Posted 05 April 2011 - 09:52 PM

Hi MarkC,

I figured you have already finished the interface. Can you post your interface code? It will be really helpful for me. I'm getting nowhere by bumping into errors that the compiler wouldn't tell me. I'm also trying to build interface based on classes provided by Asscotte.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#11 tomstefanou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-June 11

Posted 25 June 2011 - 10:52 AM

Hello i am new to VB .net and i have created 2 projects in vb .net 2010. I am using the RTS_SocketClient, RTS_Listener and BB_server classes. I have correctly implemented these and so far i can do the following...
1) connect multiple clients to the server
2) send string messages between them.

My problem is that from what i can see in the RTS_SocketClient and RTS_Listener classes are methods for exchanging files.
e.g send file from server to client and vice versa.

I do not know how to implement these functions in the BB_Server / BB_Client wrappers.

I keep looking at the "Do Not : Ask us to do your homework or project for you". but i do not have enough knoweledge to make it on my own. If anybody is willing to help it would be greatly appreciated.

Perhaps i am at the wrong implementation here but anyway so to be clear as what i want to accomplish is this.

1) Server will be on a machine with a static ip.
2) Server module will run there.
3) Client will connect to ths ip and be able to fetch files from the server.
4) Client will also be able to send files to the server.

I think all that is possible with the provided classes but as i said my knoweledge very limited as to what i try to do.

Thank you all for your time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1