在C#.NET 2.0 Windows应用程序中使用套接字应用程序进行文件传输 [英] File Transfer using Socket Application in C# .NET 2.0 windowsapplication

查看:70
本文介绍了在C#.NET 2.0 Windows应用程序中使用套接字应用程序进行文件传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#.NET 2.0中使用套接字应用程序进行文件传输

File Transfer using Socket Application in C# .NET 2.0

推荐答案

最接近您想要的内容(请参阅我对问题的评论,目前尚不清楚定义)是FTP,请参见 http://en.wikipedia.org/wiki/FTP [高级FTP服务器 [ http: //msdn.microsoft.com/zh-CN/library/system.net.ftpwebrequest%28v=vs.80%29.aspx [ http://www.codeproject .com/search.aspx?q = FTP& doctypeid = 1 [
The closest to what you might want (please see my comments to the question, which is not clearly defined) is FTP, see http://en.wikipedia.org/wiki/FTP[^].

If you need to implement the server part, this is more difficult. Please see this CodeProject article:
Advanced FTP Server[^].

I looked at the article before answering to make sure the code for .NET v.2.0 is available.

Client part is much easier. You need to use the class FtpWebRequest. This is a help page for v.2.0: http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest%28v=vs.80%29.aspx[^].

Please see the code sample in this article.

If this is not enough, Google or find some more in CodeProject articles: http://www.codeproject.com/search.aspx?q=FTP&doctypeid=1[^].

You will find a lot.

—SA


SERVER:
SERVER:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
 
namespace SocketfileServer
{
public partial class Form1 : Form
{
private Thread thrDownload;
private Stream strLocal;
private NetworkStream strRemote;
private TcpListener tlsServer;
private delegate void UpdateStatusCallback(string StatusMessage);
private delegate void UpdateProgressCallback(Int64 BytesRead, Int64 TotalBytes);
private static int PercentProgress;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
private void btnStart_Click(object sender, EventArgs e)
{
thrDownload = new Thread(StartReceiving);
thrDownload.Start();
}
private void StartReceiving()
{
try
{
string hstServer = Dns.GetHostName();
IPAddress ipaLocal = Dns.GetHostEntry(hstServer).AddressList[0];
if (tlsServer == null)
{
tlsServer = new TcpListener(ipaLocal, Convert.ToInt32(txtPort.Text));
}
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"Starting the server...\r\n" });
tlsServer.Start();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"The server has started. Please connect the client to " + ipaLocal.ToString() + "\r\n" });
TcpClient tclServer = tlsServer.AcceptTcpClient();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"The server has accepted the client\r\n" });
strRemote = tclServer.GetStream();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"The server has received the stream\r\n" });
int bytesSize = 0;
byte[] downBuffer = new byte[2048];
bytesSize = strRemote.Read(downBuffer, 0, 2048);
string FileName = System.Text.Encoding.ASCII.GetString(downBuffer, 0, bytesSize);
strLocal = new FileStream(@"C:\" + FileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
downBuffer = new byte[2048];
bytesSize = strRemote.Read(downBuffer, 0, 2048);
long FileSize = Convert.ToInt64(System.Text.Encoding.ASCII.GetString(downBuffer, 0, bytesSize));
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"Receiving file " + FileName + " (" + FileSize + " bytes)\r\n" });
downBuffer = new byte[2048];
while ((bytesSize = strRemote.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
strLocal.Write(downBuffer, 0, bytesSize);
this.Invoke(new UpdateProgressCallback(this.UpdateProgress), new object[] { strLocal.Length, FileSize });
}
}
finally
{
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"The file was received. Closing streams.\r\n" });
strLocal.Close();
strRemote.Close();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"Streams are now closed.\r\n" });
StartReceiving();
}
}
private void UpdateStatus(string StatusMessage)
{
txtLog.Text += StatusMessage;
}
private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
{
if (TotalBytes > 0)
{
PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);
prgDownload.Value = PercentProgress;
}
}
private void btnStop_Click(object sender, EventArgs e)
{
strLocal.Close();
strRemote.Close();
txtLog.Text += "Streams are now closed.\r\n";
}
}
}
 

CLIENT:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
 
namespace SocketfileClient
{
public partial class Form1 : Form
{
TcpClient tcpClient;
FileStream fstFile;
NetworkStream strRemote;
public Form1()
{
InitializeComponent();
}
private void ConnectToServer(string ServerIP, int ServerPort)
{
tcpClient = new TcpClient();
try
{
tcpClient.Connect(ServerIP, ServerPort);
txtLog.Text += "Successfully connected to server\r\n";
}
catch (Exception exMessage)
{
txtLog.Text += exMessage.Message;
}
}
private void btnConnect_Click(object sender, EventArgs e)
{
ConnectToServer(txtServer.Text, Convert.ToInt32(txtPort.Text));
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
tcpClient.Close();
strRemote.Close();
fstFile.Close();
txtLog.Text += "Disconnected from server.\r\n";
}
private void btnSend_Click(object sender, EventArgs e)
{
if (tcpClient.Connected == false)
{
ConnectToServer(txtServer.Text, Convert.ToInt32(txtPort.Text));
}
if (openFile.ShowDialog() == DialogResult.OK)
{
txtLog.Text += "Sending file information\r\n";
strRemote = tcpClient.GetStream();
byte[] byteSend = new byte[tcpClient.ReceiveBufferSize];
fstFile = new FileStream(openFile.FileName, FileMode.Open, FileAccess.Read);
BinaryReader binFile = new BinaryReader(fstFile);
FileInfo fInfo = new FileInfo(openFile.FileName);
string FileName = fInfo.Name;
byte[] ByteFileName = new byte[2048];
ByteFileName = System.Text.Encoding.ASCII.GetBytes(FileName.ToCharArray());
strRemote.Write(ByteFileName, 0, ByteFileName.Length);
long FileSize = fInfo.Length;
byte[] ByteFileSize = new byte[2048];
ByteFileSize = System.Text.Encoding.ASCII.GetBytes(FileSize.ToString().ToCharArray());
strRemote.Write(ByteFileSize, 0, ByteFileSize.Length);
txtLog.Text += "Sending the file " + FileName + " (" + FileSize + " bytes)\r\n";
int bytesSize = 0;
byte[] downBuffer = new byte[2048];
while ((bytesSize = fstFile.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
strRemote.Write(downBuffer, 0, bytesSize);
}
txtLog.Text += "File sent. Closing streams and connections.\r\n";
tcpClient.Close();
strRemote.Close();
fstFile.Close();
txtLog.Text += "Streams and connections are now closed.\r\n";
}
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
}
}


这篇关于在C#.NET 2.0 Windows应用程序中使用套接字应用程序进行文件传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆