ftp data connection problem!!

This is a discussion on ftp data connection problem!! within the Networking/Device Communication forums, part of the General Programming Boards category; hello punters, I am trying to code an FTP indexer in C. here is the code i have written, it ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    58

    ftp data connection problem!!

    hello punters,

    I am trying to code an FTP indexer in C. here is the code i have written, it works fine till the point of the connection for the data socket. Could some one tell me what the problem is here.
    Thanx for your help, very much.
    I have traced it and the point where it gets stuck has been highlighted in color

    Code:
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<stdlib.h>
    #include<string.h>
    #include<unistd.h>
    #include<netdb.h>
    #include<ctype.h>
    #define DEST_PORT 21
    #define MAX 1000
    
    void send_recv(int,char *command);
    void cleanup();
    void parse();
    int getport(); 
    int cmdftp_connect(int);
    char recv_buf[1000];
    char hostname[1000];
    int sockfd,dtsockfd;//socket file descriptor
    char ip_addr[20];
    int port;
    struct hostent *server;
    
    int main()
    {
    //	struct sockaddr_in dest_addr;//destination soket addr
    	strcpy(hostname,"ftp.microsoft.com");
    	
    	sockfd=cmdftp_connect(DEST_PORT);
    
    
    	char send_buf[MAX];
    	char temp_buf[3];
    	if(recv(sockfd,recv_buf,MAX-1,0)==-1)
    	{
    		perror("recieve error\n");
    		exit(1);
    	}
    	printf(recv_buf);
    	cleanup();
    	printf("\n");
    	int i;
    
    	send_recv(sockfd,"user anonymous\012");
    	cleanup();
    
    	send_recv(sockfd,"pass arbit@yahoo.com\012");
    	cleanup();
    
    	send_recv(sockfd,"type a\012");
    	cleanup();
    
    //	send_recv(sockfd,"pasv \012");
    	port=getport();
    	cleanup();
    
    
    
    	------------>dtsockfd=cmdftp_connect(port);//when it enters here
    
    	if(recv(dtsockfd,recv_buf,MAX-1,0)==-1)
    	{
    		perror("recieve error\n");
    		exit(1);
    	}
    	printf(recv_buf);
    	cleanup();
    	printf("\n");
    	
    	send_recv(dtsockfd,"list\012");
    	cleanup();
    	//parse and retrive port address to connect to(ip is same!!)
    	
    	send_recv(sockfd,"quit\012");
    }
    
    
    void cleanup()
    {
    	int i=0;
    	int len=strlen(recv_buf);
    	while(i!=len)
    	recv_buf[i++]='\0';
    	
    }
    void send_recv(int sockfd,char *command)
    {
    	if(send(sockfd,command,strlen(command),0)==-1)
    	{
    		perror("send error\n");
    		exit(1);
    	}
    	if(recv(sockfd,recv_buf,MAX-1,0)==-1)
    	{
    		perror("recieve error\n");
    		exit(1);
    	}
    	printf(recv_buf);
    	printf("\n");
    	
    }
    
    int cmdftp_connect(int port) 
    {
    	struct sockaddr_in address; int s, opvalue; socklen_t slen;
      
    	opvalue = 8; slen = sizeof(opvalue);
    	memset(&address, 0, sizeof(address));
    
    	if ( ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) ||
    	       ((setsockopt(s, IPPROTO_IP, IP_TOS, &opvalue, slen)) < 0) )
    		return 0;
    
    	address.sin_family = AF_INET;
    	address.sin_port = htons((unsigned short)port);
    
    	if (!server)
    		if (!(server = gethostbyname(hostname))) 
    			return 0;
    
    	memcpy(&address.sin_addr.s_addr, server->h_addr, server->h_length);
      
    	if (connect(s, (struct sockaddr*) &address, sizeof(address)) == -1)
    		return 0;//gets stuck here when it enters there!!!
    
    	return s;
    }
    
    int getport() 
    {
    	unsigned int b[6]; 
    	int i, answer; size_t len; char* port_str;
    	//if ((answer = send_command("PASV")) != 227) return 0;
    
    	send_recv(sockfd,"pasv\012");
    	
    	port_str = recv_buf + 4;
    	len = strlen(port_str += 4);
    
    	for (i = 0; i < len; i++) 
    		if (!isdigit(port_str[i])) port_str[i] = ' ';
      
    	if (sscanf(port_str, "%u %u %u %u %u %u", 
    		b, b + 1, b + 2, b + 3, b + 4, b + 5) != 6)
    		return 0;
    
    	return b[4] * 256 + b[5];
    }


    Once again thanx!!!
    even a fish would not have been in danger had it kept its mouth shut.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Replace this:
    Code:
    if ( ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) ||
    	       ((setsockopt(s, IPPROTO_IP, IP_TOS, &opvalue, slen)) < 0) )
    		return 0;
    
    	address.sin_family = AF_INET;
    	address.sin_port = htons((unsigned short)port);
    
    	if (!server)
    		if (!(server = gethostbyname(hostname))) 
    			return 0;
    
    	memcpy(&address.sin_addr.s_addr, server->h_addr, server->h_length);
      
    	if (connect(s, (struct sockaddr*) &address, sizeof(address)) == -1)
    		return 0;//gets stuck here when it enters there!!!
    With this:
    Code:
    struct hostent    *h;
    if((h = gethostbyname(hostname)) == NULL) return(0);
      address.sin_addr = *(struct in_addr*)h->h_addr_list[0];
      address.sin_port = htons(port);
      address.sin_family = AF_INET;
    	// Initiate socket
      if((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) return(0);
    if(connect(s, (struct sockaddr*)&address, sizeof(struct sockaddr)) < 0) return(0);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  3. Problem about Data connection with C++
    By ralph23 in forum C++ Programming
    Replies: 0
    Last Post: 02-15-2006, 05:33 AM
  4. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 11:19 PM

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