Skip to content

Commit a74c5d0

Browse files
committed
binary type 및 passive mode 명령 전송을 하나의 메소드로 분리함
1 parent 76a5cab commit a74c5d0

3 files changed

Lines changed: 40 additions & 42 deletions

File tree

FtpStack/FtpClient.cpp

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -173,24 +173,7 @@ bool CFtpClient::Upload( const char * pszLocalPath )
173173
}
174174
#endif
175175

176-
if( Send( "TYPE I" ) == false ||
177-
Recv( 200 ) == false )
178-
{
179-
return false;
180-
}
181-
182-
CFtpResponse clsRes;
183-
184-
if( Send( "PASV" ) == false ||
185-
Recv( clsRes, 227 ) == false )
186-
{
187-
return false;
188-
}
189-
190-
std::string strIp;
191-
int iPort;
192-
193-
if( clsRes.GetIpPort( strIp, iPort ) == false )
176+
if( SendBinaryPassive() == false )
194177
{
195178
return false;
196179
}
@@ -200,10 +183,10 @@ bool CFtpClient::Upload( const char * pszLocalPath )
200183
return false;
201184
}
202185

203-
Socket hSocket = TcpConnect( strIp.c_str(), iPort, m_iTimeout );
186+
Socket hSocket = TcpConnect( m_strDataIp.c_str(), m_iDataPort, m_iTimeout );
204187
if( hSocket == INVALID_SOCKET )
205188
{
206-
CLog::Print( LOG_ERROR, "%s TcpConnect(%s:%d) error(%d)", __FUNCTION__, strIp.c_str(), iPort, GetError() );
189+
CLog::Print( LOG_ERROR, "%s TcpConnect(%s:%d) error(%d)", __FUNCTION__, m_strDataIp.c_str(), m_iDataPort, GetError() );
207190
return false;
208191
}
209192

@@ -220,7 +203,7 @@ bool CFtpClient::Upload( const char * pszLocalPath )
220203

221204
if( TcpSend( hSocket, szBuf, iRead ) != iRead )
222205
{
223-
CLog::Print( LOG_ERROR, "%s TcpSend(%s:%d) error(%d)", __FUNCTION__, strIp.c_str(), iPort, GetError() );
206+
CLog::Print( LOG_ERROR, "%s TcpSend(%s:%d) error(%d)", __FUNCTION__, m_strDataIp.c_str(), m_iDataPort, GetError() );
224207
break;
225208
}
226209
}
@@ -272,24 +255,7 @@ bool CFtpClient::Download( const char * pszFileName, const char * pszLocalPath )
272255
}
273256
#endif
274257

275-
if( Send( "TYPE I" ) == false ||
276-
Recv( 200 ) == false )
277-
{
278-
return false;
279-
}
280-
281-
CFtpResponse clsRes;
282-
283-
if( Send( "PASV" ) == false ||
284-
Recv( clsRes, 227 ) == false )
285-
{
286-
return false;
287-
}
288-
289-
std::string strIp;
290-
int iPort;
291-
292-
if( clsRes.GetIpPort( strIp, iPort ) == false )
258+
if( SendBinaryPassive() == false )
293259
{
294260
return false;
295261
}
@@ -299,10 +265,10 @@ bool CFtpClient::Download( const char * pszFileName, const char * pszLocalPath )
299265
return false;
300266
}
301267

302-
Socket hSocket = TcpConnect( strIp.c_str(), iPort, m_iTimeout );
268+
Socket hSocket = TcpConnect( m_strDataIp.c_str(), m_iDataPort, m_iTimeout );
303269
if( hSocket == INVALID_SOCKET )
304270
{
305-
CLog::Print( LOG_ERROR, "%s TcpConnect(%s:%d) error(%d)", __FUNCTION__, strIp.c_str(), iPort, GetError() );
271+
CLog::Print( LOG_ERROR, "%s TcpConnect(%s:%d) error(%d)", __FUNCTION__, m_strDataIp.c_str(), m_iDataPort, GetError() );
306272
return false;
307273
}
308274

@@ -342,6 +308,35 @@ bool CFtpClient::Download( const char * pszFileName, const char * pszLocalPath )
342308
return true;
343309
}
344310

311+
/**
312+
* @ingroup FtpStack
313+
* @brief FTP 서버로 binary type 및 passive mode 명령을 전송한다.
314+
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
315+
*/
316+
bool CFtpClient::SendBinaryPassive( )
317+
{
318+
if( Send( "TYPE I" ) == false ||
319+
Recv( 200 ) == false )
320+
{
321+
return false;
322+
}
323+
324+
CFtpResponse clsRes;
325+
326+
if( Send( "PASV" ) == false ||
327+
Recv( clsRes, 227 ) == false )
328+
{
329+
return false;
330+
}
331+
332+
if( clsRes.GetIpPort( m_strDataIp, m_iDataPort ) == false )
333+
{
334+
return false;
335+
}
336+
337+
return true;
338+
}
339+
345340
/**
346341
* @ingroup FtpStack
347342
* @brief FTP 서버로 명령을 전송한다.

FtpStack/FtpClient.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CFtpClient
4141
bool Download( const char * pszFileName, const char * pszLocalPath );
4242

4343
private:
44+
bool SendBinaryPassive( );
4445
bool Send( const char * fmt, ... );
4546
bool Recv( CFtpResponse & clsResponse, int iWantCode = 0 );
4647
bool Recv( int iWantCode );
@@ -52,6 +53,8 @@ class CFtpClient
5253
bool m_bUseUtf8;
5354

5455
std::string m_strRecvBuf;
56+
std::string m_strDataIp;
57+
int m_iDataPort;
5558
};
5659

5760
#endif

TestFtpStack/TestFtpStack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int main( int argc, char * argv[] )
3333
}
3434
else
3535
{
36-
clsFtp.ChangeDirectory( "/home/test/doc/temp" );
36+
clsFtp.ChangeDirectory( "/home/test" );
3737
clsFtp.Upload( "C:\\Temp\\ÇѱÛ.jpg" );
3838
clsFtp.Download( "ÇѱÛ.jpg", "C:\\Temp\\ÇѱÛ2.jpg" );
3939
}

0 commit comments

Comments
 (0)