LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Could not bind listening socket (https://www.linuxquestions.org/questions/linux-networking-3/could-not-bind-listening-socket-109946/)

chabotrobert 10-29-2003 10:38 AM

Could not bind listening socket
 
RedHat 9
vsftpd running ok through xinetd
verified that listening on port 21

On any client when ftp <servername> with any allowed user we get :
"500 OOPS Could not bind listening socket"

Tried all kind of searches with (usually responsive) Google but got nothing explaining/correcting. If it happened on the server i would understand, the daemon may not be able to start on port 21 if something else is on it etc.... but when trying to connect from another machine ???

Any ideas would be welcomed !

chort 10-29-2003 12:36 PM

Hmmm, the only thing I can think of is that it's using Active Mode FTP by default, which uses port 20 for data. It might be trying to bind to port 20 locally, but if your daemon has dropped root privilages it won't be able to open a port < 1024. Have the clients try the "PASV" or "passive" command immediately after connecting. That *should* use a high port and ought to be able to bind OK.

nehapagare 02-20-2024 02:36 AM

error binding port
 
I have used this code but showing error binding port for all ports please help me with this
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

void error(const char *msg)
{
perror(msg);
exit(1);
}

int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
close(newsockfd);
close(sockfd);
return 0;
}

elgrandeperro 02-24-2024 08:54 PM

I assume your test is a non-privileged user binding to a port >1024 as a daemon? Do you run selinux? If so, I believe you have to add a rule to bind to the port.

wpeckham 02-24-2024 09:37 PM

And just for the record, I hope you mean RHEL 9 and not RedHat 9.
As I recall RH9 went out of support in 2001 or 2002, while RHEL9 was just released in 2022.

michaelk 02-25-2024 12:28 AM

If you look at the date of the OP it is 2003. Post #3 is new and unrelated to the OP.

wpeckham 02-25-2024 10:30 AM

Quote:

Originally Posted by michaelk (Post 6485739)
If you look at the date of the OP it is 2003. Post #3 is new and unrelated to the OP.

Did not look for that, and now I just feel silly! ;-) Thank you michaelk.

teckk 02-25-2024 04:19 PM

Post #3. How about not posting a mess that no one can read.

BindPort.c
Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

void error(const char *msg) {
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
    int sockfd, newsockfd, portno;
    socklen_t clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    int n;
   
    if (argc < 2) {
    fprintf(stderr,"ERROR, no port provided\n");
    exit(1);
    }
   
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
   
    if (sockfd < 0)
        error("ERROR opening socket");
       
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = atoi(argv[1]);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
   
    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
        error("ERROR on binding");
       
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
   
    if (newsockfd < 0)
        error("ERROR on accept");
       
    bzero(buffer,256);
    n = read(newsockfd,buffer,255);
   
    if (n < 0)
        error("ERROR reading from socket");
       
    printf("Here is the message: %s\n",buffer);
    n = write(newsockfd,"I got your message",18);
   
    if (n < 0) error("ERROR writing to socket");
        close(newsockfd);
       
    close(sockfd);
    return 0;
}

//gcc BindPort.c -o BindPort

That appears to work.
Code:

./BindPort 8100

ss -tuna
Netid  State  Recv-Q  Send-Q    Local Address:Port    Peer Address:Port  Process 
tcp    LISTEN  0      5                0.0.0.0:8100          0.0.0.0:*             
tcp    LISTEN  0      128              0.0.0.0:22            0.0.0.0:*             
tcp    LISTEN  0      128                [::]:22              [::]:*

And when making a request on the port.
Code:

./BindPort 8100
Here is the message: GET / HTTP/1.1
Host: 127.0.0.1:8100
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 17_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/119.0.6045.109 Mobile/15E148 Safari/604.1



All times are GMT -5. The time now is 03:24 AM.