Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:20:30+00:00 2026-06-14T15:20:30+00:00

I have to write a little command line FTP client for linux in C.

  • 0

I have to write a little command line FTP client for linux in C. It works quite well for common uses (directory managing, retrieving and storing files, etc.), and I want to add the active mode (at the moment every transfer is made under passive mode).
I know that I have to send a command in the shape of :

PORT a,b,c,d,e,f

where a b c d are the ip address blocks and e f the port numbers. However, as I understand it, the ip has to be the ip of the machine on which my client is running, but I’ve been advised to use getsockname(). From what I’ve tested, getsockname() gets me the local ip of the interface(s) used by my socket, not my ip seen from the internet. So I can’t give this ip address for the server to connect.

The question is : am I understanding the command PORT correctly, and how to get the correct ip to send it ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:20:31+00:00Added an answer on June 14, 2026 at 3:20 pm

    FTP active mode means that a server opens a connection to a client and sends data ifself. It’s often unpractical, so the passive mode was invented: server opens an additional port which listens for incoming connections and starts transmission when someone is connected.

    So, a passive mode session looks like this:

    $ telnet localhost 21
    220 Welcome to EarlGray FTP
    USER ftp
    331 Please specify the password.
    PASS ftp
    230 Login successful.
    PASV 
    227 Entering Passive Mode (127,0,0,1,185,37).
    LIST
    150 Here comes the directory listing.
                ---> here client opens another telnet session, 
                ---> connecting to the same server on port 185*256+37, specified by server:
                  $ telnet localhost $((185 * 256 + 37))
                  Trying 127.0.0.1...
                  Connected to localhost.
                  Escape character is '^]'.
                  drwxrwxr-x    2 121      1003         4096 Aug 21 10:57 incoming
                  drwxrwxr-x    7 0        1003         4096 Nov 09 21:04 pub
                  Connection closed by foreign host.
                <---- end of data transfer session
    
    226 Directory send OK.
    

    Whereas an example of an active session:

    $ telnet localhost 21
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    220 Welcome to EarlGray FTP
    USER ftp
    331 Please specify the password.
    PASS ftp
    230 Login successful.
    PORT 127,0,0,1,45,45                      (ports are specified by client)
    200 PORT command successful. Consider using PASV.
    LIST
    150 Here comes the directory listing.
    
            ---> here client listens for an incoming connection on port 45*256+45
              $ nc -l 0.0.0.0 $((45 * 256 + 45))
              drwxrwxr-x    2 121      1003         4096 Aug 21 10:57 incoming
              drwxrwxr-x    7 0        1003         4096 Nov 09 21:04 pub
            <--- data are rececived
    
    226 Directory send OK.
    

    P.S. FTP is a very old protocol (definition from circa 1970), defined when there were no routers, gates and other transport level goodies, usually there were several machines, directly connected, so the active mode worked quite well, the passive mode is how the protocol survives today.

    So, yes, you’ve gotten PORT command right, but there’s no uniform way to get your external IP (there may be several your IPs in several different networks on local machine, there may be several gates with their own networks on the way to server, which one do you want to use?). The second part of the question, how to get your IP as it is seen by server, can’t be answered (that’s what the passive mode is for).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have to write in a file 4bytes representing an integer in little endian
I need to write a little ruby function that does word wrapping. I have
I have very little knowledge about unix. My task is to write a shell
i have a little problem... on win xp, if i write on commandline shutdown
I have a quick little question involving the read() command. I am really rusty
I have write this code but it does work only if I am already
I have write my own view Bar, which extends LinearLayout . My bar has
I am trying to check if I have write access to a specific key
I'm working on an application so i have write an dll which contain a
I'm writing a simple web app in PHP that needs to have write access

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.