这个是客户端发送的
        private static void SendFile()
        {
            string filePath = @"E:\release\bin\Common.dll";
            string directPath = "E:/Common.dll";
            int port = 8888;
            IPAddress ip = IPAddress.Parse("192.168.10.11");
            System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(
                System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream,
                System.Net.Sockets.ProtocolType.Tcp);
            IPEndPoint endPoint = new IPEndPoint(ip, port);
            socket.Connect(endPoint);
            Console.Write("Connect Successfull!\r\nStart Sending...\r\n");            FileInfo info = new FileInfo(filePath);            using (FileStream fs = File.Open(filePath, FileMode.OpenOrCreate))
            {
                //发送文件长度
                byte[] lenArr = BitConverter.GetBytes(fs.Length);
                socket.Send(lenArr);                //发送文件路径
                byte[] path = Encoding.ASCII.GetBytes(directPath);
                socket.Send(path);                //发送文件
                byte[] data = new byte[fs.Length];
                fs.Read(data, 0, data.Length);
                socket.Send(data);
            }
            Console.Write("Send Successfull!");
        }code][code=C#]            IPEndPoint ip = new IPEndPoint(IPAddress.Any, 8888);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(ip);
            socket.Listen(10);            Socket client = socket.Accept();
            IPEndPoint remote = (IPEndPoint)client.RemoteEndPoint;
            Console.Write("Accept:" + remote.Address);
            byte[] temp = new byte[32];            //接收文件长度
            client.Receive(temp);
            int length = BitConverter.ToInt32(temp, 0);            //接收文件路径
            client.Receive(temp);
            string path = Encoding.ASCII.GetString(temp);            //接收文件
            byte[] data = new byte[length];
            using (FileStream fs = new FileStream("", FileMode.OpenOrCreate))
            {
                fs.Write(data, 0, data.Length);
            }
            Console.Read();问题出现在“接收文件路径那里”,每次接收到的值都不一样的,有时候是:
"E:/Common.dllMZ?\0\0\0\0\0\0\0??\0\0?\0\0"
而有的时候是"\0??\0\0?\0\0\0\0\0\0\0@\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
为什么会出现这种情况呢?

解决方案 »

  1.   

    接收端的
    IPEndPoint ip = new IPEndPoint(IPAddress.Any, 8888);
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Bind(ip);
                socket.Listen(10);            Socket client = socket.Accept();
                IPEndPoint remote = (IPEndPoint)client.RemoteEndPoint;
                Console.Write("Accept:" + remote.Address);
                byte[] temp = new byte[32];            //接收文件长度
                client.Receive(temp);
                int length = BitConverter.ToInt32(temp, 0);            //接收文件路径
                client.Receive(temp);
                string path = Encoding.ASCII.GetString(temp);            //接收文件
                byte[] data = new byte[length];
                using (FileStream fs = new FileStream("", FileMode.OpenOrCreate))
                {
                    fs.Write(data, 0, data.Length);
                }
                Console.Read();
      

  2.   

    关于socket发送文件或者信息 建议楼主还是用一问一答得方式 既发送一条信息之后等待服务器确认了再发第二条,类似于你这样的发送发送的信息少,频率快,tcp会做优化把信息都放到一个包里面发,也就是所谓的粘包现象,这样你认为的发了两次信息结果只发了一次、