读文件为byte[],然后通过Socket发送,接收后通过StreamRead读byte[],还原成字符串。注意一点,流操作时需要提供相同的Encoding.ASCII或Encoding.Default。

解决方案 »

  1.   

    byte[] b = Encoding.GetBytes(xmlString);socket.Send(b);server.Receive(b);
    string xml = Encoding.GetString(b);
    都是伪代码,意思是这样.
      

  2.   


    我的XML檔案有幾人節點,我需要在接收時將XML節點的值分別顯示出來,要怎麼做?
      

  3.   

    要么在客户端发送xml前做处理,要么接到字符串后做处理。跟socket无关,socket只负责通过协议传输数据(二进制流)
      

  4.   

    [XmlRoot("bookstore")]
    public class bookstore
    {
    [XmlArray("book")]
            [XmlArrayItem("book", typeof(book))]
    public book[] {get;set;}
    }public class book
    {
    [XmlAttribute("genre")]
    public string genre {get;set;}

    ...
    [XmlAttribute("genre")]
    public string PN {get;set;}
    }TcpClient tc = new TcpClient();
    tc.Connect(...);
    NetworkStream ns = tc.GetStream();
    byte[] buffer = new byte[512];
    int readBytes = ns.Read(buffer, 0, 512);
    Stream ms = new MemoryStream(buffer, 0, readBytes);
    XmlSerializer xSer = new XmlSerializer(typeof(bookstore));
    bookstore bs = xSer.Deserialize(ms) as bookstore;
      

  5.   

    注意编码要一致,客户端Encode,然后当成普通string传即可,服务器端收到后Decode,然后当成普通的XML处理即可
      

  6.   

    我的Server端要怎麼樣一個監聽有沒有數據往上傳?
    因為我寫好了,Client與Server的程式都開著,當我第一次在Client上傳數據時,Server端可以得到數據,但是當我在Client第二次點擊傳輸時,Client卻死掉了一樣怎麼回事?
      

  7.   

    我的Client的代碼是:
    int port = 2000;
                         string host = "172.17.161.99";
                         IPAddress ip = IPAddress.Parse(host);
                         IPEndPoint ipe = new IPEndPoint(ip, port);
                         Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                         c.Connect(ipe);
                         string sendStr = spn+"/"+smodel+"/"+sop+"/"+sline+"/"+suptime;
                         byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                         c.Send(bs, bs.Length, 0);
                         string recvStr = "";
                         byte[] recvBytes = new byte[1024];
                         int bytes;                     bytes = c.Receive(recvBytes, recvBytes.Length, 0);                     recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);                     Console.WriteLine(recvStr);
                         c.Shutdown(SocketShutdown.Both);
                         c.Close();Server端的代碼是:
    int port = 2000;
                    string host = "172.17.161.99";
                    IPAddress ip = IPAddress.Parse(host);
                    IPEndPoint ipe = new IPEndPoint(ip, port);
                    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    s.Bind(ipe);
                    s.Listen(0);
                    Socket temp = s.Accept();
                    string recvStr = "";
                    //string[] items = recvStr.Split(new char[] { '/' });
                    byte[] recvBytes = new byte[1024];
                    int bytes;
                    bytes = temp.Receive(recvBytes, recvBytes.Length, 0);
                    recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
    MessageBox.Show(recvStr);
    string sendStr = "Ok!Sucess!";
                        byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                        temp.Send(bs, bs.Length, 0);
                        temp.Shutdown(SocketShutdown.Both);
                        temp.Close();
                        s.Shutdown(SocketShutdown.Both);
                        s.Close();以上是我的代碼,
    Client與Server的程式都開著,出現兩種情況:
    1.當我第一次在Client上傳數據時,Server端可以得到數據,但是當我在Client第二次點擊傳輸時,Client卻死掉了一樣 
    2.當我關掉Server端程式時,Client端又正常了
      

  8.   

    把xml文件当成file文件处理,每一条放到数组里。传到客户端直接把字符串写到文件里。