发送端
FileStream fs = new FileStream(SenFilePath, FileMode.Open);                for ( int i=0 ; i < Axlst_ServerPath.Items.Count ; i++ )
                {                    Socket sen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);                    string[] IPPort = Axlst_ServerPath.Items[i].ToString().Split(Convert.ToChar(":"));                    IPAddress myIP = IPAddress.Parse(IPPort[0]);                    IPEndPoint ipe = new IPEndPoint(myIP, Convert.ToInt32(IPPort[1]));                    sen.Connect(ipe);                    
                    //byte[] message = UTF8Encoding.UTF8.GetBytes("Statr:" + Axlab_SelectFileName.Text.ToString() + ":" + fs.Length + ":\r\n");
                    //sen.Send(message);                    ishead = true;                    while (sendCount < fs.Length)
                    {                        int count = fs.Read(by, 0, 1024);                        sen.Send(by);                        sendCount = sendCount + count;                        System.Threading.Thread.Sleep(20);   
                    }                    sen.Close();
                    fs.Close();接收            while (true)
            {
                Socket newSock = s.Accept();                IPEndPoint clientep = (IPEndPoint)newSock.RemoteEndPoint ;                FileStream fs = new FileStream(Axtxt_FilePath.Text + "1.jpg", FileMode.Create, FileAccess.Write);                newSock.Receive(bytebuffer);                fs.Write(bytebuffer,0, bytebuffer.Length);
            }
为什么没全部发送过来,只发了第一次的1024字节?要怎么修改?做个小软件到这步难倒了。

解决方案 »

  1.   

    IPEndPoint clientep = (IPEndPoint)newSock.RemoteEndPoint ;
    这句我是在网上,COPY的不没注意弄掉。
      

  2.   


                        while (sendCount < fs.Length)
                        {                        int count = fs.Read(by, 0, 1024);
                            sen.Send(by);                        sendCount = sendCount + count;                        System.Threading.Thread.Sleep(20);   
                        }
      

  3.   

    while (sendCount < fs.Length)
                        {                        int count = fs.Read(by, 0, 1024);
                            sen.Send(by);                        sendCount = sendCount + count;                        System.Threading.Thread.Sleep(20);   
                        }
      

  4.   

    那句是些问题,我现在在测试
    我想知道为什么他只发了一次信息出去就不发了
    才是我接收有问题。
    还有挂起的连接我怎么知道发送这边是不是断了?它能自己判断吗?
    我试着传了个图片只收到 了一个角,就1KB的文件.
    如果可以加Q方便些
    谢谢了。
      

  5.   

    顶!急待解决望高手,指教一二。                    while (sendCount < fs.Length)
                        {                        int count = fs.Read(by, 0, by.Length);                        sen.Send(by);                        sendCount = sendCount + count;                        System.Threading.Thread.Sleep(20);   
                        }
      

  6.   

    接收                 Socket newSock = s.Accept();                IPEndPoint clientep = (IPEndPoint)newSock.RemoteEndPoint ;                FileStream fs = new FileStream(Axtxt_FilePath.Text + "1.jpg", FileMode.Create, FileAccess.Write);
                    while (true)
                    {                  if(newSock.Receive(bytebuffer)==0)
                      {
                        fs.close();
                        break;
                      }
                       fs.Write(bytebuffer,0, bytebuffer.Length);
                    }