c# Socket网络传输中怎样获取发送方的传输文件的文件名称?
问题如上,我的做法是服务器端发送了一个.doc或者.txt等格式文件,不规定为哪一种文件,下面是服务器端的主要发送编码,如果客户端只是接收一种文件类型,我可以声明一个pa...
问题如上,我的做法是服务器端发送了一个.doc或者.txt等格式文件,不规定为哪一种文件,下面是服务器端的主要发送编码,如果客户端只是接收一种文件类型,我可以声明一个path为此类型文件的后缀,如string path=@"c:\test.txt"; 但是现在问题是客户端接收的文件类型是不确定的,请问应该如何写接收编码,我尝试过用system.Text.Encoding.BigEndianUnicode.GetString(by)方法和
binaryReader br=new binaryReader(client.GetStream)等,都不成功,希望有高手能够解答,万分感谢啊~(附上服务器端部分发送代码)
try
{
IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
IPAddress ip = ipHost.AddressList[0];
IPEndPoint ipPoint = new IPEndPoint(ip, 5000);
tcpclient.Connect(IPAddress.Parse("192.168.1.36"), 5000);
OpenFileDialog openfile = new OpenFileDialog();
openfile.FileName = "";
openfile.Title = "打开文件";
openfile.Filter = "所有文件(*.*)|*.*";
if (openfile.ShowDialog() == DialogResult.OK)
{
FileInfo fo=new FileInfo(openfile.FileName);
this.label2.Text = fo.ToString();
string path = openfile.FileName;
byte[] by=new byte[1024];
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
BinaryReader binary = new BinaryReader(file);
int data;
while ((data = binary.Read(by, 0, 1024)) != 0)
{
tcpclient.Client.Send(by, SocketFlags.None);
}
tcpclient.Client.Shutdown(SocketShutdown.Both);
binary.Close();
file.Close();
}
MessageBox.Show("文件发送成功!"); 展开
binaryReader br=new binaryReader(client.GetStream)等,都不成功,希望有高手能够解答,万分感谢啊~(附上服务器端部分发送代码)
try
{
IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
IPAddress ip = ipHost.AddressList[0];
IPEndPoint ipPoint = new IPEndPoint(ip, 5000);
tcpclient.Connect(IPAddress.Parse("192.168.1.36"), 5000);
OpenFileDialog openfile = new OpenFileDialog();
openfile.FileName = "";
openfile.Title = "打开文件";
openfile.Filter = "所有文件(*.*)|*.*";
if (openfile.ShowDialog() == DialogResult.OK)
{
FileInfo fo=new FileInfo(openfile.FileName);
this.label2.Text = fo.ToString();
string path = openfile.FileName;
byte[] by=new byte[1024];
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
BinaryReader binary = new BinaryReader(file);
int data;
while ((data = binary.Read(by, 0, 1024)) != 0)
{
tcpclient.Client.Send(by, SocketFlags.None);
}
tcpclient.Client.Shutdown(SocketShutdown.Both);
binary.Close();
file.Close();
}
MessageBox.Show("文件发送成功!"); 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询