Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

C sockets, error 99 - Cannot assign requested address

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
6 posts • Page 1 of 1
Author
Message
win_dir
n00b
n00b
Posts: 19
Joined: Fri Aug 22, 2003 8:30 am
Location: UK
Contact:
Contact win_dir
Website

C sockets, error 99 - Cannot assign requested address

  • Quote

Post by win_dir » Sat Nov 01, 2003 4:49 pm

Hi all,

I'm currently learning C programming, especially sockets...so don't flame me or anything for the lack of error handling below. Could somebody tell me why this code gives me error 99 when using the bind() function. Error 99 is: Cannot assign requested address. Thanks in advance, can't wait for this to work so i can get further.

Code: Select all

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>

int main(int argc, char *argv[])
{  //creates integers for use
   int sockid, clilen, newsockid,n,z;
   char buffer[256];

   //creates sockaddr_in structures for server and client addresses, ports etc.
   struct sockaddr_in  serv_addr, cli_addr;

   bzero((char *) &serv_addr, sizeof(serv_addr));
   
   /*Creates new socket, AF_INET specifies IPv4, SOCK_STREAM specifies
   the type of communication other choice is SOCK_DGRAM. The 0 tells the system to
   choose the best protocol depending on the communication type chosen, this can also
   be set to something like IPPROTO_TCP */
   sockid = socket(  AF_INET,SOCK_STREAM,0);

   /*initializes server port variable, htons converts 2034 to network byte order
   as can be done with the server IP address*/
   serv_addr.sin_port = htons("11000");

   /*Sets address for socket to bind to (0.0.0.0 probably)*/
   serv_addr.sin_addr.s_addr  = INADDR_ANY;

   /*Tells system to use the IPv4 address family*/
   serv_addr.sin_family = AF_INET;

   /*Binds socket to the address and port specified earlier*/
   n = bind(sockid, (struct sockaddr *) &serv_addr, sizeof(serv_addr));

   /*Listen on created and binded socket, allow for a 5 connection queue*/
   z = listen(sockid, 5);

   /*Gets the length of the address*/
   clilen = sizeof(cli_addr);

     if ( n < 0){
    printf("ERROR NO: %d\n",errno);
    }

   /*accept connection request creating new socket for that connection
   unless specifically set otherwise the accept() function will stop all commands
   until a connection is requested*/
   newsockid = accept(sockid, (struct sockaddr *) &cli_addr, &clilen);
                   
   //Set the 256byte buffer to zero
   bzero(buffer,256);

   /*read() will stop all commands until it is sent something to read,
   the it will put any status numbers is n, the newsockid from the
   accept() function is used, and all input is put into the buffer.
   only 255 or less bytes can be put into the read buffer this way.*/
      while (0==0){
      n = read(newsockid,buffer,255);
                       n = write(newsockid,"hello there",11);
      }
   return(0);
   }
   
Gentoo Linux ROOOOCKS!
http://www.gentoo.org
Top
pygoscelis
Guru
Guru
Posts: 421
Joined: Sat Jun 07, 2003 8:20 pm

  • Quote

Post by pygoscelis » Sat Nov 01, 2003 5:12 pm

Hm. Doesn't htons() accept a 16-bit integer?
Top
win_dir
n00b
n00b
Posts: 19
Joined: Fri Aug 22, 2003 8:30 am
Location: UK
Contact:
Contact win_dir
Website

  • Quote

Post by win_dir » Sat Nov 01, 2003 10:11 pm

I assume it must do, as they used it for the server example here: http://www.cs.rpi.edu/courses/sysprog/sockets/sock.html . That link may help you help to diagnose my problem, by seeing what i have done differently anyway. Thanks for you help.

Kind Regards
Mark Rawson
Gentoo Linux ROOOOCKS!
http://www.gentoo.org
Top
Jesse
Tux's lil' helper
Tux's lil' helper
Posts: 148
Joined: Wed Apr 24, 2002 2:05 am

  • Quote

Post by Jesse » Sat Nov 01, 2003 10:40 pm

right and he means do this:

Code: Select all

htons (11000);
instead of:

Code: Select all

htons("11000");
Here's working code from a server that I've done in the past

Code: Select all

    int listener;

    unsigned int size;

    struct sockaddr_in  serv_addr;

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons (35102);
    serv_addr.sin_addr.s_addr = INADDR_ANY;

    listener = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);

    bind (listener, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr));
    listen (listener, 10);
    // ....
    accept (... );
Top
pygoscelis
Guru
Guru
Posts: 421
Joined: Sat Jun 07, 2003 8:20 pm

  • Quote

Post by pygoscelis » Sat Nov 01, 2003 10:55 pm

Well, if you replace "11000" (string) with 2034 (integer), ad add couple of includes (unistd.h for read/write and strings.h for bzero) the program sorta works. That is, you can run it, telnet to port 2034, type something, and it will print "hello there". The problem is, it only accepts one client, and when that client breaks the connection, the program quits with "broken pipe". You need to do a bit more reading :)
Top
win_dir
n00b
n00b
Posts: 19
Joined: Fri Aug 22, 2003 8:30 am
Location: UK
Contact:
Contact win_dir
Website

  • Quote

Post by win_dir » Sat Nov 01, 2003 11:39 pm

Woohooo, you are a godsend! Yay, thanks it works. Thanks alot Jesse. Regarding pygoscelis' comment, i haven't yet completed the tutorial, in fact i have only just started and have only been learning C for a few hours, so don't eat me lol, but thanks for informing me that they show how to do multi-client servers at the end of the tutorial.

P.S. for anyone who wants to know, here is how to specify an IP address to bind to instead of using the following which binds to all IP addreses:

Code: Select all

serv_addr.sin_addr.s_addr = INADDR_ANY
P.S. The code below is what i figured out from the manpage + a bit of C knowledge learnt (about the pointer), if you would like to know which headers you need to include just type "man inet_pton" and it'll give you a list, as most of us already know.

Code: Select all

inet_pton(AF_INET,"192.168.0.1",&serv_addr.sin_addr.s_addr);
Gentoo Linux ROOOOCKS!
http://www.gentoo.org
Top
Post Reply

6 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic