Thread: Setting IP address to connect to

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    11

    Setting IP address to connect to

    Hey, I'm pretty new to this networking stuff in C, and i just got a question abt some example code that i found. This is only part of the Client.c file.

    The question is: I want the client to connect to the IP Address which i have to
    explicitly set as 'localhost'. But I don't know exactly how I set the IPAddress that the client is to connect to, could someone please tell me where I do that and how?

    Also,

    Code:
    socketAddr.sin_addr.s_addr = ipAddress->s_addr;	/* Copies IP address */
    The comment on this code says, "copies IP address" why do you have to do that?

    Thanx heaps,





    Code:
    int fd;
    	struct in_addr* ipAddress;
    	struct hostent* hostp;
    	struct sockaddr_in socketAddr;
    	char buffer[1024];
    	int numBytes;
    
    	hostp = gethostbyname("localhost");
    	ipAddress = (struct in_addr*)hostp->h_addr_list[0];
    
    	/* Specify the address we're going to connect to */
    	socketAddr.sin_family = AF_INET;	/* Internet socket */
    	socketAddr.sin_port = htons(13827);	/* Port number */
    	socketAddr.sin_addr.s_addr = ipAddress->s_addr;	/* Copies IP address */
    
    	/* Create TCP socket */
    	fd = socket(AF_INET, SOCK_STREAM, 0);
    
    	/* Try to connect to our server address */
    	/* Address cast to generic socket address type, but we need to specify
    	 ** the size of the address as the third argument */
    	connect(fd, (struct sockaddr*)&socketAddr, sizeof(socketAddr));

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by tesla View Post
    Hey, I'm pretty new to this networking stuff in C, and i just got a question abt some example code that i found. This is only part of the Client.c file.

    The question is: I want the client to connect to the IP Address which i have to
    explicitly set as 'localhost'. But I don't know exactly how I set the IPAddress that the client is to connect to, could someone please tell me where I do that and how?

    Also,

    Code:
    socketAddr.sin_addr.s_addr = ipAddress->s_addr;	/* Copies IP address */
    The comment on this code says, "copies IP address" why do you have to do that?

    Thanx heaps,





    Code:
    int fd;
    	struct in_addr* ipAddress;
    	struct hostent* hostp;
    	struct sockaddr_in socketAddr;
    	char buffer[1024];
    	int numBytes;
    
    	hostp = gethostbyname("localhost");
    	ipAddress = (struct in_addr*)hostp->h_addr_list[0];
    
    	/* Specify the address we're going to connect to */
    	socketAddr.sin_family = AF_INET;	/* Internet socket */
    	socketAddr.sin_port = htons(13827);	/* Port number */
    	socketAddr.sin_addr.s_addr = ipAddress->s_addr;	/* Copies IP address */
    
    	/* Create TCP socket */
    	fd = socket(AF_INET, SOCK_STREAM, 0);
    
    	/* Try to connect to our server address */
    	/* Address cast to generic socket address type, but we need to specify
    	 ** the size of the address as the third argument */
    	connect(fd, (struct sockaddr*)&socketAddr, sizeof(socketAddr));
    Your second question is the answer to the first, and vice versa. You want to connect to localhost, so you need the IP address, so you go and get it (that's what gethostbyname does, get the host address from its name). Then you have to put into your socket object thing that you want to connect with.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So replace localhost with the name of the site you do want to connect to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. IP address configuration Files?
    By Matus in forum Tech Board
    Replies: 3
    Last Post: 01-29-2009, 04:35 PM
  3. IP Address Control Text Freeing?
    By MitchellH in forum Windows Programming
    Replies: 2
    Last Post: 05-22-2005, 11:49 PM
  4. finding out ip address?
    By chiatello in forum Linux Programming
    Replies: 3
    Last Post: 08-07-2003, 06:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM