工作需要,经常会在工作的台式机和笔记本之间传文件或者需要拷贝文本,两个机器都位于局域网内,传文件或者文本的方式有很多种,之前是通过共享文件夹来进行文件的拷贝,或者通过SVN进行同步。文本传递比较简单,可以通过两台机器上装QQ登两个号码,或者在共享目录下建一个TXT,或者发电子邮件等等。
不过上面这些方法总觉得不直接,所以想基于P2P做一个小的局域网文件和文字传输小工具。
WinForm的工程,界面方面的代码就不贴了,大家自己根据喜好设计就好了,主要把TCP数据传输的代码和逻辑贴出来:
1. 文件和文本传输的通用方法:
private string ReceiveControl(Socket socket)
{
int bufSize = 1024;
byte[] buf = new byte[bufSize];
int len = socket.Receive(buf);
return len > 0 ? Encoding.UTF8.GetString(buf, 0, len) : String.Empty;
}
private void SendControl(Socket socket, string controlMsg){ byte[] msgBytes = Encoding.UTF8.GetBytes(controlMsg); socket.Send(msgBytes);}
private string ReceiveContent(Socket socket, int contentLen){ int receivedLen = 0; int bufSize = 1024; byte[] buf = new byte[bufSize]; StringBuilder sb = new StringBuilder(); while (receivedLen < contentLen) { int len = socket.Receive(buf); if (len > 0) { sb.Append(Encoding.UTF8.GetString(buf, 0, len)); receivedLen += len; } } return sb.ToString();}
private void SendContent(Socket socket, string content){ byte[] contentBytes = Encoding.UTF8.GetBytes(content); SendControl(socket, contentBytes.Length.ToString()); ReceiveControl(socket); socket.Send(contentBytes);}
private void ReceiveFile(Socket socket, string fileName, int fileLen){ string filePath = Path.Combine(GetCurrentUserDesktopPath(), RenameConflictFileName(fileName)); using (Stream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite)) { int bufLen = 1024; int receivedLen = 0; byte[] buf = new byte[bufLen]; int len = 0; while (receivedLen < fileLen) { len = socket.Receive(buf); fs.Write(buf, 0, len); receivedLen += len; } }}
private void SendFile(Socket socket, string filePath){ using (Stream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { SendControl(socket, GetFileNameFromPath(filePath)); ReceiveControl(socket); SendControl(socket, fs.Length.ToString()); ReceiveControl(socket); int bufLen = 1024; byte[] buf = new byte[bufLen]; long readLen = 0; long fileLen = fs.Length; int len = 0; while (readLen < fileLen) { len = fs.Read(buf, 0, bufLen); readLen += len; int sentLen = 0; int realSent = 0; int left = 0; while (sentLen < len) { left = len - realSent; realSent = socket.Send(buf, sentLen, left, SocketFlags.None); sentLen += realSent; } } }}
2.连接,发送文字/文件,重命名文件等方法:
private void SendText()
{
if (connected)
{
if (!String.IsNullOrEmpty(this.TextToSend.Text))
{
string txt = this.TextToSend.Text;
SendControl(clientSocket, "Text");
ReceiveControl(clientSocket);
SendContent(clientSocket, txt);
ReceiveControl(clientSocket);
}
}
}
private void Connect()
{
try
{
if (!connected)
{
passive = false;
IPAddress serverIPAddress = IPAddress.Parse(this.ServerIPAddress.Text);
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(serverIPAddress, 60000);
string msg = ReceiveControl(clientSocket);
if (msg.Equals("Connected"))
{
this.ConnectBtn.Text = "Disconnect";
connected = true;
}
}
else
{
passive = true;
SendControl(clientSocket, "Disconnect");
clientSocket.Close();
this.ConnectBtn.Text = "Connect";
connected = false;
}
}
catch (Exception err)
{
MessageBox.Show(string.Format("Failed to connect to server, error: {0}", err.ToString()));
}
}
private void ServerThread()
{
IPAddress local = IPAddress.Parse("0.0.0.0");
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(new IPEndPoint(local, 60000));
server.Listen(1);
while (true)
{
Socket receivedClientSocket = server.Accept();
IPEndPoint clientEndPoint = (IPEndPoint)receivedClientSocket.RemoteEndPoint;
SendControl(receivedClientSocket, "Connected");
if (passive)
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(clientEndPoint.Address, 60000);
string msg = ReceiveControl(clientSocket);
if (msg.Equals("Connected"))
{
connected = true;
this.ConnectBtn.Text = "Disconnect";
this.ServerIPAddress.Text = clientEndPoint.Address.ToString();
}
}
while (connected)
{
string msg = ReceiveControl(receivedClientSocket);
switch (msg)
{
case "Disconnect":
receivedClientSocket.Close();
clientSocket.Close();
this.ConnectBtn.Text = "Connect";
passive = true;
connected = false;
break;
case "Text":
SendControl(receivedClientSocket, "Received");
int length = Convert.ToInt32(ReceiveControl(receivedClientSocket));
SendControl(receivedClientSocket, "Received");
string content = ReceiveContent(receivedClientSocket, length);
SendControl(receivedClientSocket, "Received");
this.TextToSend.Text = content;
break;
case "File":
SendControl(receivedClientSocket, "Received");
string fileName = ReceiveControl(receivedClientSocket);
SendControl(receivedClientSocket, "Received");
int fileLen = Convert.ToInt32(ReceiveControl(receivedClientSocket));
SendControl(receivedClientSocket, "Received");
ReceiveFile(receivedClientSocket, fileName, fileLen);
SendControl(receivedClientSocket, "Received");
MessageBox.Show("File Received");
break;
}
}
}
}
private string GetFileNameFromPath(string path)
{
int index = path.LastIndexOf('\\');
return path.Substring(index + 1);
}
private string RenameConflictFileName(string originalName)
{
string desktopPath = GetCurrentUserDesktopPath();
int extensionIndex = originalName.LastIndexOf(".");
string fileName = originalName.Substring(0, extensionIndex);
string extensionName = originalName.Substring(extensionIndex + 1);
int renameIndex = 1;
string newNameSuffix = String.Format("({0})", renameIndex);
string finalName = originalName;
string filePath = Path.Combine(desktopPath, finalName);
if (File.Exists(filePath))
{
finalName = String.Format("{0} {1}.{2}", fileName, newNameSuffix, extensionName);
filePath = Path.Combine(desktopPath, finalName);
}
while (File.Exists(filePath))
{
renameIndex += 1;
string oldNameSuffix = newNameSuffix;
newNameSuffix = String.Format("({0})", renameIndex);
finalName = finalName.Replace(oldNameSuffix, newNameSuffix);
filePath = Path.Combine(desktopPath, finalName);
}
return finalName;
}
private string GetCurrentUserDesktopPath()
{
return Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
}
运行截图:
完整代码可以到下面的地址下载:
http://download.csdn.net/detail/qwertyupoiuytr/9895436