客户端向服务器发送命令,服务器发回数据,怎样才能让客户端收完数据后,继续执行程序
1 classSocket.SendToServer(classSocket.s, "dsXZ", "Abcd");//向服备器发送命令
2 MessageBox.Show("ok");//停一下,让数据传回来,没有这句,下面出错,有的话,能正确回来
3 ds = classSocketSerialize.DeSerialize(classSocket.ReceiveDate(classSocket.s)); //接收数据
// 现在请教,怎样在执行第一句后,服务器的数据全部到达之后,才执行第3句。
public static byte[] ReceiveDate(Socket socket)
{
string CmdCode = "";
int InfoLen = 0;
try
{
Byte[] Head = new byte[12];
socket.Receive(Head, Head.Length, SocketFlags.None);
string getInfo = System.Text.Encoding.UTF8.GetString(Head);
CmdCode =getInfo.Substring(0, 4).Trim('A');
try
{
InfoLen = Convert.ToInt32(getInfo.Substring(4, 8).Trim('A'));
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
// return null;
}
Byte[] Boby = new byte[InfoLen];
socket.Receive(Boby, Boby.Length, SocketFlags.None);
/* int i = 0;
while (true)
{
int Len = 0;
if (InfoLen > 1024)
Len=1024;
else
Len=InfoLen;
Byte[] boby = new byte[Len];
int bLen = socket.Receive(boby, boby.Length, SocketFlags.None);
boby.CopyTo(Boby, i);
i = i + bLen;
InfoLen = InfoLen - 1024;
if (InfoLen<=0)
break;
}
*/
return Boby;
}
catch (SocketException ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
------解决方案--------------------客户端也开个端口监听啊,如果有回发数据,就开始发送
------解决方案--------------------你那个类内部的sock传输是不是异步的?