|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
Hello.
I am new to C, have to build a basic ftp client for universtity. But I have some big problems, and hope you can help me. When I try to use the LIST command, I get the following message: 425 Unable to build data connection: Connection refused Here is what I have done so far (in a shortend version): .... struct sockaddr_in servAddr; struct sockaddr_in clientAddr; struct sockaddr_in csAddr; ... /* set values for (sockaddr_in) servAdrr */ ... /* create socket */ controlSocket = socket(AF_INET, SOCK_STREAM, 0); /* connect to server */ connect(controlSocket, (struct sockaddr *) &servAddr, sizeof(servAddr)) /* create SOCKET dataSocket */ dataSocket = socket( AF_INET, SOCK_STREAM, 0 ) /* set values for (sockaddr_in) clientAdrr */ bzero((char *) &clientAddr, sizeof(clientAddr)); clientAddr.sin_family = AF_INET; clientAddr.sin_addr.s_addr = htonl(INADDR_ANY); clientAddr.sin_port = htons(0); /* BIND socket to (sockaddr_in) clientAddr */ bind(dataSocket,(struct sockaddr *) &clientAddr, sizeof(clientAddr)); /* LISTEN on data_socket */ listen(dataSocket, numberOfConn); /* send USER command to server over control connection */ strcpy(command,"USER blah"); send(controlSocket, command, strlen(command), 0); /* send PASS command to server over control connection */ strcpy(command,"PASS blah"); send(controlSocket, command, strlen(command), 0); /* send PORT command to server over control connection */ gethostname(port_name, PORTBUFFER); pt_clientAddress = gethostbyname(port_name); host_id = * ((int *) (pt_clientAddress->h_addr_list[0])); host_id = htonl(host_id); sprintf(command, "PORT %d,%d,%d,%d,%d,%d\r\n", (host_id & 0xFF000000) >> 24, (host_id & 0x00FF0000) >> 16, (host_id & 0x0000FF00) >> 8, (host_id & 0x000000FF), (clientAddr.sin_port & 0xFF00) >> 8, (clientAddr.sin_port & 0x00FF)); send(controlSocket, command, strlen(command), 0); /* send MODE command to server over control connection */ strcpy(command,"MODE S\r\n"); send(controlSocket,command, strlen(command), 0); /* send TYPE command to server over control connection */ strcpy(command,"TYPE A\r\n"); send(controlSocket,command, strlen(command), 0); /* send LIST command to server over control connection */ strcpy(command,"LIST\r\n"); send(controlSocket,command, strlen(command), 0); /* create loop to incoming connections */ while(1) { *sin_size = sizeof(struct sockaddr_in); if( (fd = accept(dataSocket,(struct sockaddr *) &csAddr, sin_size )) == -1 ) { fprintf(stderr, "ERROR: can't acceopt connections to dataSocket.\n"); exit(-1); } } So, for all ftp-commands I send, I get a possitive reply from the server. Then comes "LIST", and I get the "425 Unable to build data connection: Connection refused" reply. Can someone of you please tell me, where my error is? What did I initialize wrong? Thanks in advanced for your help Greetings Henning |
|
#2
|
||||
|
||||
|
I can only think of a couple of things off the top of my head, although I'm not really an expert:
1) You are behind a firewall and the incoming connections can't get through. You need to pick a port for dataSocket rather than letting the machine pick, and then allow incoming connections on that port through the firewall, or 2) You are not sending the right info with the PORT command. Try using getsockname() on dataSocket to get the information rather than gethostname(). Also, if you are on a LAN, you might be sending your LAN IP in the port command rather than the WAN one, which the FTP server won't care about if it's on a different network than you, or 3) One of your socket calls is failing. Maybe you should check them for errors. Regardless, the problem is that the FTP server can't connect to the address/port that you specified with the PORT command. That much I know. J.C. |
|
#3
|
|||
|
|||
|
Hey, thanks for your reply!
But I still got some problems. I now use the getsockname() function, in this way: ... /* set values for (sockaddr_in) clientAdrr */ bzero((char *) &clientAddr, sizeof(clientAddr)); clientAddr.sin_family = AF_INET; clientAddr.sin_addr.s_addr = htonl(INADDR_ANY); clientAddr.sin_port = htons(0); //create with a random port number /* BIND socket to (sockaddr_in) clientAddr */ if( bind(dataSocket,(struct sockaddr *) &clientAddr, sizeof(clientAddr)) == -1 ) { fprintf(stderr, "ERROR: bind() of myAddr to socket failed!\n"); exit(-1); } /* GETSOCKNAME: get the client socket name */ *sin_size = sizeof(clientAddr); if( ( getsockname(dataSocket, (struct sockaddr *)&clientAddr, sin_size)) == -1 ) { fprintf(stderr, "ERROR: can't get clientSocketname.\n"); exit(-1); } .... But if I try to get the adress like this: clientAddr.sin_addr; I just get a bunch of zeros, and no nice address. Did I forget to use something, to initialize something? Please help me again! ![]() Thanks Henning |
|
#4
|
||||
|
||||
|
getsockname returns the local port number assigned after binding to random port, not the address. if you want the address you have to connect/accept the socket(or new socket for accept) and then call getsocketname. the reason? when you bind to INADDR_ANY you are telling the kernel you're willing to accept a connection on any address configured on this host. if you had a multi-homed host, this means at the time of binding you have no idea which address the connection will come in on. so you have to wait until you have completed the connection and actually know which address was used.
ps. if you're interested i wrote an ftp library a couple weeks ago. it's on my site found below.
__________________
Last edited by infamous41md : November 19th, 2003 at 02:32 PM. |
|
#5
|
|||
|
|||
|
Ok man, thank you very much. I will try this tomorrow and let you know if it works for me
But I guess it will.Thanks again for the fast reply bye Henning |
|
#6
|
|||
|
|||
|
Ok, getsocketname() didn't work, since there is no function getsocketname() !?!
I used the following stuff to get my IP address: hostent *pt_serverAddress; gethostname((char *)hostname, sizeof(hostname)); if( (pt_serverAddress = gethostbyname((char *)hostname)) == NULL ) { fprintf(stderr, "ERROR: couldn't get ip address of host by using gethostbyname()."); } With that it works fine. Thanks for your help guys bye Henning |
![]() |
| Viewing: Dev Shed Forums > System Administration > FTP Help > C: ftpclient LIST (beginner) problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|