Solved

error binding try to communicate on IRIX

Posted on 1997-09-01
1
255 Views
Last Modified: 2013-12-23
You ask...
              What am I doing wrong ? My code is :

              include <fcntl.h>
              #include <unistd.h>
              #include <stdio.h>
              #include <stdlib.h>
              #include <sys/types.h>
              #include <stdio.h>
              #include <stdlib.h>
              #include <sys/types.h>
              #include <sys/uio.h>
              #include <bstring.h>
              #include <sys/time.h>
              #include <netinet/in.h>
              #include <sys/socket.h>
              #include <string.h>
              #include <iostream.h>
              #include <errno.h>
              extern "C" {
              extern unsigned int inet_addr(char *string);
              }

              void main()
              {
              int SockFd,Rc,NewFd,len,lis;
              int Port = 9989;
              char addr[20] = "192.114.248.101";
              struct sockaddr_in in;

              //init sockaddr_in fields
              in.sin_family = AF_INET;
              cout << "in.sin_family = " << in.sin_family << "\n";
              in.sin_addr.s_addr = inet_addr(addr);
              cout << "in.sin_addr.s_addr = " << in.sin_addr.s_addr <<
              "\n";
              in.sin_port = htons(Port);
              cout << "in.sin_port = " << in.sin_port << "\n";

              // create socket
              SockFd = socket(AF_INET,SOCK_STREAM,0);
              cout << "SockFd is : " << SockFd << "\n";

              // make the binding
              //fcntl(SockFd,F_SETFL,FNDELAY);
              //unlink(sin.sa_data);
              Rc = bind(SockFd,(struct sockaddr_in *)&in
              ,sizeof(sockaddr_in));
              cout << "Rc is : " << Rc << "\n";
              cout << "Error is : " << strerror(errno) << "\n";

              close(SockFd);
              }



              The result I get is :

              n.sin_family = 2
              in.sin_addr.s_addr = 3228760165
              in.sin_port = 9989
              SockFd is : 3
              Rc is : -1
              Error is : Cannot assign requested address


              what should i do.
0
Comment
Question by:hanan
1 Comment
 

Accepted Solution

by:
bkc earned 100 total points
ID: 1582392
This will only work if you're running this program on hds1.mta.ac.il [192.114.248.101]

Unless your system is multi-homed, its best to specify INADDR_ANY
as the sin_addr.s_addr, allowing your OS to assign the local
endpoint IP Address. INADDR_ANY is usually = 0.

If you are multi-homed, you must be certain that the address you
are requesting is assigned to a valid and active interface on your system.

Also, you may need to ntohl() the s_addr value. I don't think you
do, but you can try that if you're certain the IP address you're
trying to use is valid for your system.

Note also that if this is the server-side of a future connection,
and you're planning to call getsockname() to find out what
IP address has been assigned (if you're multi-homed), you really
can't call getsockname until a socket has been connected, and
you must call getsockname on that newly accepted socket, not
the listening socket.

Summary: Use INADDR_ANY as the s_addr value, not a specific value.

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

If your business is like most, chances are you still need to maintain a fax infrastructure for your staff. It’s hard to believe that a communication technology that was thriving in the mid-80s could still be an essential part of your team’s modern I…
Data center, now-a-days, is referred as the home of all the advanced technologies. In-fact, most of the businesses are now establishing their entire organizational structure around the IT capabilities.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now