// JP opened flex table

Click to See Complete Forum and Search --> : FTP connection problem


duffman.sk
December 14th, 2007, 03:56 PM
Hi I have problem with creating of my ftp client for university. My program cannot create data socket and bind it to port, it always writes "Cannot assign requested address". Here's code of my procedure for connect:

int makeConnection(char server_addr[100], int portno) //function creates connection to server, else returns 1
{

struct hostent *server;
struct hostent *client;
struct sockaddr_in serv_addr; //server address
struct sockaddr_in local_addr; //client address
int return_value;
int len;
char hostname[50];


ctrlSckt = socket(AF_INET, SOCK_STREAM, 0);

return_value=0;

if (ctrlSckt < 0)
{
perror("ERROR opening socket");
return_value=1;
}

server = gethostbyname(server_addr);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
return_value=1;
exit(1);
}

((char *) &serv_addr, sizeof(serv_addr));

serv_addr.sin_family = AF_INET;

bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);

serv_addr.sin_port = htons(portno);


if (connect(ctrlSckt,&serv_addr,sizeof(serv_addr)) < 0) {
perror("ERROR connecting");
return_value=1;
}
cin = fdopen(ctrlSckt, "r");
cout = fdopen(ctrlSckt, "w");


len=sizeof(local_addr);
getsockname(ctrlSckt,(struct sockaddr *)&local_addr,&len);
printf("datapo: %d;;\n",ntohs(local_addr.sin_port));

//creating data socket
dataSckt = socket(AF_INET, SOCK_STREAM, 0);

//get port number of control connection
bzero((char *) &local_addr, sizeof(local_addr));
local_addr.sin_family = AF_INET;

len=sizeof(local_addr);
getsockname(ctrlSckt,(struct sockaddr *)&local_addr,&len);

data_portno=ntohs(local_addr.sin_port)+1;
local_addr.sin_port=htons(data_portno);

gethostname((char *)hostname, sizeof(hostname));
client = gethostbyname((char *)hostname);
bcopy((char *)client->h_addr, (char *)&local_addr.sin_addr.s_addr, client->h_length);

local_addr.sin_addr.s_addr=client->h_addr;

gethostname((char *)hostname, sizeof(hostname));

//bind data socket to port
if (bind(dataSckt,(struct sockaddr *) &local_addr, sizeof(local_addr))<0)
perror("ftp bind");

listen(dataSckt,5);

Can you help me? And don't you know some link to simple ftp client source code so I can inspire? Because I didn't find anything simple..

henky@nok.co.id
December 14th, 2007, 08:22 PM
i have some comments to your code:
1.

server = gethostbyname(server_addr);

See manual of gethostbyname() function. The input argument should be
hostname (type char *). And the important one is you didn't assign any
value to the variable.

2. What do you want to do here?

((char *) &serv_addr, sizeof(serv_addr));


3. You might not use exit() function to terminate the program. Exit() wont'
give other function/procedure to clean up the memory if there is allocated
memory must be clean.

exit(1);


4. What do want to attempt to do with these lines? Do you want to be able
to read and write socket. If yes, you don't have to call these functions().

cin = fdopen(ctrlSckt, "r");
cout = fdopen(ctrlSckt, "w");


Please fix these and show us your fixed code. Good luck.

Henky

duffman.sk
December 15th, 2007, 03:11 AM
1. server = gethostbyname(server_addr);
This function has assigned parameter from function header, there is parameter called "char server_addr[100]" which containes server's address.

4. I know, that I can write to socket without using of this files, but I just found it really simple way, how to do it...

But my biggest problem is, that I still can't bind dataSckt to port on my computer, because I can't get to my client net address or some other way how to do it...

orang_jawa
December 15th, 2007, 03:43 AM
But my biggest problem is, that I still can't bind dataSckt to port on my computer....
Look at these lines:

client = gethostbyname((char *)hostname);
bcopy((char *)client->h_addr, (char *)&local_addr.sin_addr.s_addr, client->h_length);

local_addr.sin_addr.s_addr=client->h_addr; // You can't do this.
In the other lines, you call gethostname() twice and this is not necessary.

gethostname((char *)hostname, sizeof(hostname));
client = gethostbyname((char *)hostname);
bcopy((char *)client->h_addr, (char *)&local_addr.sin_addr.s_addr, client->h_length);

local_addr.sin_addr.s_addr=client->h_addr;

gethostname((char *)hostname, sizeof(hostname));

duffman.sk
December 15th, 2007, 08:05 AM
Thank you, this solved my problem... But I have another one...When I'm trying to create passive connection to FTP server, I send server PASV command, retrieve server's response, extract port number. Then I try to connect to server's data port, which it sent me and I get just error - "Transport endpoint is already connected". Do you know how to solve this problem or what I am doing wrong?

Code of my procedure for connecting to server's data port:


int createConnection() //creates connection to server data port in passive mode
{
struct hostent *server;
struct sockaddr_in serv_addr; //server address
int return_value;
int on=1;

return_value=0;

dataSckt = socket(AF_INET, SOCK_STREAM, 0);

return_value=0;

if (dataSckt < 0)
{
perror("ERROR opening socket");
return_value=1;
}

//serv_addr is server's address typed by user
server = gethostbyname(server_addr);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
return_value=1;
exit(1);
}

serv_addr.sin_family = AF_INET;

bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
printf("portno: %d\n",data_portno);

//data_portno is server's data port number created by 256*port1+port2 from server's PASV response
serv_addr.sin_port = htons(data_portno);

setsockopt(dataSckt,SOL_SOCKET, SO_REUSEADDR,(char *)&on, sizeof (on));
if (connect(ctrlSckt,&serv_addr,sizeof(serv_addr)) < 0) {
perror("ERROR connecting data");
return_value=1;
}
return return_value;
}

//JP added flex table