Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Thu Jul 18, 2019 6:21 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: TCPSocket changing port after connect?
PostPosted: Wed Dec 12, 2012 7:33 pm 
Offline

Joined: Thu Oct 12, 2006 7:21 am
Posts: 415
Location: California
Hi guys,

Weird thing happening to me. My code is below. FTPSocketClass just subclasses TCPSocket.

//Make connection to the passive port.
ftpPassiveSocket = new FtpSocketClass
ftpPassiveSocket.Port = passivePort1
ftpPassiveSocket.Address = passiveHost
ftpPassiveSocket.infoWin = new infoWindow()
ftpPassiveSocket.Connect()

MsgBox "Connecting To:"+ftpPassiveSocket.Address+":"+str(ftpPassiveSocket.Port)


I'm working on a FTP app, but when I try to connect to a port other than 21, it fails to connect. If you know a lot about FTP you know it chooses random port ranges for it's passive FTP mode. So I have to make a connection to random ports on the server. So I did a msgbox command to see where it was trying to connect and for some reason it's always 65,300+ (seems to always increment by 1 or 2), when I know for a fact it's some lower numbers like 41 or 221 or something random it seems in that range. Is it because these are privileged ports that I am not allowed to even connect to an outside server? I can understand listening on my own server and having it change my port randomly, but connecting to an outside server I thought would not be an issue, but apparently it is?

I'm very confused, any light on the matter would help. I don't claim to know what I'm doing with FTP either, I may be misunderstanding how passive mode vs. active mode setup works.

_________________
Joe Astrahan
CEO - iTechware Inc.
iTechware - Internet, Technology & Software
Google Adwords Campaign Management
"Rocketing you to the top!"
10900 Los Alamitos Blvd, Suite #210
Los Alamitos, CA, 90720
(949)354-4ADS


Top
 Profile  
Reply with quote  
 Post subject: Re: TCPSocket changing port after connect?
PostPosted: Wed Dec 12, 2012 7:50 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
That sounds normal to me. When you connect to a service like that, it needs to keep its main port, port 21 in this case, available, so it switches you off to a random port in the range of "free" ports. That sounds consistent with what you're seeing. Is this causing a problem in your code?


Top
 Profile  
Reply with quote  
 Post subject: Re: TCPSocket changing port after connect?
PostPosted: Wed Dec 12, 2012 8:19 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
The port you set (e.g. 21) is the remote port. After calling Connect() the Port property is changed to represent the local port that RS is using, not the remote port which you're connecting to.

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: TCPSocket changing port after connect?
PostPosted: Wed Dec 12, 2012 8:56 pm 
Offline

Joined: Thu Oct 12, 2006 7:21 am
Posts: 415
Location: California
It just makes it hard to verify that the socket is correctly binding to the right address on the other server, but I think I get what you guys are talking about now. I did a test of connecting on port 21, and noticed it changed the 'port' value as well, I suppose for connections with ports, there is the port you connect to on the server machine and a port for listening on your machine and they don't have to be the same apparently. I guess I didn't understand that until now.

I'm trying to connect to one of the ports that my FTP server is telling me should be open for the passive connection but it just sits there and is never able to connect, so not sure what to do now. I already successfully logged into my FTP server sent username and password, got current directory, just getting this double socket setup seems to be difficult.

For example, the FTP server responds:

227 Entering Passive Mode (64,13,232,147,206,183)

The first 4 comma's are the IP Address, the other are 2 random ports it's supposed to have opened. I believe I should be able to connect to those, but I can't for some reason, not sure why. I set the correct host and port on a new tcpsocket but it justs hangs when trying to connect, not even any errors.

EDIT: http://www.rhinosoft.com/respcode.asp?resp=227&prod=fv

Apparently according to this I misunderstood what the port stuff meant.

I guess I'm supposed to take the first port value on there multiply by 256 then add the second port and THAT is the port I'm supposed to connect to. Wish they made that clear in the documentation. Anyways going to test that out and let you know if it works.

Essentially it's this.

Quote:
227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).

The values of h1-h4 are the IP address the server is listening on. The values of p1-p2 are used to calculate the port the server is listening on using the following formula: PASV port = (p1 * 256) + p2.

_________________
Joe Astrahan
CEO - iTechware Inc.
iTechware - Internet, Technology & Software
Google Adwords Campaign Management
"Rocketing you to the top!"
10900 Los Alamitos Blvd, Suite #210
Los Alamitos, CA, 90720
(949)354-4ADS


Top
 Profile  
Reply with quote  
 Post subject: Re: TCPSocket changing port after connect?
PostPosted: Wed Dec 12, 2012 9:05 pm 
Offline

Joined: Thu Oct 12, 2006 7:21 am
Posts: 415
Location: California
Sweet, now it works, I guess that was my problem was the calculating of the passive ports, that was really unintuitive!

_________________
Joe Astrahan
CEO - iTechware Inc.
iTechware - Internet, Technology & Software
Google Adwords Campaign Management
"Rocketing you to the top!"
10900 Los Alamitos Blvd, Suite #210
Los Alamitos, CA, 90720
(949)354-4ADS


Top
 Profile  
Reply with quote  
 Post subject: Re: TCPSocket changing port after connect?
PostPosted: Wed Dec 12, 2012 11:04 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
x11joex11 wrote:
that was really unintuitive!

You said it! That really tripped me up too.
x11joex11 wrote:
Wish they made that clear in the documentation.



Here's the unofficial FTP docs I relied on when implementing my FTP client code. I found it most helpful.

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: TCPSocket changing port after connect?
PostPosted: Thu Dec 13, 2012 6:04 am 
Offline

Joined: Thu Oct 12, 2006 7:21 am
Posts: 415
Location: California
Thanks man, that really helps :), I got it working tonight just now, got it uploading files to a location :).

_________________
Joe Astrahan
CEO - iTechware Inc.
iTechware - Internet, Technology & Software
Google Adwords Campaign Management
"Rocketing you to the top!"
10900 Los Alamitos Blvd, Suite #210
Los Alamitos, CA, 90720
(949)354-4ADS


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group