Advertisement
Guest User

ChatServer.vb

a guest
Feb 4th, 2012
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.52 KB | None | 0 0
  1. Imports Microsoft.VisualBasic
  2.  
  3. Public Class ChatServer
  4.  
  5.     Dim Tcp_Listern As System.Net.Sockets.TcpListener
  6.     Dim port As Integer = 80
  7.     Dim accept As Boolean = False
  8.     Dim sql As New SqlAbfrage
  9.     Public AllUsernames As String
  10.     Public TotalUsers As Integer = 0
  11.     Public ChatclientListe As New SortedList(Of Integer, ChatClient)
  12.     Public User_Dic As New SortedDictionary(Of String, Integer)
  13.     Public Auth As New SortedDictionary(Of String, Integer)
  14.     Public Event ClientDisconnected(ByVal c As ChatClient)
  15.     Public Event ClientNewMessage(ByVal c As ChatClient, ByVal sMessage As String)
  16.     Public Event NewclientConnected(ByVal c As ChatClient)
  17.     Public Event Kick(ByVal sKey As String, ByVal c As ChatClient)
  18.  
  19.     Public Sub start(ByVal Port As Integer)
  20.  
  21.         Tcp_Listern = New System.Net.Sockets.TcpListener(Port)
  22.         accept = True
  23.         Dim t As New System.Threading.Thread(AddressOf PickUp_connections)
  24.         t.IsBackground = True
  25.         t.Start()
  26.     End Sub
  27.     Public Sub PickUp_connections()
  28.         Tcp_Listern.Start()
  29.         While accept = True
  30.             'Dim TcpC As System.Net.Sockets.TcpClient = Tcp_Listern.AcceptTcpClient
  31.             TotalUsers += 1
  32.             Dim ChatC As New ChatClient
  33.             ChatC.Tcp_client = Tcp_Listern.AcceptTcpClient
  34.             ChatC.ClientiD = TotalUsers
  35.             ChatclientListe.Add(TotalUsers, ChatC)
  36.             Dim reply_2client As String = ChatC.Start(ChatC.Tcp_client)
  37.             If reply_2client = True Then
  38.                 If User_Dic.ContainsKey(ChatC.ChatName) = False And User_Dic.ContainsValue(TotalUsers) = False Then
  39.                     User_Dic.Add(ChatC.ChatName, TotalUsers)
  40.                 ElseIf reply_2client = False Then
  41.                     User_Dic.Remove(ChatC.ChatName)
  42.                     ChatC.Terminate_connection(reply_2client, False)
  43.                     ChatclientListe.Remove(User_Dic.Item(ChatC.ChatName))
  44.                 End If
  45.                 Get_All_Users()
  46.                 AddHandler ChatC.VerbindungUnterbrochen, AddressOf Disconnected
  47.                 AddHandler ChatC.NewMessage, AddressOf NewMessage
  48.                 AddHandler ChatC.Kiick, Sub(skey As String, c As ChatClient) Kickbykey(skey, c)
  49.                 RaiseEvent NewclientConnected(ChatC)
  50.             Else
  51.                 ChatC.Terminate_connection("nope", False)
  52.             End If
  53.         End While
  54.     End Sub
  55.     Private Sub Disconnected(ByVal c As ChatClient)
  56.         RaiseEvent ClientDisconnected(c)
  57.     End Sub
  58.  
  59.     Private Sub NewMessage(ByVal c As ChatClient, ByVal sMessage As String)
  60.         RaiseEvent ClientNewMessage(c, sMessage)
  61.         SchreibeAnJedenClient(sMessage)
  62.     End Sub
  63.  
  64.     Public Sub SchreibeAnJedenClient(ByVal sText As String)
  65.         '  Try      comment that out to get the error message when the error ocurres
  66.         System.Threading.Thread.Sleep(1500)
  67.         For Each pair In ChatclientListe
  68.             Dim c As ChatClient = pair.Value
  69.             c.write(sText)
  70.         Next
  71.     End Sub
  72.  
  73.     Public Sub Kickbykey(ByVal sKey As String, ByVal cc As ChatClient)
  74.  
  75.         If User_Dic.ContainsKey(sKey) = True Then
  76.  
  77.             sKey = User_Dic.Item(sKey)
  78.             MsgBox(sKey & "kick by key")
  79.  
  80.             Try
  81.                 Dim c As ChatClient = ChatclientListe.Item(sKey)
  82.  
  83.                 c.Terminate_connection("<kicked>", True)
  84.                 ChatclientListe.Remove(sKey)
  85.                 Get_All_Users()
  86.             Catch ex As Exception
  87.                 MsgBox(ex.Message)
  88.             End Try
  89.         Else
  90.             MsgBox("kickbykey")
  91.             ' FeedBack
  92.         End If
  93.     End Sub
  94.     Public Sub Get_All_Users()
  95.         AllUsernames = ""
  96.         Try
  97.             For Each pair In User_Dic
  98.                 If AllUsernames = "" Then
  99.                     AllUsernames = pair.Key
  100.                 Else
  101.                     Console.WriteLine(pair.Key & pair.Value)
  102.                     AllUsernames = AllUsernames & "," & pair.Key
  103.  
  104.                 End If
  105.             Next
  106.         Catch ex As Exception
  107.             Console.WriteLine("Key was not Present in User_Dic")
  108.         End Try
  109.         SchreibeAnJedenClient("userlistupdate,1" & AllUsernames)
  110.     End Sub
  111.  
  112.     Public Sub Stopp(Optional ByVal sText As String = "The server shut's down due to a failuare.")
  113.         On Error Resume Next
  114.         SchreibeAnJedenClient(sText)
  115.         accept = False
  116.         Tcp_Listern.Stop()
  117.         User_Dic.Clear()
  118.         Auth.Clear()
  119.         ChatclientListe.Clear()
  120.     End Sub
  121. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement