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));