Hi,
It is possible to get the IP address at the run-time. IP can be entered at run-time in dotted decimal format as a string, like 1.2.3.4. You can then use API available such as inet_addr() or inet_aton() to convert it from the string format to the format required in structure sockaddr_in which unsigned long.
A short sample would be
Code:
char line[20];
unsigned long ip;
unsigned short port;
struct sockaddr_in remotsock;
int sockfd;
int ret;
/* Initialize Winsock here */
printf("Enter the Remote IP address the connect: \n");
fgets (line,sizeof(line),stdin);
line[strlen(line) - 1] = '\0';
ip = inet_addr(line); /* Convert IP from string format to unsigned long */
printf("Enter the Port : \n");
fgets (line,sizeof(line),stdin);
line[strlen(line) - 1] = '\0';
sscanf(line, "%d", &port);
remotesock.sin_family = AF_INET;
remotesock.sin_addr.s_addr = ip;
remotesock.sin_port = port;
memset(&(remotesock.sin_zero),'\0',8);
sockfd = socket(AF_INET,SOCK_STREAM,0);
ret = connect(sockfd,(struct sockaddr *)&remotesock, sizeof(sockaddr_in));
if (ret < 0)
{
printf("Error in connect\n");
exit(1);
}
/* Do send/recv() here after this */
Thanks,