C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 09-11-2009, 10:10 PM   #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));
tesla is offline   Reply With Quote
Old 09-11-2009, 10:49 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 09-11-2009, 10:56 PM   #3
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:11 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22