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 7758681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:25:26+00:00 2026-06-01T13:25:26+00:00

I’m making an ftp server, and when i’m trying to connect to it with

  • 0

I’m making an ftp server, and when i’m trying to connect to it with filezilla, the server is not accepting the connection on the passive socket. It hangs at the accept call.
Here’s a part of my code :

if ((server->pasv_sock = accept(server->sockt, (struct sockaddr*)&sin_clt,
(socklen_t*)&size_sin) == -1))

My socket is bind to a specific port and the client tries to connect with this one. Telnet is not connecting either.

If you can help me find what’s wrong, thank you 🙂

  • 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-01T13:25:28+00:00Added an answer on June 1, 2026 at 1:25 pm

    Did you remember to call listen before accept?

    Remember: socket -> bind -> listen -> accept.

    Edit: Here’s some commentary on your code.

      struct protoent       *pe;
      struct sockaddr_in    sin;
      int                   sock;
      struct sockaddr_in    sin_clt;
      int                   size_sin;
    
      if ((pe = getprotobyname("TCP")) == NULL)
          perro("getprotobyname");
      sock = xsocket(AF_INET, SOCK_STREAM, pe->p_proto);
    

    Using getprotobyname is unnecessary, since the name is hard-coded and there’s only one IP stream protocol anyway. Use 0 instead of pe->p_proto, and don’t bother calling getprotobyname.

      sin.sin_family = AF_INET;
      sin.sin_addr.s_addr = INADDR_ANY;
    

    You don’t initialize sin.sin_port. That’s an error.

      while (bind(sock, (const struct sockaddr*)&sin,
                  (socklen_t)sizeof(sin)) == -1 && server->port2 <= 65535)
        sin.sin_port = htons(server->port2++);
    

    This loop is a bit of a mess. It might be mostly correct, but it’s hard to tell. Let’s rewrite it so it’s obviously correct rather than not obviously incorrect.

      if (listen(sock, 1) == -1)
        perror("listen");
      size_sin = sizeof(sin_clt);
      if ((server->pasv_sock = accept(sock, (struct sockaddr*)&sin_clt,
                                      (socklen_t*)&size_sin) == -1))
    

    This definitely doesn’t do what you want.

    Discussion: I’ll add parentheses to the last line to show you what it actually does.

    if ((server->pasv_sock = accept(...) == -1))
    

    is the same as

    if ((server->pasv_sock = (accept(...) == -1)))
    

    I’m guessing that you got a compiler warning that complained about the assignment, which suggested adding parentheses… but the parentheses you added were in the wrong place. The reason the compiler was warning you was because this is a common source of errors, and it’s impossible for the compiler to know what you actually mean by that statement. You mean something more like this:

    if ((server->pasv_sock = accept(...)) == -1)
    

    But I don’t recommend that. Easier to read and more fool-proof is to pull assignments out of if predicates,

    server->pasv_sock = accept(...);
    if (server->pasv_sock == -1)
    

    And no, there’s no difference in the resulting assembly code; so there’s no performance difference.

    There’s another problem with this line, but it’s somewhat pedantic. You shouldn’t cast (socklen_t *) &size_sin. Instead, you should change the declaration of size_sin to have the socklen_t type to begin with. The only reason it works is because socklen_t is typedefed to int, but pretend you don’t know that and use the right type to begin with.

    Sample code:

    int port, sock, r, csock;
    struct sockaddr_in saddr, caddr;
    socklen_t caddrlen;
    
    // This is a simpler way to get a TCP socket
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) abort();
    
    // Loop over available ports, and bind to one
    // (I'm not sure if this is the best way to do this)
    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = INADDR_ANY;
    for (port = 10000; port < 65536; ++port) {
        saddr.sin_port = htons(port);
        r = bind(sock, (struct sockaddr *) &saddr, (socklen_t) sizeof(saddr));
        if (!r)
            break;
    }
    if (r) abort();
    
    // Listen and accept a connection
    r = listen(sock, 1);
    if (r < 0) abort();
    caddrlen = (socklen_t) sizeof(caddr);
    csock = accept(sock, (struct sockaddr *) &caddr, &caddrlen);
    if (csock < 0) abort();
    
    // You don't want to listen for more connections
    close(sock);
    

    Reccomendations: For now, try to stay away from putting side effects (accept, bind, assignments, etc.) in conditionals. I’m not saying that it’s never okay to do this, but it looks like that’s where your problems are and it’s very easy to just move the side effects to a separate line of code, then only do the final comparison in the if condition or while condition.

    // Both of these are correct.
    // The bottom one is obviously correct.
    // Correctness is not always good enough.
    // Being obviously correct is important!
    
    if ((p->x = func()) == NULL)
        ...
    
    p->x = func();
    if (p->x == NULL)
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.