Stuck at TcpListener.AcceptTcpClient

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hey guys, im trying to make an twitch.tv chat bot but theres something wrong when i connect.

As said, its getting stuck at "identListener.AcceptTcpClient" (line 80 i believe) and i have no idea why..
Heres the code:
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.IO


Module SVIPModule
Public Sub Main()
Console.Title = "SVIP BOT Alpha Version"
Dim foo As New My_IRC("irc.twitch.tv", "#iamimago", "svipbot", 6667, "MYPASSWORDWHICHYOUWONTGET", False)
foo.Connect()

End Sub

Public Class My_IRC
Private _sServer As String = String.Empty -- IRC server name
Private _sChannel As String = String.Empty -- the channel you want to join (prefex with #)
Private _sNickName As String = String.Empty -- the nick name you want show up in the side bar
Private _sPassword As String = String.Empty -- the password?
Private _lPort As Int32 = 6667 -- the port to connect to. Default is 6667
Private _bInvisible As Boolean = False -- shows up as an invisible user. Still working on this.
Private _sRealName As String = "svipbot" -- More naming
Private _sUserName As String = "svipboot" -- Unique name so of the IRC network has a unique handle to you regardless of the nickname.

Private _tcpclientConnection As TcpClient = Nothing -- main connection to the IRC network.
Private _networkStream As NetworkStream = Nothing -- break that connection down to a network stream.
Private _streamWriter As StreamWriter = Nothing -- provide a convenient access to writing commands.
Private _streamReader As StreamReader = Nothing -- provide a convenient access to reading commands.

Public Sub New(ByVal server As String, ByVal channel As String, ByVal nickname As String, ByVal port As Int32, ByVal password As String, ByVal invisible As Boolean)
_sServer = server
_sChannel = channel
_sNickName = nickname
_lPort = port
_sPassword = password
_bInvisible = invisible
End Sub

Public Sub Connect()

-- IDENT explained:
-- -- When connecting to the IRC server they will send a response to your 113 port.
-- -- It wants your user name and a response code back. If you dont some servers
-- -- wont let you in or will boot you. Once verified it drastically speeds up
-- -- the connecting time.
-- -- -- http://en.wikipedia.org/wiki/Ident
-- Heads up - when sending a command you need to flush the writer each time. Thats key.

Dim sIsInvisible As String = String.Empty
Dim sCommand As String = String.Empty -- commands to process from the room.

-- objects used for the IDENT response.
Dim identListener As TcpListener = Nothing
Dim identClient As TcpClient = Nothing
Dim identNetworkStream As NetworkStream = Nothing
Dim identStreamReader As StreamReader = Nothing
Dim identStreamWriter As StreamWriter = Nothing
Dim identResponseString As String = String.Empty

Try
-- Start the main connection to the IRC server.
Console.WriteLine("**Creating Connection**")
_tcpclientConnection = New TcpClient(_sServer, _lPort)
_networkStream = _tcpclientConnection.GetStream
_streamReader = New StreamReader(_networkStream)
_streamWriter = New StreamWriter(_networkStream)


-- Send in your information
Console.WriteLine("**Setting up name**")
_streamWriter.WriteLine(String.Format("USER {0} {1} * :{2}", _sUserName, sIsInvisible, _sRealName))
Console.WriteLine("**Setting up password**")
_streamWriter.WriteLine(String.Format("PASS {0}", _sPassword))
_streamWriter.Flush()

-- Create your nickname.
Console.WriteLine("**Setting Nickname**")
_streamWriter.WriteLine(String.Format(String.Format("NICK {0}", _sNickName)))
_streamWriter.Flush()

-- Tell the server you want to connect to a specific room.
Console.WriteLine("**Joining Room**")
_streamWriter.WriteLine(String.Format("JOIN {0}", _sChannel))
_streamWriter.Flush()

-- By now the IDENT should be sent to your port 113. Listen to it, grab the text,
-- and send a response.
-- Idents are usually #### , ####
-- That is four digits, a space, a comma, and four more digits. You need to send
-- this back with your user name you connected with and your system.
identListener = New TcpListener(IPAddress.Any, 113)
identListener.Start()
identClient = identListener.AcceptTcpClient
identListener.Stop()
Console.WriteLine("ident connection?")
identNetworkStream = identClient.GetStream
identStreamReader = New StreamReader(identNetworkStream)
identResponseString = identStreamReader.ReadLine
Console.WriteLine("ident got: " + identResponseString)
identStreamWriter = New StreamWriter(identNetworkStream)
-- The general format for the IDENT response. You can use UNIX, WINDOWS VISTA, WINDOWS XP, or what ever your system is.
identStreamWriter.WriteLine(String.Format("{0} : USERID : WINDOWS 7 : {1}", identResponseString, _sUserName))
identStreamWriter.Flush()

While True
sCommand = _streamReader.ReadLine
Console.WriteLine(sCommand)

Brain(sCommand)

End While

Catch ex As Exception
-- Any exception quits the bot gracefully.
Console.WriteLine("Error in Connecting. " + ex.Message)

Console.ReadKey()
_streamWriter.WriteLine("QUIT")
_streamWriter.Flush()
Finally
-- close your connections
_streamReader.Dispose()
_streamWriter.Dispose()
_networkStream.Dispose()
End Try

End Sub

Public Sub Brain(ByVal _sCommand As String)
Dim sCommand As String
sCommand = _sCommand

If sCommand = ":jtv MODE #iamimago +o svipbot" Then
Console.WriteLine(" ")
Console.WriteLine(" ")
Console.WriteLine("***CONNECTED AND READY TO MODERATE***")
Console.WriteLine("-------------------------------------")
End If
Dim sCommandParts(sCommand.Split(" ").Length) As String
sCommandParts = sCommand.Split(" ")

-- Occasionally the IRC server will ping the app. If it doesnt respond in an
-- appropriate amount of time the connection is closed.
-- How does one respond to a ping, but with a pong! (and the hash it sends)
If sCommandParts(0) = "PING" Then
Dim sPing As String = String.Empty
For i As Int32 = 1 To sCommandParts.Length - 1
sPing += sCommandParts(i) + " "
Next
_streamWriter.WriteLine("PONG " + sPing)
_streamWriter.Flush()
Console.WriteLine("PONG " + sPing)
End If
-- With my jank split command we want to look for specific commands sent and react to them!
-- In theory this should be dumped to a method, but for this small tutorial you can see them here.
-- Also any user can input this. If you want to respond to commands from you only you would
-- have to extend the program to look for your non-bot-id in the sCommandParts(0)
If sCommandParts.Length >= 4 Then

If sCommandParts(3).StartsWith(":?") Then
Dim sMessage As String = "Someone is bullying me :("
Dim sOut As String = String.Empty

If (sCommandParts(3) = (":?Hello") And sCommandParts(4) = ("SVIP") Or
sCommandParts(3) = (":?Hello") And sCommandParts(4) = ("svip") Or
sCommandParts(3) = (":?Hello") And sCommandParts(4) = ("Svip") Or
sCommandParts(3) = (":?Hey") And sCommandParts(4) = ("SVIP") Or
sCommandParts(3) = (":?Hey") And sCommandParts(4) = ("svip") Or
sCommandParts(3) = (":?Hey") And sCommandParts(4) = ("Svip")) Then
Dim vNameLong As String
Dim vNameCase As String = String.Empty
vNameLong = sCommandParts(0)
Dim vNameShort(vNameLong.Split("!").Length) As String
vNameShort = vNameLong.Split("!")
vNameCase = vNameShort(0)
vNameCase = vNameCase.Substring(1, vNameCase.Length - 1)
vNameCase = StrConv(vNameCase, VbStrConv.ProperCase)

sMessage = "Why hello there, " + vNameCase + "!"
End If
If (sCommandParts(3) = (":?Faggot") And sCommandParts(4) = ("****")) Then
Dim vNameLong As String
Dim vNameCase As String = String.Empty
vNameLong = sCommandParts(0)
Dim vNameShort(vNameLong.Split("!").Length) As String
vNameShort = vNameLong.Split("!")
vNameCase = vNameShort(0)
vNameCase = vNameCase.Substring(1, vNameCase.Length - 1)
vNameCase = StrConv(vNameCase, VbStrConv.ProperCase)

sMessage = "Eat **** and die, " + vNameCase + "!"
End If


sOut = String.Format("PRIVMSG {0} : {1}", _sChannel, sMessage)
Console.WriteLine("**WRITING** " + sOut)
_streamWriter.WriteLine(sOut)
_streamWriter.Flush()
End If
End If




-- If you dont quit the bot correctly the connection will be active until a ping/pong is failed.
-- Even if your programming isnt running!
-- To stop that heres a command to have the bot quit!
If sCommandParts(3).Contains(":!Q") Then
Stop
Console.ReadKey()
_streamWriter.WriteLine("QUIT")
_streamWriter.Flush()
Exit Sub
End If
End Sub


End Class
End Module
Any help would be highly appreciated! Im doing this for work (kind of) and its rather important for me to get it going.

Also, Ive taken the code from a guide and not written it myself since i do not have the knowledge to do so, I know this is looked down upon but please be kind.

View the full article
 
Back
Top