PDA

View Full Version : how to bind on server with multiple address & Ethernet c


dev
04-09-2003, 01:10 PM
hi all,

i have a server(linux eight) with 2 Ethernet cards and thus 2 ip address, one for access to local lan(with address- "...38") and other is connected to Internet using RF link(with address- "...60").
when i assign INADDR_ANY to address while binding, the ip address on "...38" is binded and when a client connects to it, all work fine.

i want to use "...60", so i tried to assign the address "...60" using inet_addr("...60"), but when i run it , i get error as :-
"cannot assign requested address"

why?,
i am using telnet as terminal,
and even when i try to ping "...60", it does not reply.
so can it be due to security restrictions?

if its so, what and all changes in what and all files have to made?

if i have enough powers, can i hope to bind on "...60"?( at present i am not su, but i am also not using reserved ports, but still , the prob is there).

dev

RobSeace
04-09-2003, 01:56 PM
Well, binding to INADDR_ANY should allow incoming connections on
any/all interfaces/IPs...

But, yes, you should also be able to just bind to one specific IP, as
well... If it's not allowing it, I have to think either you've got the IP
wrong, or maybe the interface is not up, or SOMETHING... Can you
post some code showing how you're trying to bind(), exactly? And,
also, verify that you've got the correct IP for the interface and that
it's up, by checking "ifconfig" output...

dev
04-09-2003, 02:17 PM
hi Rob,

the code is:-

1
2 // ftp_server.c
3 // the server part of the ftp_XXX application
4 // will be binded on the 192.168.1.205/192.168.1.38
5 // on the specified port scaned from the user
6
7 # include<stdio.h>
8 # include<sys/types.h>
9 # include<sys/socket.h>
10 # include<netinet/in.h>
11 # include<fcntl.h>
12
13
14 int main()
15 {
16 int c,s;
17 int lenstruct;
18 int SERVER_PORT;
19 void *buf[150];
20 struct sockaddr_in serveraddr,clientaddr;
21 struct sockaddr_in tempaddr;
22 char saddr[32]={"162.112.127.60"};
23

24 s=socket(AF_INET,SOCK_STREAM,0);
25 if(s==-1) // error
26 {
27 perror("\n error in socket : ");
28 exit(-1);
29 }
30
31 printf("\n after socket");
32
33 printf("\n enter the port number : ");
34 scanf("%u",&SERVER_PORT);
35
36 serveraddr.sin_family=AF_INET;
37 serveraddr.sin_port=htons(SERVER_PORT);
38 serveraddr.sin_addr.s_addr=inet_addr(saddr);
39
40 if( bind(s,(struct sockaddr *)&serveraddr,sizeof(struct sockaddr
_in))==-1) //error
41 {
42 perror("\n error in bind : ");
43 exit(-1);
44 }
45

46 printf("\n after bind ");
47
48 lenstruct=sizeof(struct sockaddr_in);
49
50 if( getsockname(s,(struct sockaddr*)&tempaddr,&lenstruct) == -1)
// error
51 {
52 perror("\n error in getsockname : ");
53 exit(-1);
54 }
55
56 printf("\n server listing on address %s",inet_ntoa(tempaddr.sin_
addr));
57 printf("\n and on port %u\n",ntohs(tempaddr.sin_port));
58 if( listen(s,4)== -1) //error
59 {
60 perror("\n error in listen : ");
61 exit(-1);
62 }
63
64 printf("\n after listen ");
65
66 strcpy(buf,"hello");

67 buf[5]='\0';
68
69 while(1)
70 {
71 c=accept(s,(struct sockaddr*)&clientaddr,&lenstruct);
72 if(c==-1) //error
73 {
74 perror("\n error in accept : ");
75 exit(-1);
76 }
77
78 if( getpeername(c,(struct sockaddr*)&tempaddr,&lenstruct
) == -1) // error
79 {
80 perror("\n error in getpeername : ");
81 exit(-1);
82 }
83
84 printf("\n\n new client connected at address %s and at port %d",inet_nto
a(tempaddr.sin_addr),ntohs(tempaddr.sin_port));
85
86 if( send(c,buf,5,0) == -1) //error
87 {

88 perror("\n error in sending hello : ");
89 exit(-1);
90 }
91
92 printf("\n hello send\n\n");
93 close(c);
94 }
95 close(s);
96 }


that is it,
i am sure of the address,

so what could be the prob?


dev

RobSeace
04-09-2003, 07:13 PM
Well, I don't see anything wrong that should cause the bind() to fail...
You say it's failing with EADDRNOTAVAIL ("Cannot assign requested
address")? That should mean that the IP is not a recognized local IP
on the current machine... You really see that IP listed for one of the
local interfaces in "ifconfig" output?? And, the interface is configured
up? If so, I don't know... *shrug*

dev
04-10-2003, 08:34 AM
hi Rob,

yea, i am sorry, the address was wrong, (the MF&SOB administrator gave me a wrong one). sorry for the mess.

thanking u

dev