用C#实现的socket文件传输问题??急求:出现了传输过去的文件内容乱了,顺序错了,请高手帮我看看

dingdingbao 2006-10-22 11:47:36
我的接收端程序(先接收文件名和发送的文件长度,然后接收文件,可以接收多个文件)
using System;
using System.Threading;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Text;


namespace ConsoleApplication_socketReceive
{
public class ThreadProcessor
{

TcpClient tcpclient;
string path="E:\\DYL\\SOCKET\\file\\";//文件存放的路径

public ThreadProcessor(TcpClient maintcpclient)
{
tcpclient = maintcpclient;
}

public void ProcessService()
{
int fname_length=0;
string fname="";
string flenstr="";
int filelen=0;

NetworkStream networkstream=tcpclient.GetStream();
if(networkstream.CanRead)
{
byte[] receive=new byte[1024];
networkstream.Read(receive,0,receive.Length);
int k=receive.Length;

Console.WriteLine("k:{0}",k);
for(int i=0;i<k;i++)
{
Console.Write(Convert.ToChar(receive[i]));
if(receive[i]=='$')
{
fname_length=i;
for(int j=0;j<fname_length;j++)
{
fname+=Convert.ToChar(receive[j]);
}
}
if(receive[i]=='#')
{
int lens=i;
for(int c=fname_length+1;c<lens;c++)
{
flenstr+=Convert.ToChar(receive[c]);
filelen=Int32.Parse(flenstr);
}
}

}
Console.WriteLine("fname_length:{0}",fname_length);
Console.WriteLine("fname:{0}",fname);
Console.WriteLine("flenstr:{0}",flenstr);
Console.WriteLine("filelen:{0}",filelen);

string pathandname=path+fname;
Console.WriteLine("pathandname:{0}",pathandname);

FileStream fileWriter=new FileStream(pathandname,FileMode.Create);
byte[] write=new byte[1024];

int len=filelen;
networkstream.Read(write,0,write.Length);
int bytes=write.Length;
while(bytes>0)
{
fileWriter.Write(write,0,write.Length);

len-=bytes;
if((len<bytes))
{
byte[] lenwrite=new byte[len];
bytes=networkstream.Read(lenwrite,0,lenwrite.Length);
fileWriter.Write(lenwrite,0,lenwrite.Length);
bytes=0;
}
else
{
bytes=networkstream.Read(write,0,write.Length);
}
}
fileWriter.Close();

}
}
}

public class mainThread
{

[STAThread]
static void Main(string[] args)
{

TcpListener tcp1=new TcpListener(6016);
tcp1.Start();
TcpClient maintcpclient;
System.Console.WriteLine("Waiting for a client to connect...");

while(true)
{
maintcpclient = tcp1.AcceptTcpClient();
System.Console.WriteLine("Client Connected....");

ThreadProcessor tp = new ThreadProcessor(maintcpclient);
Thread SocketThread = new Thread(new ThreadStart(tp.ProcessService));
SocketThread.Start();
Thread.Sleep(3000);
}
}
}
}


我的发送端程序(发送端,先去读test.txt文件中的文件名,然后找到指定路径下的要传输的文件(xml文件),解析出IP+PORT,然后建立连接,发送文件)
using System;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Xml;
namespace socket_send
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
string path="E:\\DYL\\SOCKET\\";//取文件的路径
string pathandfile="";

StreamReader sr=File.OpenText("E:\\DYL\\SOCKET\\test.txt");//要发送的文件名写在test.txt中
string contents=sr.ReadToEnd();//获得文件名
sr.Close();
string[] lines=contents.Split(new Char[]{'\n'});
for(int i=0;i<lines.Length;i++)//一次发n个文件
{
Console.WriteLine(i+":\t"+lines[i]);
string filename=lines[i];
pathandfile=path+filename;
Console.WriteLine("pathandfile:{0}",pathandfile.ToString());
string[] configs=getServerAndPort(pathandfile);

TcpClient tcpClient=new TcpClient();//建立一个通信套接字
Console.WriteLine("连接中...");
tcpClient.Connect(configs[0],int.Parse(configs[1]));//连接服务器
Console.WriteLine("已连接上...");
NetworkStream networkStream=tcpClient.GetStream();//客户端流
/*若用下面的这几行代码获得文件名,传过去的文件大小没出错,但是文件的内容乱了*/
int strlen=lines[i].Length;
int lineindex= lines[i].IndexOf("\r");
if(lineindex>0)
{
filename=lines[i].Substring(0,lineindex);
Console.WriteLine("filename:{0}",filename);
}
else
{
filename=lines[i];
Console.WriteLine("filename:{0}",filename);
}
/*若用下面这两行注释掉的代码,传输的文件就完全正确*/
// Console.WriteLine("输入要传送的文件名:");//取文件名
// filename=Console.ReadLine();


pathandfile=path+filename;
Console.WriteLine("pathandfile:{0}",pathandfile.ToString());
FileStream fileReader=new FileStream(pathandfile,FileMode.Open,FileAccess.Read,FileShare.Read);//读要传送的文件
int len=(int)fileReader.Length;//获得所得文件的长度
Console.WriteLine("len:{0}",len);

string file=filename+'$'+len.ToString()+'#';
int send_length=file.Length;
byte[] readname=new byte[send_length];

readname=System.Text.Encoding.UTF8.GetBytes(file);
networkStream.Write(readname,0,readname.Length);//先发送文件名和文件的长度
byte[] read=new byte[1024];//定读文件的缓冲区
int bytes;

bytes=fileReader.Read(read,0,read.Length);
while(bytes!=0)
{
networkStream.Write(read,0,read.Length);
bytes=fileReader.Read(read,0,read.Length);
Console.WriteLine("bytes:{0}",bytes);
}
fileReader.Close();
tcpClient.Close();
// }
}

}

public static string[] getServerAndPort(string fileName)//解析XML文件的IP+PORT
{
string[] servConfig=new string[2];
XmlDocument document=new XmlDocument();
document.Load(fileName);

XmlNodeList nodes=document.GetElementsByTagName("recvr");

nodes=nodes[0].ChildNodes;

servConfig[0]=nodes[0].InnerText;

nodes=document.GetElementsByTagName("port");

servConfig[1]=nodes[1].InnerText;

return servConfig;
}
}
}

...全文
785 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
listme 2006-11-01
  • 打赏
  • 举报
回复
mark
liujia_0421 2006-10-30
  • 打赏
  • 举报
回复
看下Knight94(愚翁) 大哥给你提供的资料吧,应该很有帮助..
liujia_0421 2006-10-30
  • 打赏
  • 举报
回复
支持一下吧...
灰太狼 2006-10-30
  • 打赏
  • 举报
回复
幫你up。
dingdingbao 2006-10-30
  • 打赏
  • 举报
回复
分数太高 没什么人回复 我晚点结 呜呜
viena 2006-10-30
  • 打赏
  • 举报
回复
哦?
dingdingbao 2006-10-30
  • 打赏
  • 举报
回复
我大概知道怎么回事情了,但是没有人来证实我的理解,唉,算了 结帖了。
阿牛138588 2006-10-24
  • 打赏
  • 举报
回复
mark
xqlvcc 2006-10-24
  • 打赏
  • 举报
回复
帮顶一下,关心一下小妹妹
Knight94 2006-10-22
  • 打赏
  • 举报
回复
文件传输,和普通信息传输基本一样

参看
Sending Binary Data using Sockets
http://www.codeproject.com/cs/internet/binarydatausingsocket.asp

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧