服务器端:
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
namespace 输出
{
class Program
{
static void Main(string[] args)
{
Connect con = new Connect();
Thread thread = new Thread(new ThreadStart(con.WaitForConnect));
thread.Start();
}
}
public class Connect
{
TcpListener Listener;
TcpClient Client;
NetworkStream Stream;
public void WaitForConnect()
{
Listener = new TcpListener(IPAddress.Any, 8888);
Listener.Start();
Client = Listener.AcceptTcpClient();
Stream = Client.GetStream();
XmlSerializer ser = new XmlSerializer(typeof(XmlDocument));
XmlDocument doc = (XmlDocument)ser.Deserialize(Stream);
doc.Save("C://Server.xml");
}
}
}
客户端:
using System.Net.Sockets;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
namespace 客户端
{
static class Program
{
static void Main()
{
Connect con = new Connect();
Thread thread = new Thread(new ThreadStart(con.WaitForConnect));
thread.Start();
}
}
public class Connect
{
public TcpListener Listener;
public TcpClient Client;
public NetworkStream Stream;
public void WaitForConnect()
{
Client = new TcpClient("127.0.0.1", 8888);
Stream = Client.GetStream();
XmlDocument doc = new XmlDocument();
doc.Load("C://Client.xml");
XmlSerializer ser = new XmlSerializer(typeof(XmlDocument));
ser.Serialize(Stream, doc);
Stream.Close();
}
}
}
这是服务器端和客户端之间一个XML文档的传输,XML需要在传输之前建立好