recv from ftp server (winsock2)

This is a discussion on recv from ftp server (winsock2) within the Networking/Device Communication forums, part of the General Programming Boards category; Hi I've been struggling to connect to a FTP server for some time now, and I really need some help. ...

  1. #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.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Location
    south africa
    Posts
    7
    Ah, thank you, thank you, thank you!

    you're the best!

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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