I've been working on this same thing for quite some time now. I'm trying to make an admin console to manage my IRC server, but I can't get past the beginning stages of establishing and maintaining a connection. I finally copied the code out of the project and made a new one with just a textbox, trying to just dump every bit of data I'm sent into that box, but even that isn't doing anything now. I've looked into IRC communication, so I know I'm using the correct syntax. Can someone look at this and tell me why I'm not seeing anything in my textbox In the project I copied this out of, I was getting one messagebox, which was good, but a little odd, because I know that in that stage of the connection, I should be getting two notices from the server simultaneously. Thanks for any help.
Imports
System.TextImports
System.NetImports
System.Net.SocketsPublic
Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Server As TcpClient = New TcpClient("192.168.1.97", 6667) Dim Nickdata() As Byte = New Byte() {} Dim Userdata() As Byte = New Byte() {} Dim JoinData() As Byte = New Byte() {} Dim Nick As String = "NICK VB_Bot" Dim User As String = "USER VB_Bot Dorothy braab.myftp.org VB_Bot" Dim Join As String = "JOIN #chat" Dim PongReply As String = "PONG :" Try While Server.Connected Dim ns As NetworkStream = Server.GetStream Dim bytes(Server.ReceiveBufferSize) As ByteNickdata = Encoding.ASCII.GetBytes(Nick.ToCharArray)
Userdata = Encoding.ASCII.GetBytes(User.ToCharArray)
JoinData = Encoding.ASCII.GetBytes(Join.ToCharArray)
ns.Write(Nickdata, 0, Nickdata.Length)
ns.Write(Userdata, 0, Userdata.Length)
ns.Write(JoinData, 0, JoinData.Length)
Dim returndata As String = Encoding.ASCII.GetString(bytes)TextBox1.Text = TextBox1.Text & returndata & Environment.NewLine
End WhileCatch
ex As Exception End Try End SubEnd
Class
Can't get a TCPClient Connection working
Computing student
Terry Hutt
Dim Server As TcpClient = New TcpClient("192.168.1.97", 6667)
.....
For the server side of a connection you should be using the TCPListener class instead of TCPClient. Please see: http://msdn2.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx
Sergio G
Blake
I would love to help you out more but you need to learn more about basic network programming. I recommend
http://www.codeproject.com/csharp/XYNetSocket.asp as a starter article
You may also want to read
http://www.codeproject.com/csharp/XYNetSocket.asp
http://www.amazon.co.uk/gp/reader/1555583156/ref=sib_dp_pt/203-1147299-6707919#reader-link
Robert Bigelow
maheshbabui
There are a bunch of things wrong with this code.
The following things are wrong with the code
1. There is no need to get the network stream in a loop.
Typically you connect, get the network stream, do reads and writes and then close the network stream.
2. You are not posting a receive or READ on the network stream. if you don't you will not get any data.
3. You don't get strings from network stream, You get bytes and then you need to convert them to strings
The following things are not required
4. There is no need to declare byte array and initialize them to empty arrays,
since you are overwriting them anyway later
5. Even if you allocate a receive buffer size array you still need to know
exactly how many bytes are read before you can process them, NetworkStream.Read should return the data to you and the number of bytes actually read.
AvinashA
I wrote a mIRC script to use sockets to connect to my server and I got it to work from there. I'm not sure if this is why I can't get any further in VB, but for one thing, I'll need to send a crlf at the end of every line I write to the stream. I came up with this, but I'm not sure if this is the correct way:
ns.Write(myNickBuffer, 0, myNickBuffer.Length & vbCrLf)
Larry McCay
Also, what version of the framework are you using If you are using v2.0 of the framework, please get a trace log of the app when running using the information you find here: http://blogs.msdn.com/dgorti/archive/2005/09/18/471003.aspx
Ralphxyz
Thanks. I did want to ask if this would work. I have server declared like this:
Dim
Server As TcpClient = New TcpClient("192.168.1.97", 6667)Would this next sub work for what I want
Private
Sub Server_Available(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChangedMessageBox.Show(
"Data available") End SubI haven't moved the relevant code to that sub, but I put that messagebox there and ran it, and when the connection started, where I would have received the first data from the connection, I did get the messagebox. It's probably not a good practice to put a lot of code in one sub, but I'm just learning. Could I use that sub to check all the incoming data to decide what to do with it
sridhar reddy mitapalli
ericsstoll
On the TCPListener you need to post a receive.
You are thinking more in terms of a winsock control, with
friendly events and stuff which we are currently working on for afuture version
zkent
Thanks, I did get it working. I just needed to write a vbcrlf at the end of each line. Otherwise, the code worked fine. My only other question for now is, I've been looking online at socket programming, and found something very convenient, except that the code was for VB 6. It used the DataArrival method for a socket to know when there was data to be read. In my case, I'm using a TCPListener. Is there a similar method for this, or how should I go about writing this to know when there is data to be read Thanks.
Nikas1
M-Studios
Hi, thank you for your help. I looked at the NetworkStream.Read method, and found an example that I could use. However, I'm still only getting this:
:braab.myftp.org NOTICE AUTH :*** Found your hostname (cached)
Like I said before, I know from connecting with mIRC that I should be getting two server notices, and probably a ping. I've looked at the RFC for IRC and my strings are in the correct format, but it's almost like they're not being written. Can you see anything in this code that would explain why I don't see more from the server or why things aren't being written to the network stream I even tried writing "junk" data to the stream to see if it sent me an error message instead, but I still got that first notice. Thanks for any help.
[code]
Imports
System.TextImports
System.NetImports
System.Net.SocketsPublic
Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Nickdata() As Byte = New Byte() {} Dim Userdata() As Byte = New Byte() {} Dim JoinData() As Byte = New Byte() {} Dim JunkData() As Byte = New Byte() {} Dim Nick As String = "NICK VB_Bot" Dim User As String = "USER VB_Bot Dorothy braab.myftp.org VB_Bot" Dim Junk As String = "hkfhfjkdhkf" Dim Join As String = "JOIN #chat" Dim PongReply As String = "PONG :"JunkData = Encoding.ASCII.GetBytes(Junk.ToCharArray)
Nickdata = Encoding.ASCII.GetBytes(Nick.ToCharArray)
Userdata = Encoding.ASCII.GetBytes(User.ToCharArray)
JoinData = Encoding.ASCII.GetBytes(Join.ToCharArray)
Dim Server As TcpClient = New TcpClient("192.168.1.97", 6667) Dim ns As NetworkStream = Server.GetStreamns.Write(JunkData, 0, JunkData.Length)
Dim bytes(Server.ReceiveBufferSize) As Bytens.Write(Nickdata, 0, Nickdata.Length)
ns.Write(Userdata, 0, Userdata.Length)
ns.Write(JoinData, 0, JoinData.Length)
If ns.CanRead Thenns.Write(Nickdata, 0, Nickdata.Length)
ns.Write(Userdata, 0, Userdata.Length)
ns.Write(JoinData, 0, JoinData.Length)
Dim myReadBuffer(1024) As Byte Dim myCompleteMessage As [String] = "" Dim numberOfBytesRead As Integer = 0 ' Incoming message may be larger than the buffer size. DonumberOfBytesRead = ns.Read(myReadBuffer, 0, myReadBuffer.Length)
myCompleteMessage = [String].Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
Loop While ns.DataAvailable ' Print out the received message to the console.TextBox1.Text =
"You received the following message : " + myCompleteMessage ElseConsole.WriteLine(
"Sorry. You cannot read from this NetworkStream.") End If End SubEnd
Class[/code]
I looked at this again, and since I'm new to this, I'm not sure if I am converting the data to byte form before I write it. Could someone show me the line for converting my string to byte format so it's ready to be written Thank you very much for being patient and helping me.