C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 03-04-2005, 01:07 AM   #1
Registered User
 
Join Date: Feb 2005
Location: south africa
Posts: 7
recv from ftp server (winsock2)

Hi

I've been struggling to connect to a FTP server for some time now, and I really need some help. Here's my code (cut down to just the basics)

Code:
#include <stdio.h>
#include<winsock2.h>

int main(int argc, char* argv[])
{
	WORD VersionNumber;
	WSADATA WinSockData;
	VersionNumber = MAKEWORD(2, 0);
	WSAStartup(VersionNumber, &WinSockData);  

	sockaddr_in Server_addr;
   	memset(&Server_addr, 0, sizeof(sockaddr_in));
	
	Server_addr.sin_family = AF_INET;
	Server_addr.sin_addr.s_addr = inet_addr("192.168.207.1");
  	Server_addr.sin_port = htons(21);

	SOCKET ServerSocket = socket(AF_INET, SOCK_STREAM, 0);
	
	if(ServerSocket == INVALID_SOCKET)
	{
		printf("error creating socket\n");
		return 0;
	}

	if(connect(ServerSocket, (struct sockaddr*)&Server_addr, sizeof(sockaddr)) == SOCKET_ERROR)
	{
		printf("error connecting socket\n");
		return 0;
	}

	const int BUF_SIZE = 1024;
	char recvBuffer[BUF_SIZE]; 

	memset(&recvBuffer, 0, BUF_SIZE);

	if(send(ServerSocket, " ", 1, 0) == SOCKET_ERROR)
		printf("send(1) error\n");
	else
		printf("send(1) success\n");
	
	if(recv(ServerSocket, recvBuffer, BUF_SIZE, 0) == SOCKET_ERROR)
		printf("recv(1) error\n");
	else
		printf("recv(1) success\n");
	
	printf("> %s\n", recvBuffer);
  	memset(&recvBuffer, 0, BUF_SIZE);

	if(send(ServerSocket, " ", 1, 0) == SOCKET_ERROR)
		printf("send(2) error\n");
	else
		printf("send(2) success\n");
	
	if(recv(ServerSocket, recvBuffer, BUF_SIZE, 0) == SOCKET_ERROR)
		printf("recv(2) error\n");
	else
		printf("recv(2) success\n");
	
	printf("> %s\n", recvBuffer);
  	memset(&recvBuffer, 0, BUF_SIZE);

	if(send(ServerSocket, "user n124581", 12, 0) == SOCKET_ERROR)
		printf("send(3) error\n");
	else
		printf("send(3) success\n");
	
	if(recv(ServerSocket, recvBuffer, BUF_SIZE, 0) == SOCKET_ERROR)
		printf("recv(3) error\n");
	else
		printf("recv(3) success\n");
	
	printf(">>> %s\n", recvBuffer);
	
	return 0;
}
when i execute this I get the following output:

Code:
send(1) success
recv(1) success
> 220-FTPD1 IBM FTP CS V1R4 at MVSETE.IT.NEDNET.CO.ZA, 16:49:16 on 2005-03-03.

send(2) success
recv(2) success
> 220 Connection will close if idle for more than 5 minutes.

send(3) success
_
I think i'm supposed to get a return code 331(send password) from the server at this stage, but it just sits there waiting for who knows what. Could this be the server giving me a hard time, because my code looks fine to me?

Any help would be greatly appreciated.
commissar is offline   Reply With Quote
Old 03-04-2005, 03:27 AM   #2
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 3,020
You need to send a newline character at the end of each statement you send to the server.

For instance, you would have:
Code:
if(send(ServerSocket, "user n124581\n", 12, 0) == SOCKET_ERROR)
Also the first two send() statements which simply send a " " are unnecessary.
bithub is offline   Reply With Quote
Old 03-04-2005, 04:16 AM   #3
Registered User
 
Join Date: Feb 2005
Location: south africa
Posts: 7
Ah, thank you, thank you, thank you!

you're the best!
commissar is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
connecting to ftp server (winsock2) commissar C++ Programming 1 03-03-2005 10:22 AM
FTP and Ident Server :: Winsock kuphryn Networking/Device Communication 2 03-13-2004 08:16 PM
Visual Studio and FTP Server... Grayson_Peddie Networking/Device Communication 0 09-03-2003 12:31 PM
FTP Server :: Winsock kuphryn Windows Programming 2 10-03-2002 07:14 PM
socket question Unregistered C Programming 3 07-19-2002 01:54 PM


All times are GMT -6. The time now is 11:38 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22