4 Replies - 456 Views - Last Post: 07 January 2011 - 06:56 AM Rate Topic: -----

#1 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

bind can't be used as a function

Posted 06 January 2011 - 06:38 PM

hello all i am trying to learn network programming and having some difficulties.

i am at the point where i have gathered the information from my computer and i need to bind the socket to the ip address but i receive an error stating that bind can't be used as a function. i've tried using it in an if statement and still the same result. idk if i've included the right header file or what but the tutorial i'm using looks like i'm using everything that i need. can anyone help me.

thanks,
x

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

using namespace std;

int main()
{
	struct addrinfo hints, *res, *p;
	int status, skt, bind;
	char ipstr[INET6_ADDRSTRLEN];
	string ipaddress;

	cout<<"Enter Website: ";
	cin>>ipaddress;
	

/*if (argc!=2)
{
fprintf(stderr, "usage: showip hostname\n");
return 1;
}*/

	memset(&hints, 0, sizeof hints);
	hints.ai_family=AF_UNSPEC; //AF_INET or AF_INET6 to force version
	hints.ai_socktype=SOCK_STREAM;
	hints.ai_flags=AI_PASSIVE;

	getaddrinfo(ipaddress.c_str(),"http", &hints, &res);
/*
	if((status=getaddrinfo(NULL, "3490", &hints, &res))!=0)
	{
		cout<<stderr<<" getaddrinfo: "<<gai_strerror(status)<<"\n";
		return 2;
	}
*/	
	skt=socket(res->ai_family, res->ai_socktype, res->ai_protocol);

	if(bind(skt, res->ai_family, res->ai_protocol)<0)
		return 3;
	
	cout<<"IP addresses of "<<ipaddress<<"\n\n";
	
	for(p=res; p!=NULL; p=p->ai_next)
	{
		void *addr;
		char *ipver;

		//get the pointer to the address itself,
		//different fields in IPv4 and IPv6:

		if(p->ai_family==AF_INET)//IPv4
		{
			struct sockaddr_in *ipv4=(struct sockaddr_in *)p->ai_addr;
			addr=&(ipv4->sin_addr);
			ipver="IPV4";
		}

		else//IPv6
		{
			struct sockaddr_in6 *ipv6=(struct sockaddr_in6 *)p->ai_addr;
			addr=&(ipv6->sin6_addr);
			ipver="IPv6";
		}

		//convert the IP to a string and print it:
		inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
		//printf(" %s: %s\n", ipver, ipstr);
		cout<<ipver<<" "<<ipstr<<"\n";
	}

	freeaddrinfo(res);//free teh linked list

	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: bind can't be used as a function

#2 Draps  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 103
  • Joined: 20-November 10

Re: bind can't be used as a function

Posted 06 January 2011 - 06:47 PM

please don't double post,


I myself have not done network programming in C++, However it might be worth while having a read through this small tutorial / ebook.
http://beej.us/guide...page/bgnet.html


You seam to have the basic configuring a ip address so skip down to about 3.0
This also covers what everything is and does, like what a socket is etc.

Hope this helps

Edit: P.S 5.3 goes over the Bind() function just in case you thought i led you to junk.

This post has been edited by Draps: 06 January 2011 - 06:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: bind can't be used as a function

Posted 06 January 2011 - 06:52 PM

that is the exact tutorial i'm using. I've done some modifications because, well it makes it easier when i run it. i get the same error on my mac as i do my linux. Var names shouldn't matter i don't think :/
Was This Post Helpful? 0
  • +
  • -

#4 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: bind can't be used as a function

Posted 06 January 2011 - 07:12 PM

int main()
{
	struct addrinfo hints, *res, *p;
	int status, skt, bind;



you declaration of an int named bind in main() overrides the function bind.
Was This Post Helpful? 3
  • +
  • -

#5 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: bind can't be used as a function

Posted 07 January 2011 - 06:56 AM

ooooooohhhhhhhhh...DO'H :stupid: :blink: :pinch: :oops:
THANKS i'm a bit blonde
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1