我已经搜索了一天中的大部分时间来试图解决这个问题,而且我确定我在某个地方缺少一个简单的解决方案。
我正在使用异步套接字客户端连接来监视来自 TCP 连接的传入数据。我已经按照 msdn 示例的建议实现了 ManualResetEvent 和 Callback 方法,但是 Callback 方法无法调用用于将接收到的数据输出到我的 Windows 窗体的方法。如何获取从套接字接收到的数据,并将其发送到表单中的文本框?
我敢肯定,我在某处遗漏了一个简单的技巧。示例代码是为控制台应用程序编写的。如何让表单对来自 Socket 的传入数据做出反应?
更新:
我想你让我走上了正确的道路。我尝试输入代码来使用委托,但我显然不太了解委托是如何工作的,因为它不断抛出以下错误:
非静态字段、方法或属性“APRS_SMS_Gateway.MainForm.SockOutputDelegate”需要对象引用
你能让我靠近一点吗?这是我现在正在尝试处理的 ConnectCallBack 处理程序,但我想从所有 CallBacks 中使用相同的方法(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");
}
}
}