我在使用socket传输文件的时候,
private void Send(Socket Client,byte[] bytes)//Socket Client,byte[] bytes)
{
             string content=Encoding.UTF8.GetString(SendByte);
if((Client!=null)&&(Client.Connected)&&(!SocketBool))
{
sendDone.Reset();
Client.BeginSend(SendByte,0,SendByte.Length,0,new AsyncCallback(SendCallback),Client);
sendDone.WaitOne();
}
if(sumlen>0)
{
if((curSend*100/sumlen)>times) 
{
StateObject state=new StateObject();
state.workSocket=Client;
receiveDone.Reset();
Client.BeginReceive(bytes,0,bytes.Length,0,new AsyncCallback(ReceivetimesCallback),state);
receiveDone.WaitOne();
}
} } }
//接收进度条回调函数
private void ReceivetimesCallback(IAsyncResult ar)
{

StateObject state=(StateObject)ar.AsyncState;
Socket client=state.workSocket;
if(client!=null)
{
int Receive=client.EndReceive(ar);
if(Receive>0)
{
string content=Encoding.UTF8.GetString(state.buffer);
if((state.buffer[0]==0)&&(state.buffer[1]==5))
{
string[] temp=content.Split(';');
times=Convert.ToInt32(temp[1]);
prgBar.Value=times;
}
}
}
receiveDone.Set();

}
大致这样:
在Send函数中首先发送一个字节数组,然后判断是否符合某种条件,如果符合就开始接收。
问题:
接收的时候:
在ReceiveCallback里,int Receive=client.EndReceive(ar);返回的Receive值很正确,发送五个字节就接收五个字节,发送六个字节就接收6个字节,可是,接收到的内容却全是0,不知为什么,在发送断我已经看了,发送的内容正确