我正在用C#为我的Unity应用程序创建一个服务器套接字。我已经从下面的链接创建了异步服务器套接字 Asynchronous Server Socket
使用我可以成功地与客户端连接并从客户端接收数据。但是,在接收到少量数据之后,即使客户端正在发送数据,套接字也无法接收数据(我正在同一台计算机上测试客户端和服务器)。
在try catch中也没有引发异常。因此,我无法确定根本原因。
请在下面找到我的代码
public class NetworkStateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public List<byte> receivedBytes = new List<byte>();
}
private readonly ManualResetEvent allDone = new ManualResetEvent(false);
private readonly ManualResetEvent receiveDone = new ManualResetEvent(false);
private readonly string receiveDelimitter = "<EOF>";
socketThread = new Thread(new ThreadStart(StartSocketServer));
socketThread.Priority = System.Threading.ThreadPriority.BelowNormal;
socketThread.IsBackground = true;
socketThread.Start();
protected void Receive(Socket socket)
{
ReceiveData(socket);
receiveDone.WaitOne();
}
private void ReceiveData(Socket socket)
{
try
{
// Create the state object.
NetworkStateObject state = new NetworkStateObject();
state.workSocket = socket;
// Begin receiving the data from the remote device.
socket.BeginReceive(state.buffer, 0, NetworkStateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
private void ReceiveCallback(IAsyncResult ar)
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
NetworkStateObject state = (NetworkStateObject)ar.AsyncState;
Socket socket = state.workSocket;
// Read data from the remote device.
int bytesRead = socket.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));
var bytesArray = state.buffer;
if (bytesRead < NetworkStateObject.BufferSize)
{
Array.Resize(ref bytesArray, bytesRead);
}
var bytesList = new List<byte>(bytesArray);
state.receivedBytes.AddRange(bytesList);
var receivedBytes = state.receivedBytes;
var bytesCount = receivedBytes.Count;
if (receivedBytes.Count > 1)
{
var receivedString = Encoding.ASCII.GetString(receivedBytes.ToArray(), 0, bytesCount);
if (receivedString.IndexOf(receiveDelimitter, StringComparison.CurrentCulture) > -1)
{
var message = receivedString.Replace(receiveDelimitter, String.Empty);
message = Regex.Unescape(message);
socketBaseDelegate.ReceivedMessage(message);
state.receivedBytes.Clear();
receiveDone.Set();
}
else
{
// Get the rest of the data.
socket.BeginReceive(state.buffer, 0, NetworkStateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
}
}
else
{
receiveDone.Set();
}
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
public void ReceivedMessage(string data)
{
socketInputParser.Parse(data);
asynchronousSocketListener.ReceiveMessage();
}
在上面的代码中,一段时间后未触发ReceiveCallback。 即使客户端发送数据。
答案 0 :(得分:0)
else
{
// Get the rest of the data.
socket.BeginReceive(state.buffer, 0, NetworkStateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
这部分不应该是有条件的。接收回调完成后,您应该始终开始再次接收。当然,除非连接终止。