Trying to at least set up connection

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
Hi,

I'm trying to make an admin console/bot for my own IRC server, but I can't figure out how to get this to connect to my server. I need to connect to my local IP address (192.168.1.97). I tried some other things, but I couldn't figure out how to convert that IP to a long IP. I found that the TCPClient class can use the hostname as an argument, so I'm trying to use the hostname of my computer (Dorothy). Could someone please look at this and tell me what's wrong? It finally loads the form without errors, but I'm not so sure it's connecting. I'll also need help with how to send commands to the server once I figure this out. Thanks. Here's the code
VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Client [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Net.Sockets.TcpClient()
Client.Connect([/SIZE][SIZE=2][COLOR=#800000]"Dorothy"[/COLOR][/SIZE][SIZE=2], 6667)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Stream [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Net.Sockets.NetworkStream = Client.GetStream
Sock_Connect()
[/SIZE][SIZE=2][COLOR=#008000]'Stream.Write("NICK " & "VB_Bot" & vbCrLf)
[/COLOR][/SIZE][SIZE=2]Client.GetStream()
[/SIZE][SIZE=2][COLOR=#008000]'Client.Send("NICK " & "VB_Bot" & vbCrLf)
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Client.Send("USER " & "VB_Bot" & " " & "Dorothy" & " " & _
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'UCase("Dorothy" & ":" & 1903 & "/0") & " :" & "VB_Bot" & vbCrLf)
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Sock_Connect()
MessageBox.Show([/SIZE][SIZE=2][COLOR=#800000]"Connected"[/COLOR][/SIZE][SIZE=2])

[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
You can ignore all the commented lines. I was just testing some things.
 
Wow, thank you! I tried this on my own IRC server, and it works great. I'll have to examine the code to see how to add all the admin buttons I want. I'll also want to add channel and nick listboxes, so I'll have to see how to parse the responses to the commands to get that information. My idea was to have a listbox of all the channels on the server and when I click each one, the nick listbox will update with the names of the people in that channel. Looking at your code, I'm really curious as to why it works when you aren't sending a vbCrLf at the end of each command you send. I had thought that I determined that was the problem for why I couldn't connect. Thanks again for that code and your help. I do really appreciate it.
 
Blake81 said:
I'm really curious as to why it works when you aren't sending a vbCrLf at the end of each command you send.
I let the NetworkStream wrap into a StreamReader and a StreamWriter, then read/write through their methods ReadLine and WriteLine
 
Thanks. I've been examining your code to see how it works, and I really appreciate the commenting and the select statements you added so I know where to put most of the bot commands. That program works pretty well, but it does glitch a bit on the connection sometimes, but I think that's just because IRC expects a crlf at the end of each line you send it, which was the problem I faced when I couldn't get the server to send me a "Ping". Thanks again for your help, and especially for that program to give me a really good start on this project.
 
Quick question about that code

JohnH, thanks again for that code. I'm currently taking your code and putting it into my project, while also adding the functionality to other buttons that I have in my form, and studying the code to fully understand it. I have one quick question. In your timer sub, you have this line:

display(data)
'just to see what everything is...

When I put the code in, it says "Name display is not declared", but I have all the code pasted in just like you have it, except for changing some of the strings to hard code the bot's name. I don't see where you have display declared, or why it would work on your code and not in mine. Thanks. If you'd like to see a screenshot of how this project is going to look, you can see it here:

http://blakenet.myftp.org/AdminConsoleDev.html
 
About feedback:

- Streamwriter does append the carriage return + newline sequence when writeline method is called, which conforms to the IRC protocol specification.

- The display(string) method is in the project...

- About the responsiveness/glitch I found out that too, very bad indeed. I've searched high and low. It's got nothing to do with the use of timer. I only find vague references to the DataAvailable property of NetworkStream, that its functionality is not exactly precise regarding if there is perhaps a line or two of data lurking in the stream or not. So another way of doing things when receiving data, I have found using a receive thread that do infinite loops to the readline method successful and very responsive. ReadLine is so called 'blocking' and will not continue when called until a line can be read, the processing thread goes into a wait state (and process will not run at 100% cpu), a 'sleeping' thread is not a problem asynchronous, other parts of application remains responsive (just remember to stop the receiver when appropriate). Doing stuff asynchronous also benefits the handling of recieved data at another thread, but also means that any interacting with objects created on other threads, usually UI controls on main application thread, have to be invoked through delegates to avoid cross-thread violations. The threaded version of the same project as above is attached here as zip.
 

Attachments

  • vbnet20-IrcBot-basic-thread.zip
    13.5 KB · Views: 44
Back
Top