日期:2014-05-20  浏览次数:20891 次

socket 客户端收到的数据与服务器发送不一致问题
小弟在搭建一c/s架构的通信程序。现在遇到socket   客户端收到的数据与服务器发送不一致问题。客户端收到的数据总是比服务器端发送的少一点。如果在客户端程序中设置断点调试就能收全。百思不得其解,请各位高手帮助解决。
注:(所有测试都在局域网内进行)
服务器端使用ReadToEnd()方法将一文件一次性读出,然后再转换成UTF8码,再使用clientsocket.Send()方法一次传送给客户端。

客户端部分代码如下:
namespace   client
{

        public   class   client
        {
                static   Socket   serverSocket   =   null;
                private   static   Socket   ConnectSocket(string   server,   int   port)
                {
                        Socket   s   =   null;
                        IPHostEntry   hostEntry   =   null;
               
                        //   Get   host   related   information.
                        hostEntry   =   Dns.GetHostEntry(server);

                        //   Loop   through   the   AddressList   to   obtain   the   supported   AddressFamily.   This   is   to   avoid
                        //   an   exception   that   occurs   when   the   host   IP   Address   is   not   compatible   with   the   address   family
                        //   (typical   in   the   IPv6   case).
                        foreach(IPAddress   address   in   hostEntry.AddressList)
                        {
                                IPEndPoint   ipe   =   new   IPEndPoint(address,   port);
                                Socket   tempSocket   =new   Socket(ipe.AddressFamily,   SocketType.Stream,   ProtocolType.Tcp);

                                tempSocket.Connect(ipe);

                                if(tempSocket.Connected)
                                {
                                        s   =   tempSocket;
                                        break;
                                }
                                else