| 发表于:2007-06-03 18:08:30 楼主 |
客户端: using system; using system.collections.generic; using system.text; using system.net; using system.net.sockets; using system.io; using system.runtime.serialization.formatters.binary; namespace client { class program { private static string ipaddress = "127.0.0.1 "; private static int port = 2235; private static string filepath = @ "g:\filebaksystem\source\ "; private static string filename = "a.txt "; private static string bakpath = @ "g:\filebaksystem\target\ "; static void main(string[] args) { streamreader sr = new streamreader(filepath + filename); filestruct filestruct = new filestruct(); filestruct.filedata = encoding.default.getbytes(sr.readtoend()); sr.close(); filestruct.filename = filename; filestruct.bakpath = bakpath; tcpclient tcpclient = new tcpclient(ipaddress, port); networkstream ns = tcpclient.getstream(); binaryformatter bf; bf = new binaryformatter(); memorystream stream = new memorystream(); bf.serialize(stream, filestruct); byte[] buff = stream.toarray(); ns.write(buff, 0, buff.length); ns.close(); tcpclient.close(); } } } 服务器端: using system; using system.collections.generic; using system.text; using system.net; using system.net.sockets; using system.io; using system.threading; using system.runtime.serialization.formatters.binary; namespace server { class program { static void main(string[] args) { ipaddress currentip = ipaddress.parse( "127.0.0.1 "); int listenport = 2235; tcplistener tcplistener = new tcplistener(currentip, listenport); tcplistener.start(); tcpclient tcpclient = tcplistener.accepttcpclient(); networkstream ns = tcpclient.getstream(); binaryformatter bf; bf = new binaryformatter(); filestruct reci = (filestruct)bf.deserialize(ns);//反序列化数据 filestream fs = new filestream(reci.bakpath + reci.filename, filemode.createnew); fs.write(reci.filedata,0, reci.filedata.length); fs.close(); tcpclient.close(); tcplistener.stop(); } } } |
|
|
|
|