您的位置:程序门 -> .net技术 -> asp.net



这段socket代码哪里错了?传送小文件没问题,大文件就报错了!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


这段socket代码哪里错了?传送小文件没问题,大文件就报错了!
发表于: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();
                }
        }
}

发表于:2007-06-04 13:39:021楼 得分:0
什么异常?
发表于:2007-06-04 13:55:172楼 得分:0
乱七八糟
首先说下,asp.net用socket做什么?传输文件用那个什么webclient不是很好吗?
还有.你的服务器程序写的乱七八糟,不知道你想做什么

你如果用tcp,那么,服务器开启了监听后,是这样
tcplistener   tcplistener   =   new   tcplistener(currentip,   listenport);
                        tcplistener.start();

while(bool服务器已启动)   //
{
  socket   socket   =   tcplistener.acceptsocket();
  //不是tcpclient   tcpclient   =   tcplistener.accepttcpclient();
  //说明,该方式属于同步传输的情况下
}
//你的其他程序没看,感觉........不说了
发表于:2007-06-04 19:24:573楼 得分:0
fs.write(reci.filedata,0,   reci.filedata.length);     关键是这句。。

我传输的文件如果是很小的.txt文件他不报错,     传mp3之类的大文件就报错了。。

好象要用什么异步传输什么的。。     我现在手上没资料   说是要用beginwrite??


快速检索

最新资讯
热门点击