Home > Archive > C# > June 2004 > Continuous TcpListener
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Continuous TcpListener
|
|
| Harrison 2004-06-29, 8:57 pm |
| Hello all,
I thought I would give this newgroup a shot. I am writing an app that
continuously listens on a given local port, and accepts incoming data
and displays in a listbox.
I have a single form that performs the "Listen" and the "Send" via
buttons, however, it only seems to work once. Meaning, that I click
"Listen" and then "Send" and the listener catches the "Hello World" text
and adds it to the listbox. Then, if I hit the "Send" button again,
nothing happens, the listener doesnt catch it.
I would really appreciate any help:
private void Listen_Click(object sender, System.EventArgs e)
{
ThreadStart listener = new
ThreadStart(StartListening);
Thread t = new Thread(listener);
t.Name = "HLPop listener";
t.Start();
}
private TcpListener listener;
private TcpClient c;
NetworkStream stream;
Byte[] bytes = new Byte[256];
string data;
int i;
private void StartListening()
{
listener = new TcpListener(8001);
listener.Start();
while (true )
{
c=listener.AcceptTcpClient();
while(true)
{
// Get a stream object for reading and writing
stream = c.GetStream();
if ((i = stream.Read(bytes, 0,
bytes.Length))!=0)
{
data =
System.Text.Encoding.ASCII.GetString(bytes);
listBox1.Items.Add(data);
}
}
}
}
private Stream mstream;
private void Send_Click(object sender, System.EventArgs e)
{
TcpClient client = new TcpClient();
client.Connect("127.0.0.1", 8001);
mstream = client.GetStream();
if( mstream.CanWrite )
{
StreamWriter w = new StreamWriter(mstream);
w.WriteLine("Hello World!");
w.Flush();
}
mstream.Flush();
client.Close();
}
}
- Harrison
| |
| Harrison 2004-06-30, 9:00 pm |
| Found my problem, execution never made it out of the second "while" loop.
Thanks all.
Harrison wrote:
> Hello all,
>
> I thought I would give this newgroup a shot. I am writing an app that
> continuously listens on a given local port, and accepts incoming data
> and displays in a listbox.
>
> I have a single form that performs the "Listen" and the "Send" via
> buttons, however, it only seems to work once. Meaning, that I click
> "Listen" and then "Send" and the listener catches the "Hello World" text
> and adds it to the listbox. Then, if I hit the "Send" button again,
> nothing happens, the listener doesnt catch it.
>
> I would really appreciate any help:
>
> private void Listen_Click(object sender, System.EventArgs e)
> {
> ThreadStart listener = new
> ThreadStart(StartListening);
> Thread t = new Thread(listener);
> t.Name = "HLPop listener";
> t.Start();
> }
>
> private TcpListener listener;
> private TcpClient c;
> NetworkStream stream;
> Byte[] bytes = new Byte[256];
> string data;
> int i;
>
> private void StartListening()
> {
> listener = new TcpListener(8001);
> listener.Start();
> while (true )
> {
> c=listener.AcceptTcpClient();
> while(true)
> {
> // Get a stream object for reading and writing
> stream = c.GetStream();
> if ((i = stream.Read(bytes, 0,
> bytes.Length))!=0)
> {
> data =
> System.Text.Encoding.ASCII.GetString(bytes);
>
> listBox1.Items.Add(data);
> }
> }
> }
> }
>
> private Stream mstream;
>
> private void Send_Click(object sender, System.EventArgs e)
> {
> TcpClient client = new TcpClient();
> client.Connect("127.0.0.1", 8001);
> mstream = client.GetStream();
> if( mstream.CanWrite )
> {
> StreamWriter w = new StreamWriter(mstream);
> w.WriteLine("Hello World!");
> w.Flush();
> }
> mstream.Flush();
> client.Close();
> }
> }
>
>
> - Harrison
|
|
|
|
|