说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

如何用C#写Ftp文件上传方面的程序????????

编辑:说三道四文库 发布时间:2017-06-26 06:32
HTML文档下载 WORD文档下载 PDF文档下载
gz
up
我有一些代码资料,要的话留下e-mail地址。
echomyself@china.com
我要,谢谢
我:tcyun@21cn.com
俺也要了,fangzhenyi@263.net
谢了
三个人都发了。
to: CForce()
我收到了,可是什么也没有,既没有附件,也没有任何文字,能再发一遍吗?
echomyself@china.com 
or 
javanow@21cn.com
thanks
to javanow(苏珊娜别哭泣) and  gap435(晕):
不好意思,两位,检查了一下,给你们的邮件真的无附件。重发了,这次应该不会错了。
收到了。谢谢!!!
:)
up
我也想要!!
rdiw@21cn.com
谢谢!!
给我一份 huangbo_email@263.net
up
watch@39.net
thanks!
我也要,哈哈,没来晚吧:)

webmaster@fm960.com
我也要
lnyes@sina.com
我要~~~~~~~~~~~~~~
谢谢~~

milkmouse@etang.com
我也想要philpsun@hotmail.com
我也要
huang_leon@etang.com
可以给我也发一个么???
wftree@21cn.com
xu_zhi77@sohu.com
namespace FTP_Server
{
    using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Threading;
    /// <summary>
    ///    Server Implementing some of the FTP Protocols
    /// </summary>
    public delegate void CloseFTP();
public delegate void Connected(Thread oThread,string IP);
public delegate void Disconnected(Thread oThread,string IP);
public delegate void Disconnect(Thread oThread);
public class FTPServer
    {
        private TCPListener FTPCommandListner;
private Socket ClientSocket;
private static int ClientID=0;
private string ClientIP;
private static int intPort;
private bool bClose=false;
//Declare a Private Delegate that will be raised when client ask for Passive mode
//for Data Transfer
//private delegate Socket PassiveSocket(ref TCPListener clientDataListner);
//Declare a reference to an Client delegate
//Register the delegate to raise event for the Console update
private OnMsgArrived onMsgArrived=new OnMsgArrived(Console.ClientConnect);

public FTPServer()
{

}

public void Dispose()
{
if (FTPCommandListner!=null)
{
bClose=true;
Thread.CurrentThread.Join(1000);
if(FTPCommandListner.Pending())
{
FTPCommandListner.Stop();
}
//FTPCommandListner.Stop();
}
}


public bool Close
{
set
{
bClose=value;
}
}
public Thread Start()
{
Thread startFTPServer=new Thread(new ThreadStart(Run));
startFTPServer.Start();
return startFTPServer;
}

public void EndSession()
{
if (FTPCommandListner!=null)
{
bClose=true;
FTPCommandListner.Stop();
}
}

private void  Run()
        {
Thread ClientThread;
FTPCommandListner=new TCPListener(21);
FTPCommandListner.Start();
            try
{
while (true)
{
ClientSocket=FTPCommandListner.Accept();
ClientThread=new Thread(new ThreadStart(FTPClientThread));
ClientIP=ClientSocket.RemoteEndpoint.ToString();
//Raise Event
ClientID++;
onMsgArrived(ClientID,ClientIP);
Connected clientConnected=new Connected(Console.Connected);
clientConnected(ClientThread,ClientIP);
ClientThread.Start();
}
}
catch(ThreadInterruptedException e)
{
Thread.CurrentThread.Abort();
}
catch(ThreadStopException e)
{
System.Console.WriteLine("Thread killed");
}
        }

private void FTPClientThread()
{
int Port=0;
string user="",dir="/";
string PresentDirOnFTP="/";
string rootDirOnSystem="C:";
int ClientID =  FTPServer.ClientID;
TCPListener FTPDataListner=null;
TCPClient FTPDataClient=null;
Socket ClientSocket=this.ClientSocket;
Socket PassiveClientDataSocket=null;
bool PassiveMode=false;
string serverMsg="";
string strIP=ClientSocket.RemoteEndpoint.ToString();
try

//Open a Buffered Network Stream for PI(Protocol Interpreter) IN.
NetworkStream inBuffer=new NetworkStream(ClientSocket,FileAccess.Read);
//Open a Buffered Network Stream for PI(Protocol Interpreter) OUT.
NetworkStream outBuffer=new NetworkStream(ClientSocket,FileAccess.Write);
//Open a Buffered Network Stream for DTP (Data Transfer Protocol).
NetworkStream nwClientData;
string oldFileName="";
//Send Greeting to Client
serverMsg="220 FTP server[FTP Server in C# by Pramod Kumar Singh]\r\n" ;
SendMsg(serverMsg,ref outBuffer);
bool done = false;
    while(!done)
{  
//break for some time
//other wise with the loop the CPU is 100%
Thread.Sleep(100);
if(!bClose)
{
string clientMsg ;
//Poll on the Socket to see do we have data to read
clientMsg=ReadMsgFromBuffer(ref ClientSocket,ref inBuffer);
string strSwitchExpression=clientMsg.Length==0?"": clientMsg.Substring(0,4).Trim();
//Parse the FTP Command from Client.
//Tested all the Commands here with Microsoft Command Prompt FTP .
strSwitchExpression=strSwitchExpression.ToUpper();
switch(strSwitchExpression)
{
case "USER":
Console.ClientConnect(ClientID,clientMsg);
user = clientMsg.Substring(4).Trim();
SendMsg("331 Password\r\n",ref outBuffer);
break;
case "PASS":
Console.ClientConnect(ClientID,clientMsg);
SendMsg("230 User "+ user +" logged in\r\n",ref outBuffer);
break;
case "XCWD":
goto case "CWD";
case "CDUP":
Console.ClientConnect(ClientID,clientMsg);
//Move up one directory
string changeToDir ="..";
SendMsg(ChangeDirectory(rootDirOnSystem,ref PresentDirOnFTP,changeToDir),ref outBuffer);
break;
case "XCUP":
//Move up one directory
Console.ClientConnect(ClientID,clientMsg);
changeToDir ="..";
SendMsg(ChangeDirectory(rootDirOnSystem,ref PresentDirOnFTP,changeToDir),ref outBuffer);
break;
case "CWD":
Console.ClientConnect(ClientID,clientMsg);
changeToDir =clientMsg.Substring(3).Trim();
SendMsg(ChangeDirectory(rootDirOnSystem,ref PresentDirOnFTP,changeToDir),ref outBuffer);
break;
case "PORT":
Console.ClientConnect(ClientID,clientMsg);
PortData(clientMsg,out Port,out strIP);
PassiveMode=false;
SendMsg("200 PORT command successful\r\n",ref outBuffer); 
break;
case "PASV":
Console.ClientConnect(ClientID,clientMsg);
FTPDataListner=null;
int intPassivePort=PassiveModePort(ref FTPDataListner);
string strIPAddress=ClientSocket.RemoteEndpoint.ToString();
//Remove the client Port
strIPAddress=strIPAddress.IndexOf(":")>0?strIPAddress.Substring(0,strIPAddress.IndexOf(":")):strIPAddress;
strIPAddress=strIPAddress.Replace(new string(".").ToChar(), new string(",").ToChar());
strIPAddress = strIPAddress + "," + intPassivePort / 256 + "," + (intPassivePort % 256);
SendMsg("227 Entering Passive Mode (" + strIPAddress + ")\r\n",ref outBuffer);
PassiveClientDataSocket=PassiveClientSocket(ref FTPDataListner,intPassivePort);
if(PassiveClientDataSocket==null)
{
SendMsg("425 Error in Passive Mode connection\r\n",ref outBuffer);
}
PassiveMode=true;
break;
case "TYPE":
//Emulate the TYPE Command
Console.ClientConnect(ClientID,clientMsg);
SendMsg("200 type set\r\n",ref outBuffer);
break;
case "RETR":
//Send the requested file to Client.
//Raise event for the Console Window to register the request
Console.ClientConnect(ClientID,clientMsg);    
SendMsg("150 Binary data connection\r\n",ref outBuffer);
clientMsg = clientMsg.Substring(4).Trim();
//Open the requested file for Transfer.
clientMsg=clientMsg.Replace(new string("\\").ToChar(),'/');
clientMsg=(clientMsg.Substring(0,1)=="/"?clientMsg.Substring(1):clientMsg);
string strPath = rootDirOnSystem+ PresentDirOnFTP;
strPath=(strPath.Substring(strPath.Length-1,1)=="/"?strPath:strPath+"/");
strPath+=clientMsg;
nwClientData= Mode(PassiveMode,ref FTPDataClient,ref PassiveClientDataSocket,strIP,Port);
if(SendFile(strPath,ref nwClientData))
{
SendMsg("226 transfer complete\r\n",ref outBuffer);
}
else
{
SendMsg("550 file not found, or no access.\r\n",ref outBuffer);
}
PassiveMode=false;
break;
case "STOR":
//Create File on the FTP Server
SendMsg("150 Binary data connection\r\n",ref outBuffer);
Console.ClientConnect(ClientID,clientMsg);   
clientMsg= clientMsg.Substring(4).Trim();
nwClientData= Mode(PassiveMode,ref FTPDataClient,ref PassiveClientDataSocket,strIP,Port);
//Open the requested file for Transfer.
clientMsg=clientMsg.Replace(new string("\\").ToChar(),'/');
clientMsg=(clientMsg.Substring(0,1)=="/"?clientMsg.Substring(1):clientMsg);
strPath = rootDirOnSystem+ PresentDirOnFTP;
strPath=(strPath.Substring(strPath.Length-1,1)=="/"?strPath:strPath+"/");
strPath+=clientMsg;
//Open the requested file for Transfer.
if(CreateFile(strPath,ref nwClientData))
{
SendMsg("226 transfer complete \r\n",ref outBuffer);
}
else
{
SendMsg("550 file not found, or no access.\r\n",ref outBuffer);
}
PassiveMode=false;
break;
case "NLST":
goto case "LIST";
case "LIST":
Console.ClientConnect(ClientID,clientMsg);
SendMsg("150 ASCII data\r\n",ref outBuffer);
nwClientData=Mode(PassiveMode,ref FTPDataClient,ref PassiveClientDataSocket,strIP,Port);
ListDirectory(rootDirOnSystem,PresentDirOnFTP,ref nwClientData);
//Close the socket to signal the End of Data Transfer
if(!PassiveMode)
{
nwClientData.Close();
}
else
{
if(PassiveClientDataSocket!=null)
{
nwClientData.Close();
}
}
SendMsg("226 Transfer complete.\r\n",ref outBuffer);
break;
case "XPWD":
goto case "PWD"; 
case "PWD":
Console.ClientConnect(ClientID,clientMsg);
SendMsg("257 " + "\"" + PresentDirOnFTP + "\"" + " is current directory \r\n" ,ref outBuffer);
break;
case "DELE":
//Delete the file from the Server and reply back.
Console.ClientConnect(ClientID,clientMsg);
clientMsg= clientMsg.Substring(4).Trim();
SendMsg(DeleteFileForServer(rootDirOnSystem,PresentDirOnFTP,clientMsg),ref outBuffer);
break;
case "XRMD":
goto case "RMD";
case "RMD":
//Remove Directory
Console.ClientConnect(ClientID,clientMsg);
clientMsg= clientMsg.Substring(4).Trim();
SendMsg(RemoveDirectory(rootDirOnSystem,PresentDirOnFTP,clientMsg),ref outBuffer);
break;
case "XMKD":
goto case "MKD";
case "MKD":
//Make Directory
Console.ClientConnect(ClientID,clientMsg);
clientMsg= clientMsg.Substring(4).Trim();
SendMsg(CreateDirectory(rootDirOnSystem,PresentDirOnFTP,clientMsg),ref outBuffer);
break;
case "NOOP":
SendMsg("200 NOOP command executed.\r\n",ref outBuffer);
break;
case "HELP":
//214 list of supported command
break;
case "ABOR":
goto case "QUIT";
case "QUIT":
Console.ClientConnect(ClientID,clientMsg);
if(PassiveClientDataSocket!=null)
{
PassiveClientDataSocket.Close();
}
if(FTPDataClient!=null)
{
FTPDataClient.Close();
}
SendMsg("221 GOOD BYE\r\n",ref outBuffer);
done = true; 
break;
case "RNFR":
//Rename File
Console.ClientConnect(ClientID,clientMsg);
 oldFileName=FilePath(rootDirOnSystem,PresentDirOnFTP,clientMsg.Substring(4).Trim());
if(File.FileExists(oldFileName))
{
SendMsg("350 Requested file action pending further info \r\n",ref outBuffer);
}
else
{
SendMsg("550 Requested file not found\r\n",ref outBuffer);
oldFileName="";
}
break;
case "RNTO":
Console.ClientConnect(ClientID,clientMsg);
if(oldFileName!="")
{
string strNewFile=FilePath(rootDirOnSystem,PresentDirOnFTP,clientMsg.Substring(4).Trim());
File.Copy(oldFileName,strNewFile);
File.Delete(oldFileName);
}
SendMsg("250 Requested file action completed. \r\n",ref outBuffer);
break;
case "REST":
SendMsg("350 Requested file action pending further info \r\n",ref outBuffer);
break;

case "": //We have recieved blank 
break;
default: //We Don't understand the Command
Console.ClientConnect(ClientID,clientMsg);
SendMsg("500 Command not understood\r\n",ref outBuffer);
break;
}
}
else
{
done=true;
SendMsg("221 Connection forcefull closed. \r\n",ref outBuffer);
outBuffer.Close();
inBuffer.Close();
}
}
Disconnected clientDisconnted=new Disconnected(Console.Disconnected);
clientDisconnted(Thread.CurrentThread,ClientSocket.RemoteEndpoint.ToString());
ClientSocket.Close();
}
catch(ThreadInterruptedException e)
{
//SendMsg("221 Connection forcefull closed. \r\n",ref outBuffer);
//outBuffer.Close();
//inBuffer.Close();
Disconnected clientDisconnted=new Disconnected(Console.Disconnected);
clientDisconnted(Thread.CurrentThread,ClientSocket.RemoteEndpoint.ToString());
bClose=false;
Thread.CurrentThread.Abort();
}
catch (Exception e)
{
  //System.out.println(e);
Console.ClientConnect(ClientID,e.ToString());
}
//At the End make the flag true;
bClose=false;
}
//Interpreat the File Path
private string FilePath(string rootDirOnServer,string PresentDirOfFTP,string fileName)
{
Thread oThread =Thread.CurrentThread;
lock(oThread)
{
//Change to proper file format..
fileName= fileName.Trim();
rootDirOnServer=rootDirOnServer.Trim();
PresentDirOfFTP=PresentDirOfFTP.Trim();
rootDirOnServer=rootDirOnServer.Replace(new string("\\").ToChar(),'/');
PresentDirOfFTP=PresentDirOfFTP.Replace(new string("\\").ToChar(),'/');
fileName=fileName.Replace(new string("\\").ToChar(),'/');
//Remove the PresentDirOfFTP from filename
string root="";
string changeTo="";
//???? Open Point
if(fileName.LastIndexOf("/")>0)
{
//fileName=fileName.Remove(0,PresentDirOfFTP.Length);
fileName=fileName.Substring(0,1)!="/"?"/" +fileName : fileName;
root=rootDirOnServer + fileName;
changeTo=fileName;
}
else
{
root=rootDirOnServer + PresentDirOfFTP;
if (root.Substring(root.Length-1,1)!="/")
{
root+="/";
}
if(fileName.Substring(0,1)=="/")
{
fileName=fileName.Substring(1).Trim();
}
//Error Correction Logic
//??
string tmpRoot=root+fileName;
try
{
if(Directory.DirectoryExists(tmpRoot))
{
//Change to this Directory
root=tmpRoot;
}
else
{
//Not found try with System Root
tmpRoot=fileName.Substring(0,1)!="/"?"/" +fileName : fileName;
tmpRoot=rootDirOnServer + tmpRoot;
if(Directory.DirectoryExists(tmpRoot))
{
root=tmpRoot;
}
else
{
//Directory doen't exist...
root+=fileName;
}
}

}
catch(Exception e)
{
//No Directory Exist
root+=fileName;
}
}
return root;
}
}
//Rename the File
private string RenameTheFile(string fileName)
{
Thread oThread=Thread.CurrentThread;
lock(oThread)
{
try
{
if(File.FileExists(fileName))
{

}
}
catch(Exception e)
{
}
return null;
}
}
//Remove Directory
private string RemoveDirectory(string rootDirOnServer,string PresentDirOfFTP,string fileName)
{
Thread oThread=Thread.CurrentThread;
lock(oThread)
{
rootDirOnServer=rootDirOnServer.Replace(new string("\\").ToChar(),'/');
PresentDirOfFTP=PresentDirOfFTP.Replace(new string("\\").ToChar(),'/');
fileName=fileName.Replace(new string("\\").ToChar(),'/');
string root="";
root=rootDirOnServer + PresentDirOfFTP;
if (root.Substring(root.Length-1,1)!="/")
{
root+="/";
}
if(fileName.Substring(0,1)=="/")
{
fileName=fileName.Substring(1).Trim();
}
root+=fileName;
try
{
if(Directory.DirectoryExists(root))
{
Directory.Delete(root,true);
return "250 Directory deleted.\r\n";
}
else
{
return "550 Directory not found, or no access.\r\n";
}
}
catch(Exception e)
{
return "550 Command can't be executed.\r\n";
}
}
}
//Create Directory Command
private string CreateDirectory(string rootDirOnServer,string PresentDirOfFTP,string fileName)
{
Thread oThread =Thread.CurrentThread;
lock(oThread)
{
//Change to proper file format..
string root=FilePath(rootDirOnServer,PresentDirOfFTP,fileName);
try
{
if(Directory.DirectoryExists(root))
{
return "550 Directory already exists!\r\n";
}
else
{
Directory.CreateDirectory(root);
return "257 Directory Created.\r\n";
}
}
catch(Exception e)
{
Console.ClientConnect(ClientID,e.ToString());
return "550 Command can't executed.\r\n";
}
}
}
//Check for the directory
private string ChangeDirectory(string rootDirOnServer,ref string PresentDirOfFTP,string fileName)
{
Thread oThread =Thread.CurrentThread;
lock(oThread)
{
bool bUp =false;
string root=FilePath(rootDirOnServer,PresentDirOfFTP,fileName);
if(fileName.Length>=2)
{
if(fileName.Substring(0,2)==".." ||fileName.IndexOf("..")>0)
{
//change to parent dir..
//int iPos=fileName.IndexOf("..");
//string strChangeDir=fileName.Substring(iPos);
if(PresentDirOfFTP.Length>1)
{
string temp="";
string [] updir=fileName.Split(new char[] {'/'});
int iDirectoryUp=updir.Length;
int iStart=PresentDirOfFTP.Length;
for(int i=0;i<iDirectoryUp;i++)
{
int iPosUnder = PresentDirOfFTP.LastIndexOf("/",iStart);
temp = PresentDirOfFTP.Substring(0,iPosUnder);
iStart=iPosUnder-1;
if(iStart<=0)
{
break;
}
}
//string reverse=PresentDirOfFTP;
//StringBuilder t;
fileName=temp;
}
else if (PresentDirOfFTP.LastIndexOf("/")==0)
{
//We are at root
fileName="";
}
root=rootDirOnServer + fileName;
bUp=true ;
}
}
try
{
//Directory oDir=new Directory(root);
if(Directory.DirectoryExists(root))
{
//fileName=fileName.Substring(0,fileName.Length);
if (PresentDirOfFTP.Substring(PresentDirOfFTP.Length-1,1)=="/")
{
PresentDirOfFTP=PresentDirOfFTP.Substring(0,PresentDirOfFTP.Length-1);
}
if(bUp)
{
PresentDirOfFTP=root.Remove(0,rootDirOnServer.Length);
if(PresentDirOfFTP=="")
{
PresentDirOfFTP="/";
}
}
else
{
PresentDirOfFTP=root.Remove(0,rootDirOnServer.Length);
}
return "250 CWD command succesful\r\n";
}
else
{
Console.ClientConnect(ClientID,"Directory not found, or no access." + fileName);
return "550 Directory not found, or no access.\r\n";
}
}
catch(IOException e)
{
Console.ClientConnect(ClientID,e.ToString());
return "550 Directory not found, or no access.\r\n";
}
}
}
//Check for the FileName 
private string DeleteFileForServer(string rootDirOnServer,string PresentDirOfFTP,string fileName)
{
//check for seperator
Thread oThread =Thread.CurrentThread;
lock(oThread)
{
string root=FilePath(rootDirOnServer,PresentDirOfFTP,fileName);
try
{
File oFile=new File(root);
if(oFile.FullName!="")
{
oFile.Delete();
return "250 delete command successful\r\n";
}
}
catch(FileNotFoundException e)
{
Console.ClientConnect(ClientID,e.ToString());
return "550 file not found, or no access.\r\n";
}
catch(IOException e)
{
Console.ClientConnect(ClientID,e.ToString());
return "550 file not found, or no access.\r\n";
}
Console.ClientConnect(ClientID,"Error in Deleteing file " + fileName);
return "550 file not found, or no access.\r\n";
}
}
//Directory Listing
private bool ListDirectory(string rootDirOnSystem,string PresentDirOnFTP ,ref NetworkStream nw)
{
//Open the Directory
string strPath=rootDirOnSystem + PresentDirOnFTP;
string strFileNameTemp="";
Directory oDir=new Directory(strPath);
File[] oFiles=oDir.GetFiles();
Directory[] oDirectories= oDir.GetDirectories();
try
{
foreach(File oFile in oFiles)
{
string strFile;
strFile="-rwxr--r-- 1 owner group ";
try
{
if(oFile.Name.Substring(oFile.Name.Length-4)!=".SYS")
{
strFileNameTemp=oFile.Name.Replace(new String("\\").ToChar(),'/');
//strFileNameTemp=strFileNameTemp.Remove(0,strPath.Length);
strFile+= oFile.Length + " " + oFile.LastWriteTime.Format("MMM dd  yyyy",null).ToString() + " "  + strFileNameTemp.Trim() + "\r\n";
byte[] Buffer=Encoding.ASCII.GetBytes(strFile);
try
{
if(nw.CanWrite)
{
nw.Write(Buffer,0,Buffer.Length);
}
}
catch(Exception e)
{

}
}
}
catch(Exception e)
{
//Some Error in Reading the Files Information
}
}
foreach(Directory od in oDirectories)
{
string strDirectory;
strDirectory ="drwxr-xr-x 1 owner group ";
if(od.IsDirectory)
{
strFileNameTemp=od.Name.Replace(new String("\\").ToChar(),'/');
//strFileNameTemp=strFileNameTemp.Remove(0,strPath.Length);
strDirectory+="  0  " + "  " + od.CreationTime.Format("MMM dd  yyyy",null).ToString() + "  " + strFileNameTemp.Trim() +  "\r\n";
byte[] Buffer=Encoding.ASCII.GetBytes(strDirectory);
try
{
if(nw.CanWrite)
{
nw.Write(Buffer,0,Buffer.Length);
}
}
catch(Exception e)
{

}
}
}
}
catch(IOException e)
{
Console.ClientConnect(ClientID,e.ToString());
return false;
}
return true;
}
//Check For the MODE Passive or Port
private NetworkStream Mode(bool PassiveMode,ref TCPClient client,ref Socket clientSocket,string strIP,int Port)
{
Thread oThread=Thread.CurrentThread;
NetworkStream nw=null;
lock (oThread)
{
if(PassiveMode)
{
//Socket is Opened
if (clientSocket!=null)
{
nw=new NetworkStream(clientSocket,FileAccess.ReadWrite);
}
}
else
{
//Client Need to be Connected
client=new TCPClient(strIP,Port);
nw=client.GetStream();
}
}
return nw;
}
//Read Message from Network Stream
private string ReadMsgFromBuffer(ref Socket clientSocket,ref NetworkStream inBuffer)
{
string clientMsg="";
StringBuilder clientTmp=new StringBuilder();
byte[] buffer=new Byte[1024];
int iBytes=0;
//inBuffer.Flush();
Thread oThread=Thread.CurrentThread;
lock(oThread)
{
string tmp="";
/*
iBytes=inBuffer.Read(buffer,0,buffer.Length);
tmp=Encoding.ASCII.GetString(buffer,0,iBytes);
clientTmp.Append(tmp);
*/
if(clientSocket.Available > 0)
{
while(clientSocket.Available>0)
{
iBytes=inBuffer.Read(buffer,0,buffer.Length);
tmp=Encoding.ASCII.GetString(buffer,0,iBytes);
clientTmp.Append(tmp);
}
}
clientMsg=clientTmp.ToString();

return clientMsg;
}
//Send Message through Network Stream
private void SendMsg(string msg,ref NetworkStream outBuffer)
{
Thread oThread=Thread.CurrentThread;
lock(oThread)
{
byte[] buffer;
buffer=Encoding.ASCII.GetBytes(msg);
outBuffer.Write(buffer,0,buffer.Length);
}
}

//Read Data from File and Send to the Client
private bool SendFile(string strpath,ref NetworkStream nw)
{

Thread oThread=Thread.CurrentThread;
try
{
lock(oThread)
{
StreamReader outFile=new StreamReader(strpath);

char[] buff=new Char[1024];
int amount;
while((amount = outFile.Read(buff,0,1024)) != 0)
{       
byte[] buffer = Encoding.ASCII.GetBytes(buff);
nw.Write(buffer,0,amount);
}
}
}
catch( Exception e )
{
Console.ClientConnect(ClientID,e.ToString());
nw.Close();
return false;

nw.Close();
return true;
}

//Write Data to File and Send to the Client
private bool CreateFile(string strpath,ref NetworkStream nw)
{
Thread oThread=Thread.CurrentThread;
lock(oThread)
{
try
{
StreamWriter inFile=new StreamWriter(strpath);
byte[] buffer=new Byte[128];
int iBytes=1;
while(iBytes != 0)
{
string tmp="";
iBytes=nw.Read(buffer,0,buffer.Length);
char[] buff=Encoding.ASCII.GetChars(buffer);
inFile.Write(buff,0,buff.Length);
}
inFile.Close();
}
catch(Exception e )
{
nw.Close();
return false;
}
nw.Close();
return true;
}
}

//Passive Data MODE
private int PassiveModePort(ref TCPListener clientDataListner)
{
Thread oThread = Thread.CurrentThread;
lock(oThread)
{
int intPort=0; 
bool done=true;
while (done)
{
intPort=Port();
try
{
if(clientDataListner!=null)
{
clientDataListner.Stop();
}
clientDataListner=new TCPListener(intPort);
clientDataListner.Start();
//This Port is free
done=false;
}
catch (Exception e)
{
//done=false;
}
}
return intPort;
/*
strIPAddress = CStr(wscControl.LocalIP)
strSend = "PORT " & Replace(strIPAddress, ".", ",")
strSend = strSend & "," & intPort \ 256 & "," & (intPort Mod 256)
strSend = strSend & vbCrLf
wscControl.SendData strSend
*/
}
}

//Passive Mode Data Transfer Listner.
private Socket PassiveClientSocket(ref TCPListener clientDataListner,int intPort)
{
Thread oThread=Thread.CurrentThread;
lock(oThread)
{
try
{
if(clientDataListner.LocalEndpoint==null)
{
bool done=false;
Socket s=null;
try
{
s= clientDataListner.Accept();
done=true;
}
catch(Exception e)
{

}
return s;
}
else
{
Socket s= clientDataListner.Accept();
return s;
}
}
catch(Exception e)
{
Console.ClientConnect(ClientID,e.StackTrace);
}
return null;
}
}
//Get Free Port Numeber from Operating System
private int Port()
{
if(intPort==0)
{
  intPort = 1100;

else
{
intPort++;
}
return  intPort;
}
//Process PORT Command and get the IP and Port to Connect 
//for Data Transfer
private void PortData(string clientMsg,out int Port,out string strIP)
{
Thread oThread=Thread.CurrentThread;
lock (oThread)
{
//string strDataAddress = clientMsg.Replace(',', '.', 1, 3);
Port=0;
//Remove the FTP Command
clientMsg=clientMsg.Remove(0,5);
//Remove the last two character
clientMsg=clientMsg.Substring(0,clientMsg.Length-2);
int iLen =clientMsg.Length;
int iCounter=0;
StringBuilder IP=new StringBuilder();
string clientPort="";
for(int i=0;i<iLen;i++)
{
if (clientMsg.Substring(i,1)==",")
{
iCounter++;
if(iCounter==4)
{
clientPort=clientMsg.Substring(i+1);
break;

else
{
IP.Append(clientMsg.Substring(i,1));
}
}
else
{
IP.Append(clientMsg.Substring(i,1));
}
}
//IP of the Client
strIP=IP.ToString().Replace(',','.');
string[] strPort=clientPort.Split(new Char[]{','});
if (strPort.Length>0)
{
Port =(int)(strPort[0].ToDecimal() * 256 + strPort[1].ToDecimal());
}
/*
iPos = InStr(1, strDataAddress, ",")
strIP = Left$(strDataAddress, iPos - 1)
lPort = CLng(Mid$(strDataAddress, iPos + 1, InStr(iPos + 1, strDataAddress, ",") - iPos))
lPort = lPort * 256
lPort = lPort + CLng(Mid$(strDataAddress, InStrRev(strDataAddress, ",") + 1))
     */
}
}
    }
}
都发了!
谢了~~~
cforce()大哥留个联系方式,好吗?
我也要,有没有来晚呀,谢谢

icemxh@263.net
我也要 cmuwyb@21cn.com.
没有来晚吧!
Yelp处理1.02亿/月绝对造访人次及3900万评论的省钱秘诀 可视化:图像的主题色提取算法,是不是太高端了? 不利于写出好代码的15个职场因素 GPU Saturday技术沙龙:分享异构计算新技术及应用 微软隐藏最深的秘密武器之一——Visual Studio可作为Python IDE 历时六月,Qt 5.1正式版终于面世! HTML5全硬件加速游戏引擎:Sphinx正式公测 专访“Monster Blade”创始人:无需营销,百万下载背后的秘密 Twitter实时分析平台主要合作伙伴Dataminr:让用户不再错过重要的推文 OpenStack、OpenNebula、Eucalyptus、CloudStack社区活跃度比较 XOR的黄色大象:Erasure Code为Hadoop节省数据恢复带宽 竞争将越来越激烈:戴尔或也进军可穿戴计算市场 新技术:可直接在人脑播放广告 编程调试和诊断的五大规则 70%开发者:对现有应用推广和广告并不满意 四种方案:将OpenStack私有云部署到Hadoop MapReduce环境中 扩展PHP的五个简单技巧,让单服务器并行支撑30000用户 硅谷为什么无法复制 PubNub+Cloudinary:简单几步快速搭建实时图片分享应用 影响互联网行业的十大API(信息图) Hadoop+GPU强强联手的性能探索 Eurora获最节能超算殊荣,中国天河2号位列第31 程序员的野心:让GPU像CPU一样运行 Gungho热潮正在逐渐消退? Thalmic创始人专访:穿戴式体感设备MYO背后的开发故事 移动周报:GSMA各种神器,Qt 5.1,Android 4.3,本周惊喜多多! 奇虎360Web平台部基础架构团队访谈:开源线上数据库中间件Atlas 化繁为简:UC优视发布“UC+”开放平台战略 从40亿美元到10亿美元 Hulu的美梦是否又一次成为泡影? 百度开发者俱乐部第六期:BAE3.0开放内测 云编程马拉松启动 微软宣布将于今年9月30日正式关闭MSN TV服务 我安装7.1时候选择了chinese(tw)语言,但是安装好后还是英文界面? 急寻深圳FDF高手,JSP(懂LINX)高手! 为什么我的SQLServer2000(企业版正版)不时出现坏表? ACM音频压缩 bcb6 控件的下载及应用 求救!!我用VB写的windows服务为什么不能运行?快来看看 这个文本文件的数据如何才能传送到数据库表中? 奇怪的现象,Crystal Report 8.5设计器中图表无法显示中文? 各位,谁有比较完整清晰的C#(Asp.net)编码规范,可否给小弟一份,谢谢。赠送100分。 關于 線程內使用數據庫的問題 Unix的文件名字符长度能否超过14个?为什么? 类初始化的问题。求教 数据窗口倒出存为excel格式时,如何倒出表头 请高手指教!!! 如何使用DLL(在线等待) 终于配置成功了jboss应用服务器 动态报表设计 备份日志语句! 98的MSDOS程序都不能运行了,说与系统连接的设备不能运行了,怎么解决?急!!! 问个色彩寄存器的问题 关于WINDOWS服务定时运行的问题,求救!!!!。。 加急!如何只将文档发送给[代办事宜]中,而在邮件数据库中不留痕迹? initialize the Borland Database Engine (error $2501) 带小数点的数字用什么变量类型? 谁用过DHTMLEDIT控件?帮帮忙介绍一下用法。 该死的ACCESS 怎样编写服务器接受客户端的相应程序 我的机子安装完WIN2000后,访问其它的计算机就死机,别的机子访问我机子的时候,我的机子也会死机。 怎样合并两个表??????? 一个有关ISampleGrabber接口调用的问题!Please HELP ME... 如何访问客户端指定路径上的ACCESS数据库? 可以远程安装win2k服务器版么?(各位帮忙!) 还有1。2的考试吗 一条sql语句! 我能在w2k下为非PnP串口设备写驱动吗? 哪位可以解释一下,构造函数到底有什么作用??? 难道没有高手帮我解决这个问题吗?mysql 版主请进 菜单如何显示在工具栏之上? 有关线程之间交换信息的问题 为什么在WIDNOWS98下用MASM50编译的汇编程序不能运行? sql如何自动定时清空日志?(在线等待!) 游戏 有没有办法用纯Javascript取得<form>里面用post方法post过来的数据?100分在线等候!!!绝对给分!!!!! 大家好,级联菜单怎么做呀?再线等待!!!急 邮件发送不了??? 一个落后的问题,用DAO能链接sql server数据库吗?如果可以,怎么链接!? PB中用代码建立ODBC数据源问题。 response中的问题 大家好。 我是新人。。。。。。。 关于用vc操作word中表格的问题 我的机子运行了几个用bde连接数据库的程序,在delphi里就打开不了src了出现以下错误! You need to clean the classroom.(改为被动句) I was sorry to hear that he had a coid last week这句话什么意思 选词填空 She knew better than(to turn,turned,turning,going to turn) around or show panic.感觉这里省略了什么,She knew it is better to do..than to turn...1,to turn ,以前听过 than 后面加原形的说法,如果这里有原形 turn,2, we need the whole day to clean the classroom,对clean the classroom提问( ) ( ) do you need the whole day ( )( ) 请专业英语教师回答:-----the old man was killed last week.---A.what a pity.B.sorry to hear that.请问怎么选?根据是什么? She's going to give her a dog对还是She's going to give her dog.her那个是人称代词…… 如何下载cctv9英语新闻到MP3 How's it going?I hope that grandpa is well now,I was sorry to hear that he had a cold laHow's it going?I hope that grandpa is well now,I was sorry to hear that he had a cold last week I hope you are in good health .Things are fine here,I finished my i am going to turn on them i am going to turn them on 都对吧 He ___ the classroom now.He can clean it this afternoon.A、don`t need clean B、need not to cleanC、need not clean D、needs not clean正确答案是选C,而我选B,这些真弄不清楚了, 大家都看cctv9的新闻吗?谁提供一些英语新闻高频词? l have enough money to buy her a CD.(同一句)i _____ ______ _____ _____to buy a CD for her. 3.——will it take us to get there by bus?3.( )will it take us to get there by bus?about two hours A.How soon B.How long C.How often D.How far 为什么呢 will 是将来时的 用 how soon应该也可以地把 how soon 是问多久的亚 cctv4 和cctv9都在播英语新闻,这两者有什么区别呢 homiez中文意思 be busy with 之后的动词需不需要加ing? CCTV9英语新闻中,有几句话,能否帮忙听写下.(一)http://english.cntv.cn/program/newsupdate/20110414/108572.shtml0:30-0:48处,记者Guan Xin,她说的话能否帮忙写一下. A:What's the matter_you?B:I feel_.I have a fever.A:i'm sorry_hear that You should_ B:Thank you! 日本地震的感悟 CCTV9英语新闻中,有几句话,能否帮忙听写下.(二)1:06-1:19处,采访Eric Pang,他说的话能否帮忙写一下. what's the matter i have a cold.可以把问句换成what‘s your matter 1. I am sorry I know _____ English. A. little of B.few of C. a little D.a few2.There are _____ American students in that school.A. a little B. a few C. a lot D.a lots3.Do you know the boy sitting between Prter and _____?A. she B i know a little english中文什么意思 there are three__in our classA.german B.germans C.german's D.germen这个题选什么,说下理由~! I'm sorry .I'm ____ late.A.a lot of B.a little of C.a little D.a few 划线提问I know a little English about qu yuan————---- ----- do you know about Qu Yuan两个格子在do的前面 There are three windows in our class.同义句? 请翻 but they will take their cars for repair at the first sign of a problems The river rose by two feet after the heavy rain是什么时态 如果是过去完成时态怎么rose前没有have那可以为一般现在时态吗 The river rise by two foot after the rain there are 45 students in our class,i_____ three Englishmen. they took(the old mowan)to the hospital.( 提问) ( ) ( ) they( )to the hospital. _______(unfortunate) i don't have enough money to travel abroad. Heavy rains ____the river.(rise raise) They found the hospital easily翻译______________(________) Practice English with your classmates every day是什么意思? He can speak some English,but not ____A.many B.lot C.a little D.much the sign tells us not to park cars here.怎样译这个句子? persuade your friends to do morning exercise with you every day的英文稿子 Most people have little chance to (p ) for the earthquake. She gives her seat to the woman in need.(画线部分提问)画线部分为in need.请在30分钟内回答问题,还有一句:Please give him a pen.(改为同义句) 英语作文 是every your da y 还是 your every day如题 I'm sorry ____(hear) that you are ill. the plains of North America of rainfall怎么翻译,两个of I believe __with people in English every day is of great help to improve your English?to communicate B communicating那个正确,为什么? jane has three ping-pong balls.对后面的句子提问 in need 在 she aiways gives her seat ta someone in need做什么成分,同义词是什么? 上海的英语机构比较的好是哪家? 什么食物含优质蛋白 when she is on the bus, she always ( ) her seat to someone in need. 解释!A givesB giveC gaveD giving 在上海哪里的英语教学机构比较好? 哪些食物含有优质的蛋白质? Do you know__ about English?- No,__ do i know.A many,few B much,little C.much,nothingD a lot,a few正确答案为B,我选的是C,请解释C中nothing为什么不可以,是不是因为little在前,句子要倒装?要写理由!是不是因为little Little dose she know what may happen改为正常语序 优质蛋白食物有哪些 Linda has four ping-pong balls.对画线句子提问.画线句子是ping-pong balls. I don't know _____.A.from what city she comes B.what city she comes from 选A还是B?为什么A不行? My friends a________ Tom and Jack.根据句意及首字母提示完成单词. Mr li has t------- in our school for about twenty years I know what she t_____ of .根据首字母写出单词.没有上下文,只有这一句,填什么? Tom,Jack and Luce ___ my friends.A isB are选哪个?为什么?这里不用“就近原则”吗?那什么时候用啊?
备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘