在本机的2000端口监听客户端连接,然后在页面上显示传送的文件内容;
问题如下:第一次传送是可以显示的;但是不能连续显示第二次传入的内容?
请教高手指点一下,谢谢!
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;namespace TcpReceiver
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Thread thread = new Thread(new ThreadStart(Listen));
thread.Start();
} protected void Listen()
{
try
{
// IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPAddress ipAddress = IPAddress.Any;
Int32 port = 2000;
TcpListener tcpListener = new TcpListener(ipAddress, port);
tcpListener.Start(); //TcpClient tcpClient = tcpListener.AcceptTcpClient(); 这里是同步调用会阻塞后面的请求
tcpListener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), tcpListener); //这里采用异步调用,接收一个连接请求,就开启一个线程来处理请求
}
catch (Exception ex)
{
MessageBox.Show("出错了: \n" + ex.Message, "错误");
}
} protected void AcceptClient(IAsyncResult ar)
{
try
{
TcpListener tcpListener = (TcpListener)ar.AsyncState;
TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
NetworkStream networkStream = tcpClient.GetStream();
StreamReader streamReader = new StreamReader(networkStream);
string content = streamReader.ReadToEnd(); rtbxContent.Invoke(new MethodInvoker(delegate
{
rtbxContent.Text = "";
rtbxContent.Text = content;
})); networkStream.Close();
tcpClient.Close();
//tcpListener.Stop();
}
catch (Exception ex)
{
MessageBox.Show("出错了: \n" + ex.Message, "错误");
}
}
}
}
问题如下:第一次传送是可以显示的;但是不能连续显示第二次传入的内容?
请教高手指点一下,谢谢!
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;namespace TcpReceiver
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Thread thread = new Thread(new ThreadStart(Listen));
thread.Start();
} protected void Listen()
{
try
{
// IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPAddress ipAddress = IPAddress.Any;
Int32 port = 2000;
TcpListener tcpListener = new TcpListener(ipAddress, port);
tcpListener.Start(); //TcpClient tcpClient = tcpListener.AcceptTcpClient(); 这里是同步调用会阻塞后面的请求
tcpListener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), tcpListener); //这里采用异步调用,接收一个连接请求,就开启一个线程来处理请求
}
catch (Exception ex)
{
MessageBox.Show("出错了: \n" + ex.Message, "错误");
}
} protected void AcceptClient(IAsyncResult ar)
{
try
{
TcpListener tcpListener = (TcpListener)ar.AsyncState;
TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
NetworkStream networkStream = tcpClient.GetStream();
StreamReader streamReader = new StreamReader(networkStream);
string content = streamReader.ReadToEnd(); rtbxContent.Invoke(new MethodInvoker(delegate
{
rtbxContent.Text = "";
rtbxContent.Text = content;
})); networkStream.Close();
tcpClient.Close();
//tcpListener.Stop();
}
catch (Exception ex)
{
MessageBox.Show("出错了: \n" + ex.Message, "错误");
}
}
}
}
{
Socket socket = tcplistener.AcceptSocket();
if (socket != null)
StartItem(socket);}
------------
上面这句在protected void AcceptClient(IAsyncResult ar)函数的尾部再加上一句,不过你这样加上效果也不会很好,不如while来的实在
不是很明白;是要对所有的tcpclient执行操作还是要怎么样操作;
我目前的测试程序就是显示最后一个连接发送的数据;
我理解,监听一直开着,有连接请求就会执行protected void AcceptClient(IAsyncResult ar)方法;
谢谢!
你用完了就关闭就行了
我的理解可能是错误的!(90%)
为什么还要while呢?如果用while,那么用while是循环什么对象?该怎么写 ,谢谢!
{
......................
while()
{
TcpListener tcpListener = (TcpListener)ar.AsyncState;
TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
NetworkStream networkStream = tcpClient.GetStream();
StreamReader streamReader = new StreamReader(networkStream);
string content = streamReader.ReadToEnd(); rtbxContent.Invoke(new MethodInvoker(delegate
{
rtbxContent.Text = "";
rtbxContent.Text = content;
})); networkStream.Close();
tcpClient.Close();
}
}
是这样吗?
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Thread thread = new Thread(new ThreadStart(Listen));
thread.Start();
} protected void Listen()
{
try
{
// IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPAddress ipAddress = IPAddress.Any;
Int32 port = 2000;
TcpListener tcpListener = new TcpListener(ipAddress, port);
tcpListener.Start(); //TcpClient tcpClient = tcpListener.AcceptTcpClient(); 这里是同步调用会阻塞后面的请求
tcpListener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), tcpListener); //这里采用异步调用,接收一个连接请求,就开启一个线程来处理请求
}
catch (Exception ex)
{
MessageBox.Show("出错了: \n" + ex.Message, "错误");
}
}
while(true)
{
tcpListener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), tcpListener);
}
TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
while (true)
{。。
----求分
//tcpClient.Close();
//tcpListener1.Stop();这个不能关闭 要去掉
{
tcpListener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), tcpListener);
}这样加了以后,功能倒是可以了;
但是性能大打折扣,接收端不久就outofmemoryException了!
public Form1()
{
InitializeComponent();
Thread thread = new Thread(new ThreadStart(Listen));
thread.Start();
}
TcpListener tcpListener = null;
protected void Listen()
{
try
{
// IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPAddress ipAddress = IPAddress.Any;
Int32 port = 2000;
tcpListener = new TcpListener(ipAddress, port);
tcpListener.Start(); //TcpClient tcpClient = tcpListener.AcceptTcpClient(); 这里是同步调用会阻塞后面的请求
while (true)
{
tcpListener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), tcpListener);
}
//这里采用异步调用,接收一个连接请求,就开启一个线程来处理请求
}
catch (Exception ex)
{
MessageBox.Show("出错了: \n" + ex.Message, "错误");
}
}