Jump to content
  • Advertisement
Sign in to follow this  
Black_Moon

winsock FTP client

This topic is 4050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing ftp client with winsock library. Client must connect to server, send login and password and transfer a file. There are no problems with winsock initialisation and connectning to server. But when I try to send login and receive answer no data receiving. here is code
BOOL SendData(char* pbuf, int len)
{
	BOOL bres = 1;
	SOCKET _socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == _socket) return 0;		

	sockaddr_in addr; 
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = hkStruct.uAddr;
	addr.sin_port = htons(21);
	
	char buffer[64], sendBuf[32];
	u_char i = 0;	

	if (SOCKET_ERROR != connect(_socket, (SOCKADDR*)&addr, 16))
	{
//		sprintf_s(line, 64, "331 USER %s OK. Password required", hkStruct.szLogin);
		sprintf_s(sendBuf, 32, "USER %s", hkStruct.szLogin);
//		memset(recBuf, 0, 64);	
		
		DWORD recb = 0, flag = 0, transb = 0;

		WSABUF _buf;
		_buf.buf = buffer;
		_buf.len = 1024;

		WSAEVENT _event = WSACreateEvent();
		WSAOVERLAPPED _over;
		memset(&_over, 0, sizeof(WSAOVERLAPPED));
		_over.hEvent = _event;
// receive welcome information		
		while (1)
		{
			WSARecv(_socket, &_buf, 1, &recb, &flag, &_over, 0);
			WSAWaitForMultipleEvents(1, &_event, 0, WSA_INFINITE, 0);
			WSAResetEvent(_event);
			WSAGetOverlappedResult(_socket, &_over, &transb, 0, &flag);
			 
			if (64 != transb) 
			{
				
				break;
			}
		}
		
		WSAResetEvent(_event);
// here send login						
		if (SOCKET_ERROR != send(_socket, sendBuf, 32, 0))
		{
			memset(_buf.buf, 0, 64);
// here receives nothing
			while (1)
			{
				WSARecv(_socket, &_buf, 1, &recb, &flag, &_over, 0);
				WSAWaitForMultipleEvents(1, &_event, 0, 500, 0);
				WSAResetEvent(_event);
				WSAGetOverlappedResult(_socket, &_over, &transb, 0, &flag);
			 
				if (64 != transb) 
				{
				
					break;
				}
			}
		}
		DWORD dw = WSAGetLastError();

		WSACloseEvent(_event);

/*		while (0 == receiveBytes(_socket, &recBuf, 1))								
		{
			if (recBuf == '\r')
			{
//				if (0 != strstr(recBuf, line)) break;
				memset(recBuf, 0, 64);
				i = 0;

				receiveBytes(_socket, &recBuf, 1);		// /n
			}
			else i++;
		}		
		sendBytes(_socket, sendBuf, strlen(sendBuf));		*/
		closesocket(_socket);
	}	
	else bres = 0;

	DWORD dw = WSAGetLastError();
	return bres;
}

Where's bug? Thanks. May be someone help me with link on ftp client topics..

Share this post


Link to post
Share on other sites
Advertisement
Well, I only looked at it briefly, and I don't know how you set hkStruct.szLogin, but I think part of it is that you aren't waiting long enough.

Try telnetting to a public FTP server, for instance:
telnet ftp.loc.gov 21

to see how a typical session runs.

I don't see where you wait for 220, or confirm that you get a 331 after sending the USER command. If you don't mind reading technical documents, have a look at http://www.w3.org/Protocols/rfc959/ which gives an overview of the protocol.

Also what may be helpful is to output every message you send and receive to STDOUT so you can see exactly what is (or isn't) happening.

Sample session:


$ telnet ftp.loc.gov 21
Trying 140.147.249.7...
Connected to ftp.loc.gov.
Escape character is '^]'.
220 rs7 FTP server (Library of Congress) ready.
user ftp
331 Guest login ok, send your complete e-mail address as password.
pass somebody@someemail.com
230 Guest login ok, access restrictions apply.
help
214-The following commands are recognized (* =>'s unimplemented).
USER PORT STOR MSAM* RNTO NLST MKD CDUP
PASS PASV APPE MRSQ* ABOR SITE XMKD XCUP
ACCT* TYPE MLFL* MRCP* DELE SYST RMD STOU
SMNT* STRU MAIL* ALLO CWD STAT XRMD SIZE
REIN* MODE MSND* REST XCWD HELP PWD MDTM
QUIT RETR MSOM* RNFR LIST NOOP XPWD
214 Direct comments to ftp-bugs@rs7.
quit
221 Goodbye.
Connection closed by foreign host.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!