Hello all,
I really am not sure where to put this question. I am trying to use TcpClient on a windows form to communicate with a Cisco router at my college. I have code that works on a C# form just fine. The "identical", as far as I can tell, code for VB does not seem to work. Specifically, it will not display anything to the textbox that I set up on the form. However, if I step through the program in debug mode, with a breakpoint on line 24, as I step through the program, everything works as expected. Any ideas why this is happening?
VB Code (I took out the IP and Port for security reasons obviously)
1 |
Public Class Form1 |
2 |
|
3 |
Dim client As New Net.Sockets.TcpClient() |
4 |
Dim stream As Net.Sockets.NetworkStream |
5 |
|
6 |
Protected Sub GetResponse(ByRef stream As Net.Sockets.NetworkStream) |
7 |
|
8 |
While (stream.DataAvailable) |
9 |
Dim data(1024) As Byte |
10 |
Dim response As String |
11 |
Dim bytes As Int32 |
12 |
bytes = stream.Read(data, 0, bytes) |
13 |
response = System.Text.Encoding.ASCII.GetString(data, 0, bytes) |
14 |
TextBox1.AppendText(response) |
15 |
End While |
16 |
|
17 |
End Sub |
18 |
|
19 |
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load |
20 |
Dim ip As String = 'taken out |
21 |
Dim port As Integer = 'taken out |
22 |
|
23 |
client.Connect(ip, port) |
24 |
stream = client.GetStream() |
25 |
|
26 |
While (stream.DataAvailable) |
27 |
GetResponse(stream) |
28 |
End While |
29 |
|
30 |
End Sub |
31 |
End Class |
Here is the corresponding C# code that works correctly all the time:
1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.ComponentModel; |
4 |
using System.Data; |
5 |
using System.Drawing; |
6 |
using System.Text; |
7 |
using System.Windows.Forms; |
8 |
using System.Net.Sockets; |
9 |
|
10 |
namespace WindowsApplication4 |
11 |
{ |
12 |
public partial class Form1 : Form |
13 |
{ |
14 |
TcpClient client = new TcpClient(); |
15 |
NetworkStream stream; |
16 |
|
17 |
public Form1() |
18 |
{ |
19 |
InitializeComponent(); |
20 |
Application.Idle += new EventHandler(Application_Idle); |
21 |
} |
22 |
|
23 |
void Application_Idle(object sender, EventArgs e) |
24 |
{ |
25 |
GetResponse(); |
26 |
txtCommand.Focus(); |
27 |
} |
28 |
|
29 |
private void Form1_Load(object sender, EventArgs e) |
30 |
{ |
31 |
string ip = "some number here ;-)" |
32 |
int port = a port number here; |
33 |
client.Connect(ip, port); |
34 |
stream = client.GetStream(); |
35 |
while (stream.DataAvailable) |
36 |
{ |
37 |
GetResponse(); |
38 |
} |
39 |
} |
40 |
|
41 |
private void btnSubmit_Click(object sender, EventArgs e) |
42 |
{ |
43 |
SendResponse(); |
44 |
txtCommand.Text = ""; |
45 |
txtCommand.Focus(); |
46 |
} |
47 |
|
48 |
private void SendResponse() |
49 |
{ |
50 |
string command = txtCommand.Text; |
51 |
|
52 |
Byte[] data = System.Text.Encoding.ASCII.GetBytes(command + "\r\n"); |
53 |
|
54 |
foreach (Byte b in data) |
55 |
{ |
56 |
stream.WriteByte(b); |
57 |
} |
58 |
|
59 |
} |
60 |
|
61 |
private void GetResponse() |
62 |
{ |
63 |
while (stream.DataAvailable) |
64 |
{ |
65 |
Byte[] data = new Byte[5112]; |
66 |
String response = String.Empty; |
67 |
Int32 bytes = stream.Read(data, 0, data.Length); |
68 |
|
69 |
response = System.Text.Encoding.ASCII.GetString(data, 0, bytes); |
70 |
|
71 |
txtRouter.AppendText(response); |
72 |
} |
73 |
} |
74 |
} |
75 |
} |
Any help would be appreciated!!
Thanks!