|
-
Jul 8th, 2009, 06:14 PM
#1
Thread Starter
Addicted Member
Detect client disconnect
I've taken and modified a sample chat program found online, and I've added a few features, one of which adds the username of a new user to a listbox clientside. This isn't exactly what I want, as it only detects users after you have joined the chat, but I'm taking baby steps. What I want to do is detect when a user leaves and then remove that name from the list. What's a good way of going about this? I'm not entirely sure this code was written very well, so if it looks like crap, point out ways to improve it, as I'm not experienced enough to see if its poorly written or not. Any help would be appreciated. Thanks.
Client:
Code:
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Dim clientSocket As New System.Net.Sockets.TcpClient()
Dim serverStream As NetworkStream
Dim readData As String
Dim infiniteCounter As Integer
Dim UserNotify As String = "_"
Private Sub Send_Message(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendButton.Click
Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(SendBox.Text + "$")
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
End Sub
Private Sub msg()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf msg))
Else
DisplayBox.Text = DisplayBox.Text & Environment.NewLine & Now & ": " & readData
End If
End Sub
Private Sub Connect_Server(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
readData = "Connected to Chat Server ..."
msg()
clientSocket.Connect("192.168.1.2", 1123)
serverStream = clientSocket.GetStream()
Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(UserNotify + UserName.Text + "$")
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf getMessage)
ctThread.Start()
End Sub
Private Sub getMessage()
For Me.infiniteCounter = 1 To 2
Try
infiniteCounter = 1
serverStream = clientSocket.GetStream()
Dim buffSize As Integer
Dim inStream(10024) As Byte
buffSize = clientSocket.ReceiveBufferSize
serverStream.Read(inStream, 0, buffSize)
Dim returndata As String = System.Text.Encoding.ASCII.GetString(inStream)
If returndata.Substring(0, 1) = UserNotify Then
'returndata = readdata.Remove((readdata.LastIndexOfAny(" Joined") - 6), 7)
readData = returndata.Remove(0, 1)
NewUser(readData.Remove((readData.LastIndexOfAny(" Joined") - 6), 7))
Else
readData = returndata
End If
msg()
'Catch Loss of Server Connection
Catch ex As Exception
MsgBox("Connection to chat server lost!")
infiniteCounter = 3
clientSocket.Close()
clientSocket = New System.Net.Sockets.TcpClient()
End Try
Next
End Sub
Private Delegate Sub NewUserInvoker(ByVal newusername As String)
Private Sub NewUser(ByVal newusername As String)
If UsersOnline.InvokeRequired Then
Me.UsersOnline.Invoke(New NewUserInvoker(AddressOf NewUser), newusername)
Else
UsersOnline.Items.Add(newusername)
End If
End Sub
End Class
Server:
Code:
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Module Module1
Dim clientsList As New Hashtable
Sub Main()
Dim hoststring As String
Dim ipstring As String
Dim Port As Integer = 1123
hoststring = System.Net.Dns.GetHostName()
ipstring = System.Net.Dns.GetHostByName(hoststring).AddressList(0).ToString()
Dim localAddr As IPAddress = IPAddress.Parse(ipstring)
Dim serverSocket As New TcpListener(localAddr, Port)
Dim clientSocket As New TcpClient
Dim counter As Integer
serverSocket.Start()
msg("Chat Server Started ....")
msg("Server Hostname: " & hoststring)
msg("Server IP: " & ipstring & " Port: " & Port)
counter = 0
While (True)
counter += 1
clientSocket = serverSocket.AcceptTcpClient()
Dim bytesFrom(10024) As Byte
Dim dataFromClient As String
Dim networkStream As NetworkStream = clientSocket.GetStream()
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
clientsList(dataFromClient) = clientSocket
broadcast(dataFromClient + " Joined ", dataFromClient, False)
msg(dataFromClient + " Joined chat room ")
Dim client As New handleClient
client.startClient(clientSocket, dataFromClient, clientsList)
End While
clientSocket.Close()
serverSocket.Stop()
msg("exit")
Console.ReadLine()
End Sub
Sub msg(ByVal mesg As String)
mesg.Trim()
Console.WriteLine(" >> " + mesg)
End Sub
Private Sub broadcast(ByVal msg As String, ByVal uName As String, ByVal flag As Boolean)
Dim Item As DictionaryEntry
For Each Item In clientsList
Dim broadcastSocket As TcpClient
broadcastSocket = CType(Item.Value, TcpClient)
Dim broadcastStream As NetworkStream = broadcastSocket.GetStream()
Dim broadcastBytes As [Byte]()
If flag = True Then
broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg)
Else
broadcastBytes = Encoding.ASCII.GetBytes(msg)
End If
broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
broadcastStream.Flush()
Next
End Sub
Public Class handleClient
Dim clientSocket As TcpClient
Dim clNo As String
Dim clientsList As Hashtable
Public Sub startClient(ByVal inClientSocket As TcpClient, ByVal clineNo As String, ByVal cList As Hashtable)
Me.clientSocket = inClientSocket
Me.clNo = clineNo
Me.clientsList = cList
Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
ctThread.Start()
End Sub
Private Sub doChat()
'Dim infiniteCounter As Integer
Dim requestCount As Integer
Dim bytesFrom(10024) As Byte
Dim dataFromClient As String
'Dim sendBytes As [Byte]()
'Dim serverResponse As String
Dim rCount As String
requestCount = 0
While (True)
Try
requestCount = requestCount + 1
Dim networkStream As NetworkStream = clientSocket.GetStream()
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
msg("From client - " + clNo + " : " + dataFromClient)
rCount = Convert.ToString(requestCount)
broadcast(dataFromClient, clNo, True)
Catch ex As Exception
End Try
End While
End Sub
End Class
End Module
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|