0

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.

colomaxy
  • 9
  • 1
  • Note that `bzero()` is deprecated, use [`memset()`](http://man7.org/linux/man-pages/man3/memset.3.html) – Marco Bonelli Feb 04 '20 at 14:28
  • This involves setting up a controlling terminal. – dbush Feb 04 '20 at 14:37
  • @dbush can you tell me more about this? which functions should i use? – colomaxy Feb 04 '20 at 14:50
  • @colomaxy It's a fairly large topic, one I never fully got into. Google "allocating a controlling terminal" or something similar to get you started. – dbush Feb 04 '20 at 14:54
  • _You generally do not need to worry about the exact mechanism used to allocate a controlling terminal to a session, since it is done for you by the system when you log in._ [From here](https://www.gnu.org/software/libc/manual/html_node/Controlling-Terminal.html) – ryyker Feb 04 '20 at 14:57
  • You don't need a controlling terminal (as far as I know) but you do need to process input and output at the same time. – user253751 Feb 04 '20 at 16:04

0 Answers0