Hi,
I am trying to develop a small chat application for office use. I tried to do some research on the web and came up with something of my own. At first it was a single window where you would put your name and the IP of your correspondent. Worked great! Then I decided to add advanced functionalities by including a list of all the people on the network, allowing multiple chats, getting photos of the people connected, etc....
That is where it started going crazy. The app works ok; I mean I can send and receive messages fine but it gives both me and anyone using the app tons of error messages and it kind of lags a lot
My main form
Public Class MainForm
Dim listerner As New TcpListener(44444)
Dim client As TcpClient
Dim message As String = ""
Dim tts As Object
Public connString As String
Public arrImage() As Byte
Public displayName As String
Public myIP As String
Public myID As String
Public myStatus As String
Dim chat As Chat_Window
Dim Sound As New System.Media.SoundPlayer()
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim myMS As New IO.MemoryStream
lblUsername.Text = displayName
For Each ar As Byte In arrImage
myMS.WriteByte(ar)
Next
Dim inImg As Image = Image.FromStream(myMS)
pbProfilePic.Image = inImg
getStaffs()
listerner.Start()
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub MainForm_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
listerner.Stop()
setStatus("Offline", myID, "", "")
Using writer As New StreamWriter("closeApp.bat", False)
writer.WriteLine("Taskkill /IM ""DIOC.exe"" /F")
End Using
Process.Start("closeApp.bat")
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Dim conn As OleDbConnection
Dim command As String
Dim reader As OleDbCommand
Dim sdrReader As OleDbDataReader
Dim intcount As Integer
Dim myIMGCell As New DataGridViewImageCell
Dim temp() As String
Dim friendName As String
Dim myForms As FormCollection = Application.OpenForms
Dim found As Boolean
conn = New OleDbConnection(connString)
Try
conn.Open()
command = "SELECT ID, Status, LastLoginIP FROM Staff"
reader = New OleDbCommand(command, conn)
reader.CommandType = CommandType.Text
reader.CommandText = command
sdrReader = reader.ExecuteReader
While sdrReader.Read
Dim id, ip, status As String
id = sdrReader.Item("ID").ToString
ip = sdrReader.Item("LastLoginIP").ToString
status = sdrReader.Item("Status").ToString
For intcount = 0 To dgContacts.Rows.Count - 1
If dgContacts.Rows(intcount).Cells(0).Value = id Then
dgContacts.Rows(intcount).Cells(4).Value = status
dgContacts.Rows(intcount).Cells(6).Value = ip
If status = "Offline" Then
dgContacts.Rows(intcount).Cells(3).Value = (CType(My.Resources.ResourceManager.GetObject("offline"), Image))
ElseIf status = "Online" Then
dgContacts.Rows(intcount).Cells(3).Value = (CType(My.Resources.ResourceManager.GetObject("online"), Image))
ElseIf status = "Busy" Then
dgContacts.Rows(intcount).Cells(3).Value = (CType(My.Resources.ResourceManager.GetObject("busy"), Image))
ElseIf status = "Away" Then
dgContacts.Rows(intcount).Cells(3).Value = (CType(My.Resources.ResourceManager.GetObject("away"), Image))
End If
End If
Next intcount
End While
sdrReader.Close()
conn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Try
If listerner.Pending = True Then
message = ""
client = listerner.AcceptTcpClient
Dim reader2 As New StreamReader(client.GetStream())
While reader2.Peek <> -1
message = message + Convert.ToChar(reader2.Read()).ToString
End While
Me.Focus()
temp = message.Split(" says:")
friendName = getNameFromIP(temp(0))
message = message.Replace(temp(0), friendName)
For Each frmName As Form In myForms
If frmName.Text.ToString = friendName Then
found = True
Exit For
Else
found = False
End If
Next
If found = True Then
For Each frmName As Form In myForms
If frmName.Text.ToString = friendName Then
chat.txtRead.Text = (chat.txtRead.Text + message + vbCrLf)
chat.txtRead.SelectionStart = chat.txtRead.TextLength
chat.txtRead.ScrollToCaret()
My.Computer.Audio.Play(My.Resources.alert, AudioPlayMode.Background)
frmName.Focus()
Exit For
End If
Next
Else
chat = New Chat_Window
chat.pbFriend.Image = myIMGCell.Value
chat.Text = friendName
chat.pbMe.Image = pbProfilePic.Image
chat.connString = connString
chat.myID = myID
chat.myIP = myIP
chat.friendIP = temp(0)
chat.txtRead.Text = (chat.txtRead.Text + message + vbCrLf)
chat.txtRead.SelectionStart = chat.txtRead.TextLength
chat.txtRead.ScrollToCaret()
My.Computer.Audio.Play(My.Resources.alert, AudioPlayMode.Background)
chat.Show()
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub getStaffs()
Dim conn As OleDbConnection
Dim command As String
Dim reader As OleDbCommand
Dim sdrReader As OleDbDataReader
Dim inImg As Image
conn = New OleDbConnection(connString)
Try
conn.Open()
command = "SELECT * FROM Staff"
reader = New OleDbCommand(command, conn)
reader.CommandType = CommandType.Text
reader.CommandText = command
sdrReader = reader.ExecuteReader
If sdrReader.HasRows Then
While sdrReader.Read()
Dim id As String = sdrReader.Item("ID").ToString
Dim picture = sdrReader.Item("Picture")
Dim name As String = sdrReader.Item("Name").ToString
Dim surname As String = sdrReader.Item("Surname").ToString
Dim status As String = sdrReader.Item("Status").ToString
Dim email As String = sdrReader.Item("Email").ToString
Dim ip As String = sdrReader.Item("LastLoginIP").ToString
If (id = myID) Then Continue While
Dim myMS As New IO.MemoryStream
arrImage = picture
For Each ar As Byte In arrImage
myMS.WriteByte(ar)
Next
If status = "Offline" Then
inImg = (CType(My.Resources.ResourceManager.GetObject("offline"), Image))
ElseIf status = "Online" Then
inImg = (CType(My.Resources.ResourceManager.GetObject("online"), Image))
ElseIf status = "Busy" Then
inImg = (CType(My.Resources.ResourceManager.GetObject("busy"), Image))
ElseIf status = "Away" Then
inImg = (CType(My.Resources.ResourceManager.GetObject("away"), Image))
End If
Dim inImg2 As Image = Image.FromStream(myMS)
dgContacts.Rows.Add(id, inImg2, name & " " & surname, inImg, status, email, ip)
End While
Else
MsgBox("Database empty!")
End If
Catch ex As Exception
MsgBox(ex.ToString)
MsgBox("Error Connecting to database")
Finally
conn.Close()
End Try
End Sub
Public Sub setStatus(ByVal status As String, ByVal id As String, ByVal ip As String, ByVal cnStrng As String)
Dim cn As New OleDb.OleDbConnection
Dim myCmd As New OleDb.OleDbCommand
If connString = "" Then
cn.ConnectionString = cnStrng
Else
cn.ConnectionString = connString
End If
cn.Open()
myCmd.Connection = cn
myCmd.CommandText = "UPDATE Staff SET Status = '" & status & "', LastLoginIP = '" & ip & "' WHERE ID = " & id & ";"
myCmd.ExecuteNonQuery()
myStatus = status
cn.Close()
End Sub
Private Sub dgContacts_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgContacts.CellContentClick
Dim myForms As FormCollection = Application.OpenForms
Dim found As Boolean
For Each frmName As Form In myForms
If frmName.Text.ToString = dgContacts.Rows(e.RowIndex).Cells(2).Value Then
found = True
Exit For
Else
found = False
End If
Next
If found = True Then
For Each frmName As Form In myForms
If frmName.Text.ToString = dgContacts.Rows(e.RowIndex).Cells(2).Value Then
frmName.Focus()
Exit For
End If
Next
Else
chat = New Chat_Window
Dim myIMGCell As New DataGridViewImageCell
myIMGCell = dgContacts.Item("Picture", e.RowIndex)
chat.Text = dgContacts.Rows(e.RowIndex).Cells(2).Value
chat.pbMe.Image = pbProfilePic.Image
chat.pbFriend.Image = myIMGCell.Value
chat.friendIP = dgContacts.Rows(e.RowIndex).Cells(6).Value
chat.friendID = dgContacts.Rows(e.RowIndex).Cells(0).Value
chat.connString = connString
chat.myID = myID
chat.myIP = myIP
chat.myName = lblUsername.Text
chat.Show()
End If
End Sub
Private Function getNameFromIP(ByVal ip As String) As String
Dim reply As String = ""
Dim conn As OleDbConnection
Dim command As String
Dim reader As OleDbCommand
Dim sdrReader As OleDbDataReader
conn = New OleDbConnection(connString)
Try
conn.Open()
command = "SELECT (Name + ' ' + Surname) AS FullName, ID FROM Staff WHERE LastLoginIP = '" & ip & "';"
reader = New OleDbCommand(command, conn)
reader.CommandType = CommandType.Text
reader.CommandText = command
sdrReader = reader.ExecuteReader
If sdrReader.HasRows Then
sdrReader.Read()
reply = sdrReader.Item("FullName").ToString
chat.friendID = sdrReader.Item("ID").ToString
Else
End If
Catch ex As Exception
MsgBox(ex.ToString)
'MsgBox("Error Connecting to database")
Finally
conn.Close()
End Try
Return reply
End Function
End Class
My chat window
Public Class Chat_Window
Public friendIP As String
Public friendID As String
Public myID As String
Public myIP As String
Public myName As String
Public connString As String
Dim listerner As New TcpListener(44444)
Dim client As TcpClient
Private Sub Chat_Window_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Timer1.Enabled = True
Timer1.Start()
listerner.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
'Select Case getStatus(friendID)
'Case "Online"
'friendStatusPanel.BackColor = Color.GreenYellow
'Case "Busy"
'friendStatusPanel.BackColor = Color.Red
'Case "Away"
'friendStatusPanel.BackColor = Color.Orange
'Case "Offline"
'friendStatusPanel.BackColor = Color.White
'End Select
Select Case getStatus(myID)
Case "Online"
myStatusPanel.BackColor = Color.GreenYellow
Case "Busy"
myStatusPanel.BackColor = Color.Red
Case "Away"
myStatusPanel.BackColor = Color.Orange
Case "Offline"
myStatusPanel.BackColor = Color.White
End Select
End Sub
Private Function getStatus(ByVal id As String) As String
Dim conn As OleDbConnection
Dim command As String
Dim reader As OleDbCommand
Dim sdrReader As OleDbDataReader
Dim response As String = ""
conn = New OleDbConnection(connString)
Try
conn.Open()
command = "SELECT Status FROM Staff WHERE ID=" & id & ";"
reader = New OleDbCommand(command, conn)
reader.CommandType = CommandType.Text
reader.CommandText = command
sdrReader = reader.ExecuteReader
sdrReader.Read()
response = sdrReader.Item("Status").ToString
sdrReader.Close()
conn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Return response
End Function
Private Sub btnSend_Click(sender As System.Object, e As System.EventArgs) Handles btnSend.Click
If txtWrite.Text = "" Or txtWrite.Text = Nothing Then Exit Sub
Try
client = New TcpClient(friendIP, 44444)
Dim writer As New StreamWriter(client.GetStream())
txttempmsg.Text = (txtWrite.Text)
writer.Write(myIP + " says: " + txtWrite.Text)
txtRead.Text = (txtRead.Text + myName + " says: " + txttempmsg.Text + vbCrLf)
writer.Flush()
txtWrite.Text = ""
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Can you guys help?