我有个问题想请教下:
有计算机A 和计算机B,A上存有 一些.txt的文件 但是非常大估计能有1M或更大,然后计算机B要发送指令 给A
,比如发送 123 A就需要把123.txt发送到B的指定目录下面,怎么实现思路? 文件过大 不方便读入数据库然后共享然后读出 会耗费大量时间 前提是不能把A拿硬盘全考给B 然后数据库存路径~~~~
求解决办法 SOCKET可以吗? SOCKET可以发送文件吗? A上是否应该建立服务? 还有.NET的服务程序怎么建立?
有计算机A 和计算机B,A上存有 一些.txt的文件 但是非常大估计能有1M或更大,然后计算机B要发送指令 给A
,比如发送 123 A就需要把123.txt发送到B的指定目录下面,怎么实现思路? 文件过大 不方便读入数据库然后共享然后读出 会耗费大量时间 前提是不能把A拿硬盘全考给B 然后数据库存路径~~~~
求解决办法 SOCKET可以吗? SOCKET可以发送文件吗? A上是否应该建立服务? 还有.NET的服务程序怎么建立?
解决方案 »
- 如何解决开线程界面卡的问题。
- 求大侠帮忙 多线程界面卡死
- 跪求 在WorldWind中如何让3D模型随鼠标移动 例子
- 编译环境下如何删除其已经动态加载的dll(续昨天58分提问)
- 创建dataset的时候为什么要指定名称?
- new char[] {str} 怎么理解啊
- 求Rational XDE Developer Plus for VS.NET 2003的用法!
- 是高手请进,特别是懂彩信的高手
- 昨天给老婆定了3支玫瑰,结果花店把卡片搞错了,老婆心情大坏.......
- VS添加项目多了太慢.添加DLL如何方便的管理DLL
- 求算法:学生数不定,老师10名,每个老师分到的学生数可以不同,每个老师所带学生的平均分数相差越小越好
- 如何定时激活系统?
.net服务器程序:不知道楼主说的是不是window服务的意思,.net建议windows服务还是比较方便,具体的可以上网搜索一下,说不太清楚。
socket.Blocking = true ;
IPEndPoint computernode1 = new IPEndPoint(serverIpadress, 8080);socket.Bind(computernode1);socket.Listen(-1);当有其他的计算机发出连接请求的时候,被请求的计算机将对每一个连接请求分配一个线程,用于处理文件传输和其他服务。while ( true ) { clientsock = socket.Accept(); if ( clientsock.Connected ) { Thread tc = new Thread(new ThreadStart(listenclient)); tc.Start(); } }下面的代码展示了listenclient方法是如何处理另外一台计算机发送过来的请求。首先并对发送过来的请求字符串作出判断,看看是何种请求,然后决定相应的处理方法。void listenclient()
{
Socket sock = clientsock ;
try
{
while ( sock != null )
{
byte[] recs = new byte[32767];
int rcount = sock.Receive(recs,recs.Length,0) ;
string message = System.Text.Encoding.ASCII.GetString(recs) ;
//对message作出处理,解析处请求字符和参数存储在cmdList 中
execmd=cmdList[0];
sender = null ;
sender = new Byte[32767];
string parm1 = "";
//目录列举
if ( execmd == "LISTING" )
{
ListFiles(message);
continue ;
}
//文件传输
if ( execmd == "GETOK" )
{
cmd = "BEGINSEND " + filepath + " " + filesize ;
sender = new Byte[1024];
sender = Encoding.ASCII.GetBytes(cmd);
sock.Send(sender, sender.Length , 0 );
//转到文件下载处理
DownloadingFile(sock);
continue ;
}
}
}
catch(Exception Se)
{
string s = Se.Message;
Console.WriteLine(s);
}
}至此,基本的工作已经完成了,下面我们看看如何处理文件传输的。
while(rdby < total && nfs.CanWrite)
{
//从要传输的文件读取指定长度的数据
len =fin.Read(buffed,0,buffed.Length) ;
//将读取的数据发送到对应的计算机
nfs.Write(buffed, 0,len);
//增加已经发送的长度
rdby=rdby+len ;
}
从上面的代码可以看出是完成文件转换成FileStream 流,然后通过NetworkStream绑定对应的套节子,最后调用他的write方法发送到对应的计算机。
我们再看看接受端是如何接受传输过来的流,并且转换成文件的:
NetworkStream nfs = new NetworkStream(sock) ;
try
{
//一直循环直到指定的文件长度
while(rby < size)
{
byte[] buffer = new byte[1024] ;
//读取发送过来的文件流
int i = nfs.Read(buffer,0,buffer.Length) ;
fout.Write(buffer,0,(int)i) ;
rby=rby+i ;
}
fout.Close() ;从上面可以看出接受与发送恰好是互为相反的过程,非常简单。//取得预保存的文件名
string fileName="test.rar";
//远程主机
string hostName=TextBoxHost.Text.Trim();
//端口
int port=80;
//得到主机信息
IPHostEntry ipInfo=Dns.GetHostByName(hostName);
//取得IPAddress[]
IPAddress[] ipAddr=ipInfo.AddressList;
//得到ip
IPAddress ip=ipAddr[0];
//组合出远程终结点
IPEndPoint hostEP=new IPEndPoint(ip,port);
//创建Socket 实例
Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
try
{
//尝试连接
socket.Connect(hostEP);
}
catch(Exception se)
{
LeixunCMS.Common.MessageBox.Show(this.Page,"连接错误"+se.Message);}
string path = "E:\\c#\\convey_file\\convey_file\\Form1.cs"; //要传输的文件
TcpClient client = new TcpClient();
client.Connect(IPAddress.Parse("192.168.0.52"),9999);
FileStream file = new FileStream(path,FileMode.Open,FileAccess.Read); //注意与receive的filestream的区别
BinaryReader binaryreader = new BinaryReader(file);
byte[] b = new byte[4098];
int data;
Console.WriteLine("正在发送文件");
while ((data = binaryreader.Read(b, 0, 4098)) != 0) //这个注意是将文件写成流的形式
{
client.Client.Send(b,data,SocketFlags.None); //发送文件流到目标机器
}
client.Client.Shutdown(SocketShutdown.Both);
binaryreader.Close();
file.Close();
receive:
private Socket s;
TcpListener tl;
public void lis()
{
string path = "d:\\1.cs"; //要传入文件的路径
tl = new TcpListener(9999);
tl.Start();
Console.WriteLine("等待接受");
s = tl.AcceptSocket();
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write); //注意这个的属性和send端有所不同
BinaryWriter binarywrite = new BinaryWriter(fs);
int count;
byte[] b = new byte[4098];
while ((count = s.Receive(b, 4098, SocketFlags.None)) != 0) //这个是接受文件流
{
binarywrite.Write(b,0,count); //将接收的流用写成文件
}
binarywrite.Close();
fs.Close();
s.Close();
tl.Stop();
Console.WriteLine("文件传输完毕");
今天终于把文件传输问题给研究明白了,特发此代码用来帮助正在学习的人们。send :
string path = "E:\\c#\\convey_file\\convey_file\\Form1.cs"; //要传输的文件
TcpClient client = new TcpClient();
client.Connect(IPAddress.Parse("192.168.0.52"),9999);
FileStream file = new FileStream(path,FileMode.Open,FileAccess.Read); //注意与receive的filestream的区别
BinaryReader binaryreader = new BinaryReader(file);
byte[] b = new byte[4098];
int data;
Console.WriteLine("正在发送文件");
while ((data = binaryreader.Read(b, 0, 4098)) != 0) //这个注意是将文件写成流的形式
{
client.Client.Send(b,data,SocketFlags.None); //发送文件流到目标机器
}
client.Client.Shutdown(SocketShutdown.Both);
binaryreader.Close();
file.Close();
receive:
private Socket s;
TcpListener tl;
public void lis()
{
string path = "d:\\1.cs"; //要传入文件的路径
tl = new TcpListener(9999);
tl.Start();
Console.WriteLine("等待接受");
s = tl.AcceptSocket();
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write); //注意这个的属性和send端有所不同
BinaryWriter binarywrite = new BinaryWriter(fs);
int count;
byte[] b = new byte[4098];
while ((count = s.Receive(b, 4098, SocketFlags.None)) != 0) //这个是接受文件流
{
binarywrite.Write(b,0,count); //将接收的流用写成文件
}
binarywrite.Close();
fs.Close();
s.Close();
tl.Stop();
Console.WriteLine("文件传输完毕");