我用socket这样发送一个文件行吗?
         FileStream fs = 
         new FileStream"c:\\1.txt",FileMode.Open,System.IO.FileAccess.ReadWrite);
fs.Read(sendbyte,0,(int)fs.Length-1);
         connectsock.Send(sendbyte,sendbyte.Length,0);         我用socket这样接收一个文件行吗?
         int bytes=0;
string jsStr="";
byte[] jsBytes=new byte[1024]; 
try
{
    while(true)
{
bytes = accsock.Receive(jsBytes,jsBytes.Length,0);
if(bytes<=0) 
{
    break; 
}
         jsStr+=System.Text.Encoding.ASCII.GetString(jsBytes,0,bytes);
byte[] content=System.Text.Encoding.ASCII.GetBytes(jsStr);
FileStream fs = new FileStream("d:\\1.txt",FileMode.Create,FileAccess.Read);
fs.Write(content,0,content.Length);
          }
         }
         catch(exception ex)
         {
                
          }

解决方案 »

  1.   

    发送的时候最好以一个固定长度进行发送;接收的时候也是如此。例如:
    //发送
    byte[] bBuffer = new byte[1024];
    int nRealRead = 0;
    do
    {
         nRealRead = fs.Read( bBuffer, 0, 1024 );
         connectsock.Send(bBuffer ,nRealRead,0);
    }
    while( nRealRead == 1024 );//接收
    byte[] bBuffer = new byte[1024];
    int nRealRead = 0;
    do
    {
         nRealRead = accsock.Receive(bBuffer ,1024,0);;
         fs.Write( bBuffer, nRealRead);
    }
    while( nRealRead == 1024 && accsock.Available > 0 );