
Problem with my VB.NET telnet client
(((To REPLY to my message, please remove the xyz from my email name:
I simplified my VB.NET telnet client code for troubleshooting and I use MUD
servers for testing it. You can compile and run this code to see the problem
(NOTE: To run it, you need a form1 with a richtextbox1, a textbox1, and two
buttons.).
It has the following problem:
The client connects to all servers and receives their welcome messages just
fine. But on some servers, after I get the welcome message and try to
respond with login text or whatever is requested, the server just sits there
like it never got my message. In the code comments below, I list two MUD
server addresses, one that works correctly and one that fails. I am really
stuck, any ideas would be greatly appreciated. Thanks!
Imports System.Net
Imports System.Net.Sockets
Public Class Form1
Inherits System.Windows.Forms.Form
Dim sock1 As New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
'This is the connect button
Connect()
Button2.Enabled = False
Receive()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'This is the send button
Send()
TextBox1.Text = ""
Receive()
End Sub
Private Function Connect()
'BAD connection example = 206.245.158.190 5000
'GOOD connection example = 63.205.237.177 1912
Dim myIP As IPAddress = IPAddress.Parse("63.205.237.177")
'Parse the port number from the end of the string in the combobox
Dim EPhost As New IPEndPoint(myIP, 1912)
'Catch errors trying to connect to hosts that may not be there
Try
sock1.Connect(EPhost)
Catch ex As Exception
RichTextBox1.Text += ControlChars.CrLf & "ERROR: " & ex.Message
End Try
End Function
Function Receive()
Dim scanstring As String
Dim buffer(32000) As Byte
Dim ttlbytes As Integer
Dim x As Integer
sock1.Blocking = False
For x = 1 To 1000
Try
ttlbytes = sock1.Receive(buffer, buffer.Length, 0)
Catch ex As Exception
'RichTextBox1.Text += "ERROR: " & ex.Message
End Try
'Convert this received message from a byte array to an easier to
use string
If ttlbytes > 0 Then
scanstring = System.Text.Encoding.ASCII.GetString(buffer, 0,
ttlbytes)
RichTextBox1.Text += scanstring
ttlbytes = 0
End If
Next x
End Function
Private Function Send()
'This sub sends a message to the server
Dim i As Integer
Dim b(3200) As Byte
Dim message As String = TextBox1.Text + ControlChars.CrLf
'Take what the user typed and convert it to a byte array so the
socket can send it
For i = 1 To message.Length
b(i) = Asc(Mid(message, i, 1))
Next
'Send the message to the server
Try
sock1.Blocking = True
sock1.Send(b)
Catch ex As Exception
RichTextBox1.Text += ControlChars.CrLf & "ERROR: " & ex.Message
End Try
End Function
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
End Sub
End Class