Solved

C++ Language question

Posted on 1997-09-01
2
326 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 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