I need to write a simple telnet app in C language using TCP sockets. What i've done is a client & server, which is working with commands like 'ls' or 'pwd', but i need ability to use different shell programs like bash, sh, csh. Can someone tell me which functions should i use or how to rebuild my server? Because now when i use "sh" command, server and client feezes.
Actually i've got this function inside client:
void chat(int sockfd){
char buff[MAX];
char text[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("->: ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Exiting...\n");
break;
}
bzero(buff, sizeof(buff));
read(sockfd, tex, sizeof(tex));
printf("Server message ->: %s", tex);
bzero(tex, MAX);
}
}
And this one inside server:
void chat(int sockfd){
char buff[MAX];
int n,p[1];
FILE *fp;
char path[1024];
for(;;){
bzero(buff, MAX);
read(sockfd, buff, sizeof(buff));
fp = popen(buff, "r");
if(fp == NULL){
strncpy(buff, "Failed to run command", MAX);
write(sockfd, buff, sizeof(buff));
continue;
}
printf("%s\n", buff);
while(fgets(path, sizeof(path), fp) != NULL){
write(sockfd, path, sizeof(path));
}
pclose(fp);
}
}
Thanks for attention.