scoket傳輸XML文件內容
想要將一個XML文件裡的內容:如:<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<PN>a</PN>
<Model>asddfadfa</Model>
<OP>fas</OP>
<Line>dfasdf</Line>
<UpTime>2008/11/18 下午 01:39:50</UpTime>
</book>
</bookstore>
通過Sockect從客戶端傳輸到服務器,並在服務器上讀取節點的值,並在服務器上生成這個xml檔案 --------------------编程问答-------------------- 把它当成普通的字符串直接发送就行了。 --------------------编程问答-------------------- TS 顶贴接分 --------------------编程问答-------------------- 读文件为byte[],然后通过Socket发送,接收后通过StreamRead读byte[],还原成字符串。注意一点,流操作时需要提供相同的Encoding.ASCII或Encoding.Default。 --------------------编程问答-------------------- byte[] b = Encoding.GetBytes(xmlString);
socket.Send(b);
server.Receive(b);
string xml = Encoding.GetString(b);
都是伪代码,意思是这样. --------------------编程问答--------------------
我的XML檔案有幾人節點,我需要在接收時將XML節點的值分別顯示出來,要怎麼做? --------------------编程问答-------------------- 要么在客户端发送xml前做处理,要么接到字符串后做处理。跟socket无关,socket只负责通过协议传输数据(二进制流) --------------------编程问答--------------------
[XmlRoot("bookstore")]--------------------编程问答-------------------- 注意编码要一致,客户端Encode,然后当成普通string传即可,服务器端收到后Decode,然后当成普通的XML处理即可 --------------------编程问答-------------------- 我的Server端要怎麼樣一個監聽有沒有數據往上傳?
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;
因為我寫好了,Client與Server的程式都開著,當我第一次在Client上傳數據時,Server端可以得到數據,但是當我在Client第二次點擊傳輸時,Client卻死掉了一樣
怎麼回事? --------------------编程问答-------------------- 第一次传输后,流关闭掉了吗? --------------------编程问答-------------------- 我的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端又正常了 --------------------编程问答-------------------- 有人回答不咯 --------------------编程问答-------------------- ????????? --------------------编程问答-------------------- 把xml文件当成file文件处理,每一条放到数组里。传到客户端直接把字符串写到文件里。
补充:.NET技术 , C#