【发布时间】:2015-04-16 05:37:02
【问题描述】:
我使用 2 种方式通过连接到 IP 地址的套接字接收数据。
- 同步接收。
- 异步接收。
使用同步接收,我可以在不到 1 秒的时间内从服务器获得所有响应。但是使用异步接收,接收所有响应需要 30 多秒。
代码如下:
class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
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)
{
Console.WriteLine(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.ReceiveTimeout = 500;
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)
{
MessageBox.Show(e.ToString());
}
}
【问题讨论】:
-
你有什么问题?
-
我认为问题很明确(为什么延迟) - 我不是 100% 确定,但你能用你的代码测试
SocketFlags.Partial标志吗? -
我怎样才能像同步方法一样快地接收数据?
-
if (bytes Read > 0) ... else { ... receiveDone.Set(); }看起来也很可疑——你需要一个 0 字节的读取来设置它(这很可能只有在另一端断开连接时才会发生——或者我猜是这样) -
根据我的上一条评论和您的代码,您似乎可以在阅读任何内容后立即设置
receiveDone(您只需要 1 个 ASCII 字符 - 1 个字节) - 那你为什么不@ 987654325@ 就在state.sb.Append(...)之后)?
标签: c# sockets beginreceive