Module1.vb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. Imports System.Net.Sockets
  2. Imports System.Text
  3. Module Module1
  4. Dim clientsList As New Hashtable
  5. Sub Main()
  6. Dim serverSocket As New TcpListener(8888)
  7. Dim clientSocket As TcpClient
  8. Dim infiniteCounter As Integer
  9. Dim counter As Integer
  10. serverSocket.Start()
  11. msg("Chat Server Started ....")
  12. counter = 0
  13. infiniteCounter = 0
  14. For infiniteCounter = 1 To 2
  15. infiniteCounter = 1
  16. counter += 1
  17. clientSocket = serverSocket.AcceptTcpClient()
  18. Dim bytesFrom(10024) As Byte
  19. Dim dataFromClient As String
  20. Dim networkStream As NetworkStream = _
  21. clientSocket.GetStream()
  22. networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
  23. dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
  24. dataFromClient = _
  25. dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
  26. clientsList(dataFromClient) = clientSocket
  27. broadcast(dataFromClient + " Joined ", dataFromClient, False)
  28. msg(dataFromClient + " Joined chat room ")
  29. Dim client As New handleClinet
  30. client.startClient(clientSocket, dataFromClient, clientsList)
  31. Next
  32. clientSocket.Close()
  33. serverSocket.Stop()
  34. msg("exit")
  35. Console.ReadLine()
  36. End Sub
  37. Sub msg(ByVal mesg As String)
  38. mesg.Trim()
  39. Console.WriteLine(" >> " + mesg)
  40. End Sub
  41. Private Sub broadcast(ByVal msg As String, _
  42. ByVal uName As String, ByVal flag As Boolean)
  43. Dim Item As DictionaryEntry
  44. For Each Item In clientsList
  45. Dim broadcastSocket As TcpClient
  46. broadcastSocket = CType(Item.Value, TcpClient)
  47. Dim broadcastStream As NetworkStream = _
  48. broadcastSocket.GetStream()
  49. Dim broadcastBytes As [Byte]()
  50. If flag = True Then
  51. broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg)
  52. Else
  53. broadcastBytes = Encoding.ASCII.GetBytes(msg)
  54. End If
  55. broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
  56. broadcastStream.Flush()
  57. Next
  58. End Sub
  59. Public Class handleClinet
  60. Dim clientSocket As TcpClient
  61. Dim clNo As String
  62. Dim clientsList As Hashtable
  63. Public Sub startClient(ByVal inClientSocket As TcpClient, _
  64. ByVal clineNo As String, ByVal cList As Hashtable)
  65. Me.clientSocket = inClientSocket
  66. Me.clNo = clineNo
  67. Me.clientsList = cList
  68. Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
  69. ctThread.Start()
  70. End Sub
  71. Private Sub doChat()
  72. Dim infiniteCounter As Integer
  73. Dim requestCount As Integer
  74. Dim bytesFrom(10024) As Byte
  75. Dim dataFromClient As String
  76. Dim sendBytes As [Byte]()
  77. Dim serverResponse As String
  78. Dim rCount As String
  79. requestCount = 0
  80. For infiniteCounter = 1 To 2
  81. infiniteCounter = 1
  82. Try
  83. requestCount = requestCount + 1
  84. Dim networkStream As NetworkStream = _
  85. clientSocket.GetStream()
  86. networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
  87. dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
  88. dataFromClient = _
  89. dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
  90. msg("From client - " + clNo + " : " + dataFromClient)
  91. rCount = Convert.ToString(requestCount)
  92. broadcast(dataFromClient, clNo, True)
  93. Catch ex As Exception
  94. MsgBox(ex.ToString)
  95. End Try
  96. Next
  97. End Sub
  98. End Class
  99. End Module