# VB.NET IRC client help



## shuggans (Oct 9, 2012)

Hello all... hoping someone can help me.  When I run this code I get the line I returned confirming the socket is connected to the server, however immediately after the program stops responding and I get the "send error report" dialog.  Any ideas?


```
Imports System.Net.Sockets
Imports System.IO

Public Class console_MAIN
    Dim Socket As System.Net.Sockets.Socket
    Dim StreamIn As System.IO.TextReader
    Dim StreamOut As System.IO.TextWriter

    Private Sub SetServerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetServerToolStripMenuItem.Click
        Console_Server.Show()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub ConnectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectToolStripMenuItem.Click
        StartConnect()
    End Sub

    Public Sub StartConnect()
        Dim ServerAddress As String = My.Settings.ServerAddress
        Dim ServerPort As Integer = My.Settings.ServerPort
        TextBox_MsgIn.AppendText("Initializing connection..." & vbCrLf)

        Dim ipHostInfo As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(ServerAddress)
        Dim EP As New System.Net.IPEndPoint(ipHostInfo.AddressList(0), ServerPort)
        Socket = New System.Net.Sockets.Socket(EP.Address.AddressFamily, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
        Socket.Connect(ServerAddress, ServerPort)

        'send PASS
        Send("PASS " & My.Settings.ServerPassword)
        'send NICK
        Send("NICK " & My.Settings.Nick)
        'send USER
        Send("USER " & My.Settings.Nick & " 0 * :RoX Player")

        Connected()

    End Sub

    Sub Send(ByVal msg As String)
        msg &= vbCr & vbLf
        Dim data() As Byte = System.Text.ASCIIEncoding.UTF8.GetBytes(msg)
        Socket.Send(data, msg.Length, SocketFlags.None)
    End Sub

    Sub Connected()
        Dim ConfirmConnect As Boolean = False
        Dim PasswordSet As Boolean = False
        If My.Settings.ServerPassword <> "" Then
            PasswordSet = True
        End If

        While Socket.Connected = True
            If ConfirmConnect = False Then
                TextBox_MsgIn.AppendText("Connected to " & My.Settings.ServerAddress & " on port " & My.Settings.ServerPort & " using password: " & PasswordSet.ToString)
                ConfirmConnect = True
            End If
            Dim mail As String = recv()
            If mail <> Nothing Then
                StreamOut.WriteLine(mail)
                TextBox_MsgIn.AppendText(mail & vbCrLf)
            End If
        End While

    End Sub

    Function recv() As String
        Dim data(4096) As Byte
        Socket.Receive(data, 4096, SocketFlags.None)
        Dim mail As String = System.Text.ASCIIEncoding.UTF8.GetString(data)
        If mail.Contains(" ") Then
            If mail.Substring(0, 4) = "PING" Then
                Dim pserv As String = mail.Substring(mail.IndexOf(":"), mail.Length - mail.IndexOf(":"))
                pserv = pserv.TrimEnd(Chr(0))
                mail = "PING from " & pserv & vbNewLine & "PONG to " & pserv
                send("PONG " & pserv)
            ElseIf mail.Substring(mail.IndexOf(" ") + 1, 7) = "PRIVMSG" Then
                Dim tmparr() As String = Nothing
                Dim rnick As String
                Dim rmsg As String
                mail = mail.Remove(0, 1)
                tmparr = mail.Split("!")
                rnick = tmparr(0)
                tmparr = mail.Split(":")
                rmsg = tmparr(1)
                mail = "msg: " & rnick & ">" & rmsg
            End If
        End If
        mail = mail.TrimEnd(Chr(0))
        mail = mail.Remove(mail.LastIndexOf(vbLf), 1)
        mail = mail.Remove(mail.LastIndexOf(vbCr), 1)
        Return mail
    End Function


    Private Sub Button_send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_send.Click
        Send(TextBox_MsgOut.Text)
        TextBox_MsgOut.Clear()
    End Sub

    Private Sub Button_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Connect.Click
        StartConnect()
    End Sub

    Private Sub console_MAIN_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label_CurrentServer.Text = My.Settings.ServerAddress
    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub
End Class
```


----------



## shuggans (Oct 9, 2012)

*Correction*

Found that issue, now it hangs after the MOTD message is delivered.

Here si new code:

```
Imports System.Net.Sockets
Imports System.IO

Public Class console_MAIN
    Dim Socket As System.Net.Sockets.Socket
    Dim StreamIn As System.IO.TextReader
    Dim StreamOut As System.IO.TextWriter

    Private Sub SetServerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetServerToolStripMenuItem.Click
        Console_Server.Show()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub ConnectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectToolStripMenuItem.Click
        StartConnect()
    End Sub

    Public Sub StartConnect()
        Dim ServerAddress As String = My.Settings.ServerAddress
        Dim ServerPort As Integer = My.Settings.ServerPort
        TextBox_MsgIn.AppendText("Initializing connection..." & vbCrLf)

        Dim ipHostInfo As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(ServerAddress)
        Dim EP As New System.Net.IPEndPoint(ipHostInfo.AddressList(0), ServerPort)
        Socket = New System.Net.Sockets.Socket(EP.Address.AddressFamily, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
        Socket.Connect(ServerAddress, ServerPort)

        'send PASS
        Send("PASS " & My.Settings.ServerPassword)
        'send NICK
        Send("NICK " & My.Settings.Nick)
        'send USER
        Send("USER " & My.Settings.Nick & " 0 * :RoX Player")
        'send MODE
        Send("MODE " & My.Settings.Nick & " +h")
        'send join
        Send("JOIN " & My.Settings.Channel)
        Connected()

    End Sub

    Sub Send(ByVal msg As String)
        msg &= vbCr & vbLf
        Dim data() As Byte = System.Text.ASCIIEncoding.UTF8.GetBytes(msg)
        Socket.Send(data, msg.Length, SocketFlags.None)
    End Sub

    Sub Connected()
        Application.DoEvents()
        Dim ConfirmConnect As Boolean = False
        Dim PasswordSet As Boolean = False
        If My.Settings.ServerPassword <> "" Then
            PasswordSet = True
        End If

        Dim mail As String
        Try
            While Socket.Connected = True
                If ConfirmConnect = False Then
                    TextBox_MsgIn.AppendText("Connected to " & My.Settings.ServerAddress & " on port " & My.Settings.ServerPort & " using password: " & PasswordSet.ToString)
                    ConfirmConnect = True
                End If
                mail = recv()
                If mail <> "" Then
                    If mail <> Nothing Then

                        ' StreamOut.WriteLine(mail)
                        TextBox_MsgIn.AppendText(mail & vbCrLf)
                    End If
                Else

                End If
            End While
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

    Function recv() As String
        Dim data(4096) As Byte
        Socket.Receive(data, 4096, SocketFlags.None)
        Dim mail As String = System.Text.ASCIIEncoding.UTF8.GetString(data)
        If mail.Contains(" ") Then
            If mail.Substring(0, 4) = "PING" Then
                Dim pserv As String = mail.Substring(mail.IndexOf(":"), mail.Length - mail.IndexOf(":"))
                pserv = pserv.TrimEnd(Chr(0))
                mail = "PING from " & pserv & vbNewLine & "PONG to " & pserv
                send("PONG " & pserv)
            ElseIf mail.Substring(mail.IndexOf(" ") + 1, 7) = "PRIVMSG" Then
                Dim tmparr() As String = Nothing
                Dim rnick As String
                Dim rmsg As String
                mail = mail.Remove(0, 1)
                tmparr = mail.Split("!")
                rnick = tmparr(0)
                tmparr = mail.Split(":")
                rmsg = tmparr(1)
                mail = "msg: " & rnick & ">" & rmsg
            ElseIf mail.Contains(":") Then
            End If
        End If
        mail = mail.TrimEnd(Chr(0))
        mail = mail.Remove(mail.LastIndexOf(vbLf), 1)
        mail = mail.Remove(mail.LastIndexOf(vbCr), 1)
        Return mail
    End Function


    Private Sub Button_send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_send.Click
        Send(TextBox_MsgOut.Text)
        TextBox_MsgOut.Clear()
    End Sub

    Private Sub Button_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Connect.Click
        StartConnect()
    End Sub

    Private Sub console_MAIN_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label_CurrentServer.Text = My.Settings.ServerAddress
    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub
End Class
```


----------



## shuggans (Oct 9, 2012)

Okay... so I tested the code above, letting the program hagn and sending a private massage to the nickname the client was running under... and boom, the program stops hanging and displays the message.

So Im guessing this a thread issue due to the loop running on the main thread?


----------



## FordGT90Concept (Oct 9, 2012)

Yes, you'd have to put a listener on a separate thread that notifies the main thread of changes.


----------

