异步客户端与表单交互
问题描述:
我已经搜索了一天中最好的部分,试图弄清楚这一点,我确定我错过了某个简单的解决方案。异步客户端与表单交互
我正在使用异步套接字客户端连接来监视来自TCP连接的传入数据。我已经按照msdn示例的建议实施了ManualResetEvent和Callback方法,但Callback方法无法调用用于将接收到的数据输出到Windows窗体的方法。如何从套接字接收数据并将其发送到表单中的文本框?
我确定有一个简单的伎俩,我错过了某个地方。示例代码是为控制台应用程序编写的。我如何使表单对来自Socket的传入数据做出反应?
更新:
我觉得你让我走上正轨。我试着使用代码来使用代码,但我显然不太了解代表的工作方式,因为它一直抛出以下错误:
非静态字段,方法或属性' APRS_SMS_Gateway.MainForm.SockOutputDelegate'
你能让我靠近吗?这是ConnectCallBack处理程序,我现在正在尝试工作,但我想使用所有CallBack中的同一个方法(SockOutput)。
public partial class MainForm : Form
{
private AutoResetEvent receiveNow;
public delegate void SockOutputDelegatetype(string text); // Define delegate type
public SockOutputDelegatetype SockOutputDelegate;
// The port number for the remote device.
private const int port = 14580;
// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);
// The response from the remote device.
private static String response = String.Empty;
public MainForm()
{
InitializeComponent();
SockOutputDelegate = new SockOutputDelegatetype(SockOutput);
}
private void Form1_Load(object sender, EventArgs e)
{
CommSetting.APRSServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//StartClient();
}
private void StartClient()
{
try
{
IPHostEntry ipHostInfo = Dns.GetHostEntry("rotate.aprs.net");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
//Socket APRSServer = new Socket(AddressFamily.InterNetwork,
// SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
CommSetting.APRSServer.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), CommSetting.APRSServer);
connectDone.WaitOne();
//Show the connect string from the host
Receive(CommSetting.APRSServer);
//receiveDone.WaitOne();
//Show the connection response
//SockOutput(response);
}
catch (Exception e)
{
SockOutput(e.ToString());
}
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket APRSServer = (Socket)ar.AsyncState;
// Complete the connection.
APRSServer.EndConnect(ar);
// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception e)
{
MainForm.Invoke(MainForm.SockOutputDelegate, e.ToString());
}
}
private static void Receive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client;
// Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
//throw new ApplicationException(e.ToString());
response = e.ToString();
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
}
catch (Exception e)
{
response = e.ToString();
}
}
void SockOutput(string text)
{
txtSockLog.AppendText(text);
txtSockLog.AppendText("\r\n");
}
}
}
答
这听起来像你的插座一侧工作正常,它只是更新是造成问题的winform。你可以在这里找到你的答案Update WinForm Controls from another thread _and_ class。
由于您的委托需要声明为静态,即静态void SockOutput(string text),您将得到异常。 – MarcF 2013-03-21 10:44:15
@ user2192887尝试删除'static'并尝试。 – 2013-03-21 11:11:16
@MarcF我尝试修改代码来声明委托为静态的,但这并没有帮助。 – 2013-03-21 14:17:16