Hỏi về socket và service chạy trong Ubuntu

Hi mọi người e có tạo 2 file service và socket dùng để chạy file C mà e đã compile trước đó cho ra là file Question1 và e muốn chạy nó trên socket và cả service e dùng ncat để test , mặc dù trên status báo socket đã chạy rồi nhưng lúc dùng ncat để test thì nó đơ và không trả về kết quả gì hết :frowning:
Đây là service file của e
https://imgur.com/a/wShyvJd
đây là socket file
https://imgur.com/a/U8vqRKV
Kết quả khi check status và dùng ncat test với socket 3343


Code file C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<readline/readline.h>
#include<readline/history.h>
#include <ctype.h>

typedef struct {
    char name[50];
    char description[50];
}LOOK_UP;


char* get_function_name(LOOK_UP * look_up,char* name,int * func_count);

int isStringAlpha( const char s[] );

int main()
{
    const int BUFSIZE = 512;

    LOOK_UP my_lookup[100];

    FILE *pipe = popen("apropos -s2 socket","r");

    char line[100];


    int i= 0;
    int ch;


    using_history ();
    while(fgets(line,BUFSIZE,pipe)){

        char **token = history_tokenize(line);

        //The first token is the name
        strcpy(my_lookup[i].name,*(token));

        char description[100];

        //reset the description
        memset(description, 0, 100);

        int y = 1;
        while(*(token+y) != NULL){

            char *ln = *(token + y);

            //only take the alphabectal values
            if(isStringAlpha(ln)){
                strcat(description,ln);
                strcat(description," ");
            }

            y++;
        }

        strcpy(my_lookup[i].description,description);

        i++;
    }

    pclose(pipe);

    printf("Socket description look up, enter name:\n");

    char user_input[50];
    gets(user_input);

    while(1){
        char* desc = get_function_name(my_lookup,user_input,i);
        printf("Description: %s\n",desc);
        printf("Socket description look up, enter name:\n");
        gets(user_input);
    }

    return 0;
}


//Check the description of the function from the name passed
char* get_function_name(LOOK_UP * look_up,char* name,int * func_count){

    char *desc;
    int found = -1;

    for(int i = 0; i < func_count; i++){

        LOOK_UP temp = *(look_up + i);

        if(strcmp(temp.name,name) == 0){
            desc = &temp.description;
            found = 0;
            break;
        }

    }

    if(found == -1){
        desc = "No description for the entry.\n";
    }

    return desc;

}


int isStringAlpha( const char s[] )
{
    unsigned char c;

    while ( ( c = *s ) && ( isalpha( c ) || isblank( c ) ) ){
        ++s;
    }

    return *s == '\0';
}

đây là sau khi e chạy thử với ncat nhưng không trả về kết quả ạ

Bạn dùng ncat để làm gì, bạn mong đợi kết quả như thế nào?

1 Like

e tính dùng ncat để test file socket ạ không biết có đúng không, ncat để chạy file C ấy ạ

Cho mình hỏi dòng này để làm gì?

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?