LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2007, 04:58 AM   #1
leamassiot
Member
 
Registered: Dec 2005
Location: France
Distribution: Debian
Posts: 123

Rep: Reputation: 17
Question Connect to a socket


I want to connect to a server socket identified by :
- its IP address : X.Y.Z.W (decimal dotted notation)
- and its TCP port : n

I cannot use the usual:
gethostbyname();
inet_ntoa();

sequence of instructions
because I do not assume I know hosts
according to their host names
but according to their IP addresses.

I need to provide the connect() system call with the proper arguments.
int connect(int socket, const struct sockaddr * address, socklen_t address_len);

Let us say I declare :
struct sockaddr_in in;
I do not know what to put in :
in.in_addr.s_addr (which is an unsigned int representing the IP address in network byte order).

Please help me.
 
Old 03-09-2007, 08:36 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
First, the normal sequence (where a hostname such as "www.google.com" is given) is not

Code:
gethostbyname()
inet_ntoa()
but simply

Code:
gethostbyname()
because what is returned from gethostbyname() is the address (or more than one address) already in the four-byte network-ordered format.

Second, inet_ntoa() converts not from decimal-dotted format to four-byte network-ordered format, but the other way around. "n" stands for "network", and "a" stands for "ASCII". inet_ntoa() goes from network to ASCII. What you want instead is inet_aton().

Third, instead of using inet_aton() and inet_ntoa(), I strongly recommend you use inet_pton() and inet_ntop(). (The "p" stands for "presentation" format.) There are two reasons to do this.
  1. If you ever go back from 4-byte network addresses to printable dotted-decimal form, inet_ntop() is re-entrant. It stores the data in memory that you allocate, not in a static buffer. This will make your program easier to maintain if it gets much larger than you originally envisioned, which is something that happens often.
  2. You'll be that much closer to the day when you want to convert your program to be IPV6-aware.
The following code demonstrates it all:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int main(int    argc,
         char **argv
        )
{
  char   ntop_buffer[INET_ADDRSTRLEN];

  struct sockaddr_in inxyz;

  if(argc!=2)
  {
    fprintf(stderr,"we need one argument: a dotted decimal IP address\n");

    exit(1);
  }

  if(inet_aton(argv[1],&inxyz.sin_addr)==0)
  {
    fprintf(stderr,"invalid address\n");

    exit(1);
  }

  printf("Network-ordered address from inet_aton(), printed as a CPU-ordered integer:\n"
         "0x%08X\n",
         inxyz.sin_addr.s_addr
        );

  printf("Dotted decimal representation from inet_ntoa():\n%s\n",
         inet_ntoa(inxyz.sin_addr)
        );

  if(inet_pton(AF_INET,argv[1],&inxyz.sin_addr)==0)
  {
    fprintf(stderr,"invalid address\n");

    exit(1);
  }

  printf("Network-ordered address from inet_pton(), printed as a CPU-ordered integer:\n"
         "0x%08X\n",
         inxyz.sin_addr.s_addr
        );

  printf("Dotted decimal representation from inet_ntop():\n%s\n",
         inet_ntop(AF_INET,&inxyz.sin_addr,ntop_buffer,INET_ADDRSTRLEN)
        );

  return 0;

} /* main() */
Another point: You can actually call gethostbyname() instead if you want. It returns four-byte network-ordered addresses, and you can give it either "www.google.com" or "66.102.7.147". It will work either way.

Finally, you'll notice in the source code that I used variable inxyz, not in as you had in your example. When choosing the name of a variable, it's always better to choose something that's not a substring of some other identifier. Why? Because if you use your text editor to search for all instances of variable in, for example, you'll also find many other uninteresting occurrences of "in".
 
Old 03-09-2007, 08:40 AM   #3
leamassiot
Member
 
Registered: Dec 2005
Location: France
Distribution: Debian
Posts: 123

Original Poster
Rep: Reputation: 17
Thanks a lot to you ,
it is exactly what I was searching for and it works

Thanks for the code and the comments.

Good day in Mariposa.

Last edited by leamassiot; 03-09-2007 at 08:45 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to connect to UNIX socket /tmp/.esd/socket error while using grip dr_zayus69 Linux - Software 4 08-23-2005 07:28 PM
Socket error on Connect mdimanna Programming 2 06-24-2005 10:28 AM
cannot connect to a socket from other computer duongvn Linux - Networking 4 04-14-2005 11:11 PM
MySQL can't connect through socket! Avatar Linux - Software 15 08-10-2004 01:25 PM
connect: socket operation on non-socket bit7 Linux - Networking 0 02-18-2003 05:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 06:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration