but it doesnt work, the server is stuck on read after it sends the command and client receives it but doesnt send anything back, the command is executed too
i tried sending something else from the client like "Hi" and it works but doesnt work when i want to send a commands result
Server Code:
Code: Select all
#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>
//Sizes
#define BUFFER_MAX 1024
//#define HIGH_BUFFER 11056
//Server DATA
#define server_ip "192.168.1.107"
#define port 7054
int main() {
int sockfd, newsockfd;
int option = 1;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
perror("Failed Creating Socket: ");
return EXIT_FAILURE;
}
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0) {
perror("Failed Setting Socket Options: ");
return EXIT_FAILURE;
}
struct sockaddr_in serv_addr, cli_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
socklen_t clilen;
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("Couldnt Bind IP And PORT: ");
return EXIT_FAILURE;
}
if(listen(sockfd, 5) < 0) {
perror("Couldn't Listen For Incoming Connections: ");
return EXIT_FAILURE;
}
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr ,&clilen );
if(newsockfd < 0) {
perror("Couldn't accept connection from client: ");
return EXIT_FAILURE;
}
printf("Connection Accpeted (%s)\n\n", inet_ntoa(cli_addr.sin_addr));
while(1) {
jump:
char buffer[BUFFER_MAX] = {0};
char full_response[BUFFER_MAX] = {0};
printf("SecureShell@%s$ ", inet_ntoa(cli_addr.sin_addr));
fgets(buffer, sizeof(buffer), stdin);
send(newsockfd, buffer, strlen(buffer), 0);
if(strncmp("disconnect", buffer, 10) == 0) {
close(sockfd);
close(newsockfd);
printf("\nDisconnecting.");
break;
} else {
recv(newsockfd, full_response, sizeof(full_response), MSG_WAITALL);
printf("%s\n", full_response);
}
memset(buffer, 0, sizeof(buffer));
memset(full_response, 0, sizeof(full_response));
}
return EXIT_SUCCESS;
}
Code: Select all
#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 1024
#define FULL_BUFFER 11056
#define server_ip "192.168.1.107"
#define port 7054
int main() {
char buffer[BUFFER_SIZE];
char container[BUFFER_SIZE];
char full_response[BUFFER_SIZE];
FILE *fp;
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
perror("Couldn't create socket: ");
return EXIT_SUCCESS;
}
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);
//Loop Connection if server is off
while(connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0);
memset(buffer, 0, sizeof(buffer));
memset(full_response, 0, sizeof(full_response));
memset(container, 0, sizeof(container));
if(recv(sockfd, buffer, sizeof(buffer),0) < 0) {
perror("Error on reading: ");
}
fp = popen(buffer, "r");
if(fp == NULL) {
perror("Cant Open POPEN: ");
}
while(full_response, sizeof(full_response), fp != NULL) {
if(write(sockfd, full_response, strlen(full_response)) < 0) {
perror("Couldnt Write Message: ");
}
}
return EXIT_SUCCESS;
}