[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: why cannot bind to someipaddress:port when something else has *:port bound?
Hola,
That's because when you bind that program to *:1000 it includes
200.47.36.254:1000, so you can't bind there again, remember what
INADDR_ANY means.
On Fri, 1 Jun 2001 horape@tinuviel.compendium.net.ar wrote:
> ¡Hola!
>
> The following program binds *:1000 to a socket, and then tries to bind
> 200.47.36.254:1000 to another socket, the error i gets is "Address
> already in use". Why?
>
> I am not asking for a "you're not allowed to do that", I know. I don't
> ask for a "why are you trying to do that?", I amn't trying. But I need
> to know why that's not permited. I know vaguely but i need a more sound
> explanation. A pointer to a mailing list/usenet archive where the subject
> was discussed in the past would be great.
>
> Just another time, i am asking for the theory about why that shouldn't
> be allowed. Not the fact that it's not allowed.
>
> Lots of thanks,
> HoraPe
>
> The code is:
>
> main()
> {
> l4();
> l4esp();
>
> select(0,NULL,NULL,NULL,NULL);
> }
>
> int l4()
> {
> int listenfd;
> struct sockaddr_in cliaddr, servaddr;
> socklen_t clilen;
>
> listenfd = socket(AF_INET, SOCK_STREAM, 0);
>
> if(listenfd < 0)
> die();
>
> memset(&servaddr, 0, sizeof(servaddr));
> servaddr.sin_family = AF_INET;
> servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
> servaddr.sin_port = htons(1000);
>
> if(bind(listenfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) != 0)
> die();
>
> if(listen(listenfd, 10) != 0)
> die();
>
> }
>
> int l4esp()
> {
> int listenfd;
> struct sockaddr_in cliaddr, servaddr;
> socklen_t clilen;
>
> listenfd = socket(AF_INET, SOCK_STREAM, 0);
>
> if(listenfd < 0)
> die();
>
> memset(&servaddr, 0, sizeof(servaddr));
> servaddr.sin_family = AF_INET;
> servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
> servaddr.sin_port = htons(1000);
>
> if(bind(listenfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) != 0)
> die();
>
> if(listen(listenfd, 10) != 0)
> die();
>
> }
>
> int l4esp()
> {
> int listenfd;
> struct sockaddr_in cliaddr, servaddr;
> socklen_t clilen;
>
> listenfd = socket(AF_INET, SOCK_STREAM, 0);
>
> if(listenfd < 0)
> die();
>
> memset(&servaddr, 0, sizeof(servaddr));
> servaddr.sin_family = AF_INET;
> servaddr.sin_addr.s_addr = htonl(0xc82f24fe);
> servaddr.sin_port = htons(1000);
>
> if(bind(listenfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) != 0)
> die();
>
> if(listen(listenfd, 10) != 0)
> die();
>
> }
>
> die()
> {
> printf("die %s\n", strerror(errno));
> }