Fedora Linux Support Community & Resources Center
  #1  
Old 1st November 2011, 06:05 PM
giobaxx Offline
Registered User
 
Join Date: Nov 2011
Posts: 7
windows_98_nt_2000ie
network programming and error on socket bind

I'm studying socket programming, and i was trying to do some very simple example
like this
Code:
# include <strings.h>
# include <stdlib.h>
# include <netinet/in.h>
# include <stdio.h>
# include <sys/types.h>
# include <sys/socket.h>

int main(int argc, char *argv[]){
    struct sockaddr_in myself, peer;
    int peer_len = sizeof(peer);
    int b_len;

    int s, i;
    char buf[1500];
    if (argc < 2) {
	fprintf (stderr, "Usage: %s port_number\n", argv[0]);
	exit(1);
    }
    if ((s=socket(AF_INET, SOCK_DGRAM, 0))==-1) {
      perror("socket");
      exit (1);
    }

    bzero((char *) &myself, sizeof(myself));
    myself.sin_family=AF_INET;
    //myself.sin_addr.s_addr=INADDR_ANY;
	myself.sin_addr.s_addr=inet_addr("10.10.10.10");
    myself.sin_port= htons(atoi(argv[1]));

    if (bind(s, (struct sockaddr *) &myself, sizeof(myself))==-1) {
       perror("bind");
       exit(1);
    }

    while(1){
       if (recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &peer, &peer_len)==-1){
           perror("recvfrom()");
           exit(1);
       }
       printf("Received packet from %s:%d\nData: %s\n\n",
       inet_ntoa(peer.sin_addr), ntohs(peer.sin_port), buf);
    }
}
When i try to bind a socket with an IP address different by the family 127.0.x.x for example 10.10.10.10 i receive an error "BIND Cannot assign requested address". what i can do?.....tanks
Reply With Quote
  #2  
Old 2nd November 2011, 12:43 PM
markkuk Offline
Registered User
 
Join Date: Apr 2005
Location: Finland
Posts: 5,075
linuxfirefox
Re: network programming and error on socket bind

Use a correct IP address for your machine and the interface you want to use, or INADDR_ANY to bind to all available addresses.
Reply With Quote
Reply

Tags
bind, error, network, programming, socket

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to get link local address of ipv6 using socket programming in c deepa Programming & Packaging 1 11th September 2010 08:51 PM
socket programming in kbasic ?? gowrishankar Using Fedora 0 24th January 2009 05:25 PM
C socket programming Help! qinglau Programming & Packaging 5 3rd May 2006 03:27 PM
socket: bind() error in a udp aplication Owy Programming & Packaging 6 29th March 2006 04:44 PM


Current GMT-time: 04:55 (Saturday, 01-11-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Varazdin Photos - Barcelona Photos on Instagram - Al Hamul