我的代码在异步通信中必须要等Server端断掉后才能接收到数据,这是为什么?

uheart uheart 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
搞了半天 不知道怎么回事,Server端已经返回数据,但就是接收不到,非要等Server端断开后才能接收到 晕死了
SendCmd为前台ajax调用,Server能接收到我发送的命令.

[AjaxPro.AjaxMethod]
public string SendCmd(string cmd)
{
string str = "";

try
{
// string strCmdPar = "a=1;b=2;";
string strCmdPar = cmd;
PacketObj pk = new PacketObj();
char[] chrCmd = strCmdPar.ToCharArray();
ArrayList alCmdPar = new ArrayList();

foreach(char chr in chrCmd)
{
byte byt = (byte)chr;
byte[] byts = pk.CmdEscape(new byte[1]{byt});
foreach(byte bytTmp in byts)
{
alCmdPar.Add(bytTmp);
// alCmdPar.Add(byt);
}
}

byte[] arrPar = new byte[alCmdPar.Count];
for(int j = 0 ; j < alCmdPar.Count ; j++)
{
arrPar[j] = (byte)alCmdPar[j];
}

byte[] bytData = this.GetCmdPacket(arrPar);
str = SendCommand(bytData);
}
catch(Exception ce)
{
str=ce.Message;
}

return str;
}

#region
// The port number for the remote device.
private const int port = 6205;
private const string ServerIp = "192.168.2.100";

// 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 static PacketObj pc = new PacketObj();



public byte[] GetCmdPacket(byte[] cmdPar)
{

byte[] bytCmd = new byte[4096];
byte bytStatus = 0x10 | 0xe0;
byte[] ClientID = new byte[]{0x31,0x31,0x30,0x31};
byte[] SiteID = new byte[]{0x61,0x31,0x30,0x31};
byte ProtocolCommand = 0x07;

bytCmd = pc.MergerPacket(bytStatus,ClientID,SiteID,ProtocolCommand,cmdPar);

return bytCmd;

}




private string SendCommand(byte[] byteData)
{
try
{



IPAddress ip = IPAddress.Parse(ServerIp);
IPEndPoint remoteEP = new IPEndPoint(ip, port);

Socket client = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);

client.BeginConnect( remoteEP,new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();

Send(client,byteData);
sendDone.WaitOne();

Receive(client);
receiveDone.WaitOne();

// Console.WriteLine("Response received : {0}", response);

client.Shutdown(SocketShutdown.Both);
client.Close();


}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return response;
}




private void ConnectCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket) ar.AsyncState;

client.EndConnect(ar);

// Console.WriteLine("Socket connected to {0}",client.RemoteEndPoint.ToString());

connectDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

private static void Send(Socket client, byte[] byteData)
{
client.BeginSend(byteData,0,pc.PacketLen,0,new AsyncCallback(SendCallback),client);
}

private static void SendCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket) ar.AsyncState;

int bytesSent = client.EndSend(ar);
// Console.WriteLine("Sent {0} bytes to server.", bytesSent);

sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}


private void Receive(Socket client)
{
try
{
StateObject state = new StateObject();
state.workSocket = client;

client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

private void ReceiveCallback( IAsyncResult ar )
{
try
{
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocket;

int bytesRead = client.EndReceive(ar);

if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));

client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReceiveCallback), state);
}
else
{
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}



public 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();
}
#endregion

...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
uheart
C# 111,128 成员 642,536 内容