Solved

C++ Language question

Posted on 1997-09-01
2
327 Views
Last Modified: 2008-03-10
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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
gaohong earned 100 total points
ID: 1168524
Chaneg this line:

Rc = bind(SockFd,(struct sockaddr_in *)&in ,sizeof(sockaddr_in));

to

Rc = bind(SockFd,(struct sockaddr *)&in ,sizeof(sockaddr_in));

Hope this helps
gaohong xie
 
0
 
LVL 3

Expert Comment

by:msmits
ID: 1168525
The proposed answer helps in getting things through the compiler for me, but it still doesn't work. I don't have IRIX but Sun Solaris, but am having the same problem with your program.

I believe that the problem is with the address you are giving with the bind call. In my own code I am using INADDR_ANY, which is actually just 0. When I try your code with my own IP address, it seems to work. My conclusion is thus that you really need to specify the exact IP address of your own machine or that of localhost (127.0.0.1) or just 0.
In a server program (you are us bind) you get the address of the other side from the accept call. The bind is just to supply some information on your own side of the socket.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

717 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