using System ;
using System. Collections ;
using System. Collections. Generic ;
using System. IO ;
using System. Linq ;
using System. Text ;
using System. Threading. Tasks ;
namespace TcpFile
{
class DirectoryList
{
private static ArrayList directorysList = new ArrayList ( ) ;
public static ArrayList DirectorysList
{
get { return DirectoryList. directorysList; }
set { DirectoryList. directorysList = value ; }
}
private static ArrayList fileList = new ArrayList ( ) ;
public static ArrayList FileList
{
get { return DirectoryList. fileList; }
set { DirectoryList. fileList = value ; }
}
public static void GetDirectory ( string sourcePath)
{
if ( Directory. Exists ( sourcePath) )
{
string [ ] tmp = Directory. GetFileSystemEntries ( sourcePath) ;
for ( int i = 0 ; i < tmp. Length; i++ )
{
if ( File. Exists ( tmp[ i] ) )
{
FileList. Add ( tmp[ i] ) ;
}
else
{
if ( ( Directory. GetDirectories ( tmp[ i] ) ) . Length == 0 )
{
DirectorysList. Add ( tmp[ i] ) ;
}
}
GetDirectory ( tmp[ i] ) ;
}
}
}
}
}
using System ;
using System. Collections. Generic ;
using System. IO ;
using System. Linq ;
using System. Net ;
using System. Net. Sockets ;
using System. Text ;
using System. Threading ;
using System. Threading. Tasks ;
using System. Windows. Forms ;
namespace TcpFile
{
class FileRecive
{
private TcpListener server;
private Form1 fm;
private NetworkStream stream;
string ip;
mFolderBrowserDialog _mFolderBrowserDialog;
public FileRecive ( Form1 fm , string _ip, int _port, mFolderBrowserDialog _mFolderBrowserDialog)
{
this . fm = fm;
try
{
this . ip = _ip;
this . _mFolderBrowserDialog = _mFolderBrowserDialog;
this . server = new TcpListener ( IPAddress. Parse ( _ip) , _port) ;
server. Start ( ) ;
}
catch ( Exception e)
{
fm. Tip ( e. Message) ;
}
}
public void CloseServer ( )
{
server. Stop ( ) ;
}
public void run ( )
{
while ( true )
{
try
{
TcpClient client = null ;
try
{
client = server. AcceptTcpClient ( ) ;
}
catch ( Exception )
{
}
if ( client!= null && client. Connected)
{
this . stream = client. GetStream ( ) ;
byte [ ] msgs = new byte [ 1024 ] ;
int len = this . stream. Read ( msgs, 0 , msgs. Length) ;
string msg = Encoding. UTF8. GetString ( msgs, 0 , len) ;
string [ ] tip = msg. Split ( '|' ) ;
msg = "1" ;
msgs = Encoding. UTF8. GetBytes ( msg) ;
this . stream. Write ( msgs, 0 , msgs. Length) ;
this . stream. Flush ( ) ;
fm. SetState ( "正在接收:" ) ;
string path = null ;
string dir = "" ;
if ( tip[ 3 ] == fileType. File. ToString ( ) )
{
path = this . _mFolderBrowserDialog. GetSavePath ( ) + tip[ 0 ] ;
}
else if ( tip[ 3 ] == fileType. dir. ToString ( ) )
{
path = this . _mFolderBrowserDialog. GetSavePath ( ) + tip[ 1 ] . Substring ( tip[ 1 ] . IndexOf ( ":" ) + 2 ) ;
} else
{
}
dir = Path. GetDirectoryName ( path) ;
if ( ! Directory. Exists ( dir) )
{
Directory. CreateDirectory ( dir) ;
}
FileStream os = new FileStream ( path, FileMode. OpenOrCreate) ;
byte [ ] data = new byte [ 1024 ] ;
long currentprogress = 0 ;
int length = 0 ;
fm. UpDateProgress ( ( int ) currentprogress) ;
fm. UpdateCurrentFileName ( path) ;
if ( client == null || ! client. Connected|| ! stream. CanRead)
{
fm. Tip ( "不能读取" ) ;
}
while ( client. Connected&& stream. CanRead&& ( length = this . stream. Read ( data, 0 , data. Length) ) > 0 )
{
currentprogress += length;
fm. UpDateProgress ( ( int ) ( currentprogress * 100 / long . Parse ( tip[ 2 ] ) ) ) ;
os. Write ( data, 0 , length) ;
}
os. Flush ( ) ;
this . stream. Flush ( ) ;
os. Close ( ) ;
this . stream. Close ( ) ;
}
}
catch ( Exception e)
{
fm. Tip ( e. Message) ;
}
}
}
}
}
using System ;
using System. Collections. Generic ;
using System. IO ;
using System. Linq ;
using System. Net. Sockets ;
using System. Text ;
using System. Threading ;
using System. Threading. Tasks ;
namespace TcpFile
{
class FileSend
{
private TcpClient client;
private NetworkStream stream;
private string [ ] param;
private Form1 fm;
public FileSend ( Form1 fm, params string [ ] param)
{
this . param = param;
this . fm = fm;
}
public void Send ( )
{
try
{
this . client = new TcpClient ( param[ 0 ] , int . Parse ( param[ 1 ] ) ) ;
string msg = param[ 2 ] + "|" + param[ 3 ] + "|" + param[ 4 ] + "|" + param[ 5 ] ;
byte [ ] m = Encoding. UTF8. GetBytes ( msg) ;
while ( true )
{
try
{
this . stream = this . client. GetStream ( ) ;
this . stream. Write ( m, 0 , m. Length) ;
this . stream. Flush ( ) ;
byte [ ] data = new byte [ 1024 ] ;
int len = this . stream. Read ( data, 0 , data. Length) ;
msg = Encoding. UTF8. GetString ( data, 0 , len) ;
if ( msg. Equals ( "1" ) )
{
fm. SetState ( "正在发送:" ) ;
FileStream os = new FileStream ( param[ 3 ] , FileMode. OpenOrCreate) ;
data = new byte [ 1024 ] ;
long currentprogress = 0 ;
len = 0 ;
fm. UpDateProgress ( ( int ) currentprogress) ;
fm. UpdateCurrentFileName ( param[ 3 ] ) ;
while ( ( len = os. Read ( data, 0 , data. Length) ) > 0 )
{
currentprogress += len;
fm. UpDateProgress ( ( int ) ( currentprogress * 100 / long . Parse ( param[ 4 ] ) ) ) ;
this . stream. Write ( data, 0 , len) ;
}
Thread. Sleep ( 1000 ) ;
os. Flush ( ) ;
this . stream. Flush ( ) ;
os. Close ( ) ;
this . stream. Close ( ) ;
client. Close ( ) ;
}
}
catch ( Exception ex)
{
return ;
}
}
}
catch ( Exception e)
{
fm. Tip ( e. Message) ;
}
}
}
}
using System ;
using System. Collections. Generic ;
using System. Linq ;
using System. Net ;
using System. Text ;
using System. Threading. Tasks ;
namespace TcpFile
{
class IpUtil
{
public static string GetIPAddress ( )
{
IPHostEntry ipe = Dns. GetHostEntry ( Dns. GetHostName ( ) ) ;
IPAddress[ ] ip = ipe. AddressList;
for ( int i = 0 ; i < ip. Length; i++ )
{
if ( ip[ i] . AddressFamily. ToString ( ) . Equals ( "InterNetwork" ) )
{
return ip[ i] . ToString ( ) ;
}
}
return null ;
}
public static string GetLocalIp ( )
{
IPHostEntry localhost = Dns. GetHostEntry ( Dns. GetHostName ( ) ) ;
IPAddress localaddr = localhost. AddressList[ 0 ] ;
return localaddr. ToString ( ) ;
}
public static int GetRandomPort ( )
{
return new Random ( ) . Next ( 1000 ) + 5000 ;
}
}
}
namespace TcpFile
{
public enum fileType
{
File,
dir,
none
}
}
using System ;
using System. Collections. Generic ;
using System. IO ;
using System. Linq ;
using System. Text ;
using System. Threading. Tasks ;
using System. Windows. Forms ;
namespace TcpFile
{
class mFolderBrowserDialog
{
Form1 fm;
private string Savepath;
private string SendPath;
private string fileName;
private string filePath;
private long fileSize;
public mFolderBrowserDialog ( Form1 fm)
{
this . fm = fm;
}
public void SetSavePath ( )
{
FolderBrowserDialog dialog = new FolderBrowserDialog ( ) ;
dialog. Description = "请选择接收文件的路径" ;
if ( dialog. ShowDialog ( ) == DialogResult. OK)
{
Savepath = dialog. SelectedPath + "\\" ;
fm. UpdateText ( ) ;
}
}
public string GetSavePath ( )
{
return Savepath;
}
public void SetSendPath ( )
{
FolderBrowserDialog dialog = new FolderBrowserDialog ( ) ;
dialog. Description = "请选择文件路径" ;
if ( dialog. ShowDialog ( ) == DialogResult. OK)
{
SendPath = dialog. SelectedPath + "\\" ;
}
}
public string GetSendPath ( )
{
return SendPath;
}
public void SelectFile ( )
{
OpenFileDialog dig = new OpenFileDialog ( ) ;
dig. ShowDialog ( ) ;
this . fileName = dig. SafeFileName;
this . filePath = dig. FileName;
if ( filePath != null && filePath. Length != 0 )
{
FileInfo f = new FileInfo ( this . filePath) ;
this . fileSize = f. Length;
}
}
public string GetSelectfileName ( )
{
return fileName;
}
public string GetSelectfilePath ( )
{
return filePath;
}
public long GetSelectfileSize ( )
{
return fileSize;
}
}
}
using System ;
using System. Collections. Generic ;
using System. ComponentModel ;
using System. Data ;
using System. Drawing ;
using System. IO ;
using System. Linq ;
using System. Text ;
using System. Threading ;
using System. Threading. Tasks ;
using System. Windows. Forms ;
namespace TcpFile
{
public partial class Form1 : Form
{
public Form1 ( )
{
InitializeComponent ( ) ;
}
private void Form1_Load ( object sender, EventArgs e)
{
LocalIPAdress = IpUtil. GetIPAddress ( ) ;
Netport = IpUtil. GetRandomPort ( ) ;
selfIPText. Text = LocalIPAdress;
selfPortText. Text = Netport. ToString ( ) ;
IPtext. Text = LocalIPAdress;
UpDateProgress ( 0 ) ;
_mFolderBrowserDialog = new mFolderBrowserDialog ( this ) ;
this . FormClosed += FrmMain_FormClosing;
}
private void FrmMain_FormClosing ( object sender, FormClosedEventArgs e)
{
}
bool serverStared = false ;
FileRecive s;
private void StartRecieve_Click ( object sender, EventArgs e)
{
string SavePath = _mFolderBrowserDialog. GetSavePath ( ) ;
if ( SavePath == null || SavePath. Length == 0 )
{
Tip ( "请选选择接收文件存储路径" ) ;
return ;
}
Thread. CurrentThread. IsBackground = true ;
Thread StartThread = null ;
if ( serverStared == false )
{
try
{
s = new FileRecive ( this , LocalIPAdress, Netport, _mFolderBrowserDialog) ;
StartThread = new Thread ( s. run) ;
StartThread. Start ( ) ;
StartRecieve. Text = "服务器已开启" ;
serverStared = true ;
}
catch ( Exception )
{
throw ;
}
}
else {
s. CloseServer ( ) ;
serverStared = false ;
StartRecieve. Text = "开启服务器" ;
UpDateProgress ( 0 ) ;
try
{
StartThread. Abort ( ) ;
}
catch ( Exception )
{
}
StartThread = null ;
}
}
private void SelectFilePathbtn_Click ( object sender, EventArgs e)
{
_mFolderBrowserDialog. SelectFile ( ) ;
SelectedFilePathText. Text = _mFolderBrowserDialog. GetSelectfilePath ( ) ;
IsFile = true ;
}
private void selectDirPathbtn_Click ( object sender, EventArgs e)
{
_mFolderBrowserDialog. SetSendPath ( ) ;
selectDirPathText. Text = _mFolderBrowserDialog. GetSendPath ( ) ;
DirectoryList. GetDirectory ( _mFolderBrowserDialog. GetSendPath ( ) ) ;
IsFile = false ;
}
private void Sendbtn_Click ( object sender, EventArgs e)
{
if ( selectDirPathText. Text != null && selectDirPathText. Text. Length != 0 && ! IsFile)
{
SendAllFile ( ) ;
}
else if ( SelectedFilePathText. Text != null && SelectedFilePathText. Text. Length != 0 && IsFile)
{
SendFile ( ) ;
}
else {
Tip ( "其他" ) ;
}
}
#region
private string LocalIPAdress;
private int Netport;
private mFolderBrowserDialog _mFolderBrowserDialog;
private bool IsFile = false ;
public void Tip ( string msg)
{
MessageBox. Show ( msg, "温馨提示" ) ;
}
private void SendFile ( )
{
string ip = IPtext. Text;
string port = Porttext. Text;
if ( _mFolderBrowserDialog . GetSelectfileName ( ) == null || _mFolderBrowserDialog. GetSelectfileName ( ) . Length == 0 )
{
Tip ( "请选择文件" ) ;
return ;
}
if ( ip. Length == 0 || port. ToString ( ) . Length == 0 )
{
Tip ( "端口和ip地址是必须的!" ) ;
return ;
}
var c = new FileSend ( this , new string [ ] { ip, port, _mFolderBrowserDialog. GetSelectfileName ( ) , _mFolderBrowserDialog. GetSelectfilePath ( ) , _mFolderBrowserDialog. GetSelectfileSize ( ) . ToString ( ) , fileType. File. ToString ( ) } ) ;
new Thread ( c. Send) . Start ( ) ;
}
private void SendAllFile ( )
{
string ip = IPtext. Text;
string port = Porttext. Text;
if ( ip. Length == 0 || port. ToString ( ) . Length == 0 )
{
Tip ( "端口和ip地址是必须的!" ) ;
return ;
}
foreach ( string item in DirectoryList. FileList)
{
FileInfo f = new FileInfo ( item) ;
var c = new FileSend ( this , new string [ ] { ip, port, f. Name, item, f. Length. ToString ( ) , fileType. dir. ToString ( ) } ) ;
new Thread ( c. Send) . Start ( ) ;
}
}
public void UpDateProgress ( int value )
{
Action< int > ShowMsg = Msg => {
this . progressBar1. Value = Msg;
this . numlable. Text = Msg + "%" ;
Application. DoEvents ( ) ;
} ;
this . Invoke ( ShowMsg, value ) ;
}
public void SetState ( string state)
{
Action< string > ShowMsg = Msg => {
Currentlable. Text = Msg;
} ;
this . Invoke ( ShowMsg, state) ;
}
#endregion
private void SavePathbtn_Click ( object sender, EventArgs e)
{
_mFolderBrowserDialog. SetSavePath ( ) ;
}
public void UpdateText ( )
{
SavePathtext. Text = _mFolderBrowserDialog. GetSavePath ( ) ;
}
public void UpdateCurrentFileName ( string _fileName)
{
Action< string > ShowMsg = Msg => {
CurrentFileName. Text = "当前文件名字是 : " + Msg;
} ;
this . Invoke ( ShowMsg, _fileName) ;
}
}
}