Send and receive while there is data to send via sockets

I have a client-server application where a server listens on one machine on a particular port, and a client can connect to that machine via it’s IP address and port.

The client sends data to the server with the command it wants to execute, for instance, cat /etc/passwd. That server uses popen to execute the command and store the results in a buffer, like so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>/* Receive commands from client */
char command_in[256];
char command_out[2048];
char command_out_send[2048];
ssize_t recv_data = recv(client_s, command_in, sizeof(command_in), 0);
// <SNIP>
/* Execute system command and obtain output */
FILE *fp = popen(command_in, "r");
if (fp == NULL) {
close(client_s);
close(s);
return 1;
} else {
while(fgets(command_out, sizeof(command_out) - 1, fp) != NULL) {
/* Capture all output and save it to buffer */
strcat(command_out_send, command_out);
}
}
/* Send executed command to client*/
if (send(client_s, command_out_send, sizeof(command_out_send), 0) == -1) {
close(client_s);
close(s);
return 1;
}
/* Cleanup */
memset(command_out_send, 0, sizeof(char));
pclose(fp);
</code>
<code>/* Receive commands from client */ char command_in[256]; char command_out[2048]; char command_out_send[2048]; ssize_t recv_data = recv(client_s, command_in, sizeof(command_in), 0); // <SNIP> /* Execute system command and obtain output */ FILE *fp = popen(command_in, "r"); if (fp == NULL) { close(client_s); close(s); return 1; } else { while(fgets(command_out, sizeof(command_out) - 1, fp) != NULL) { /* Capture all output and save it to buffer */ strcat(command_out_send, command_out); } } /* Send executed command to client*/ if (send(client_s, command_out_send, sizeof(command_out_send), 0) == -1) { close(client_s); close(s); return 1; } /* Cleanup */ memset(command_out_send, 0, sizeof(char)); pclose(fp); </code>
/* Receive commands from client */
char command_in[256];
char command_out[2048];
char command_out_send[2048];
ssize_t recv_data = recv(client_s, command_in, sizeof(command_in), 0);

// <SNIP>

/* Execute system command and obtain output */
FILE *fp = popen(command_in, "r");
if (fp == NULL) {
    close(client_s);
    close(s);
    return 1;
} else {
    while(fgets(command_out, sizeof(command_out) - 1, fp) != NULL) {
        /* Capture all output and save it to buffer */
        strcat(command_out_send, command_out);
    }
}

/* Send executed command to client*/
if (send(client_s, command_out_send, sizeof(command_out_send), 0) == -1) {
    close(client_s);
    close(s);
    return 1;
}

/* Cleanup */
memset(command_out_send, 0, sizeof(char));
pclose(fp);

The issue is the fact that it creates a buffer to hold the contents.

The client is as follows.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>while (1) {
char command_in[256];
char command_out[2048];
printf("~ ");
fgets(command_in, 256, stdin);
/* Send command to server */
if (send(s, command_in, sizeof(command_in), 0) == -1) {
close(s);
return 1;
}
/* Receive response from server */
ssize_t recv_data = recv(s, command_out, sizeof(command_out), 0);
if (recv_data == -1) {
close(s);
return 1;
}
/* Print out command results */
fputs(command_out, stdout);
}
</code>
<code>while (1) { char command_in[256]; char command_out[2048]; printf("~ "); fgets(command_in, 256, stdin); /* Send command to server */ if (send(s, command_in, sizeof(command_in), 0) == -1) { close(s); return 1; } /* Receive response from server */ ssize_t recv_data = recv(s, command_out, sizeof(command_out), 0); if (recv_data == -1) { close(s); return 1; } /* Print out command results */ fputs(command_out, stdout); } </code>
while (1) {
    char command_in[256];
    char command_out[2048];
    printf("~ ");
    fgets(command_in, 256, stdin);

    /* Send command to server */
    if (send(s, command_in, sizeof(command_in), 0) == -1) {
        close(s);
        return 1;
    }

    /* Receive response from server */
    ssize_t recv_data = recv(s, command_out, sizeof(command_out), 0);
    if (recv_data == -1) {
        close(s);
        return 1;
    }

    /* Print out command results */
    fputs(command_out, stdout);
}

Currently, the client retrieves the data and prints the output. Instead, I want to send in chunks while there is data to send on the server side, and for that client to wait until all that data is sent rather than creating a buffer to hold the entire contents of the command..

How can I do this?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật