C++ Language question

Posted on 1997-09-01
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] = "";
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
Rc = bind(SockFd,(struct sockaddr_in *)&in ,sizeof(sockaddr_in));
cout << "Rc is : " << Rc << "\n";
cout << "Error is : " << strerror(errno) << "\n";


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.
Question by:hanan

Accepted Solution

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

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


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

Hope this helps
gaohong xie

Expert Comment

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 ( 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.

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

705 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

14 Experts available now in Live!

Get 1:1 Help Now