Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <netinet/in.h>
- #include <pthread.h>
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <stdbool.h>
- #define BACKLOG 10
- #define MAX_SIZE 200
- #define BACKLOG 10
- #define BUFFSIZE 2048
- #define MAXPENDING 5
- #define MAX 2048
- typedef struct server_arg {
- int portNum;
- } server_arg;
- typedef struct server_arg1 {
- int portNum;
- } server_arg1;
- void setup(char inputBuffer[], char *args[], int *background)
- {
- const char s[4] = " \t\n";
- char *token;
- token = strtok(inputBuffer, s);
- int i = 0;
- while( token != NULL)
- {
- args[i] = token;
- i++;
- //printf("%s\n", token);
- token = strtok(NULL,s);
- }
- args[i] = NULL;
- }
- void *terminal_thread(void *arg) {
- while(1) {
- printf(">> ");
- //memset(input_command,0,strlen(str));
- char *input_command = malloc(MAX_SIZE);
- fgets(input_command,MAX_SIZE,stdin);
- if((strlen(input_command) > 0) && (input_command[strlen(input_command) - 1] == '\n'))
- input_command[strlen(input_command) - 1] = '\0';
- char list[] = "ls";
- char cp[]= "cp";
- /*char s[100];
- printf("%s\n", getcwd(s,100));
- chdir("Desktop");
- printf("%s\n", getcwd(s,100));
- */
- if(strcmp(input_command,list) == 0) {
- //ls code will run here
- }
- if (strchr(input_command,'.') != NULL && strchr(input_command,'l') !=NULL) {
- printf("remote ls\n");
- char ip[20];
- const char c[2] = " ";
- //strcpy(str,input_command);
- char *token;
- // get the first token
- token = strtok(input_command, c);
- //walk through other tokens
- int i = 0;
- while( token != NULL && i !=-1 ) {
- token = strtok(NULL, c);
- i--;
- }
- strcpy(ip,token);
- int sock;
- struct sockaddr_in echoserver;
- char buffer[BUFFSIZE];
- unsigned int echolen;
- int received = 0;
- if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
- {
- perror("Failed to create socket");
- exit(1);
- }
- int enable = 1;
- if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&enable,sizeof(int)) < 0) {
- perror("error");
- }
- memset(&echoserver, 0, sizeof(echoserver));
- echoserver.sin_family = AF_INET;
- echoserver.sin_addr.s_addr = inet_addr(ip);
- echoserver.sin_port = htons(5126);
- if (connect(sock,(struct sockaddr *) &echoserver,sizeof(echoserver)) < 0)
- {
- perror("Failed to connect with server");
- exit(1);
- }
- char s[100];
- // while(1) { // to repeat the whole process until exit is typed
- strcpy(s, "ls");
- s[strlen(s)-1]='\0'; //fgets doesn't automatically discard '\n'
- echolen = strlen(s);
- /* send() from client; */
- if (send(sock, s, echolen, 0) != echolen)
- {
- perror("Mismatch in number of sent bytes");
- }
- fprintf(stdout, "Message from server: ");
- int bytes = 0;
- /* recv() from server; */
- if ((bytes = recv(sock, buffer, echolen, 0)) < 1)
- {
- perror("Failed to receive bytes from server");
- }
- received += bytes;
- buffer[bytes] = '\0';
- /* Assure null terminated string */
- fprintf(stdout, buffer);
- bytes = 0;
- // this d {...} while block will receive the buffer sent by server
- do {
- buffer[bytes] = '\0';
- printf("%s\n", buffer);
- } while((bytes = recv(sock, buffer, BUFFSIZE-1, 0))>=BUFFSIZE-1);
- buffer[bytes] = '\0';
- printf("%s\n", buffer);
- printf("\n");
- continue;
- }
- }
- }
- void *server_socket_ls(void *arg) {
- int* exit_status = (int*)malloc(sizeof(int));
- *exit_status = 0;
- while (*exit_status == 0) {
- server_arg *s = (server_arg*)arg;
- int server_fd, new_socket;
- struct sockaddr_in address;
- int addrlen = sizeof(address);
- // Creating socket file descriptor
- if ((server_fd = socket(AF_INET, SOCK_STREAM, 0))
- == 0) {
- perror("socket failed");
- exit(EXIT_FAILURE);
- }
- int enable = 1;
- if(setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&enable,sizeof(int)) < 0) {
- perror("error");
- }
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons(s->portNum);
- if (bind(server_fd, (struct sockaddr*)&address,
- sizeof(address))
- < 0) {
- perror("bind failed");
- }
- printf("About to listen");
- if (listen(server_fd, 3) < 0) {
- perror("listen");
- }
- if ((new_socket
- = accept(server_fd, (struct sockaddr*)&address,
- (socklen_t*)&addrlen))
- < 0) {
- perror("accept");
- }
- printf("Server Connected\n");
- //code for ls
- char buffer[BUFFSIZE];
- int received = -1;
- char data[MAX];
- memset(data,0,MAX);
- // this will make server wait for another command to run until it receives exit
- data[0] = '\0';
- if((received = recv(new_socket, buffer,BUFFSIZE,0))<0){
- perror("Failed");
- }
- buffer[received] = '\0';
- strcat (data, buffer);
- if (strcmp(data, "exit")==0) // this will force the code to exit
- exit(0);
- puts (data);
- char *args[100];
- setup(data,args,0);
- int pipefd[2],lenght;
- if(pipe(pipefd))
- perror("Failed to create pipe");
- pid_t pid = fork();
- char path[MAX];
- if(pid==0)
- {
- close(1); // close the original stdout
- dup2(pipefd[1],1); // duplicate pipfd[1] to stdout
- close(pipefd[0]); // close the readonly side of the pipe
- close(pipefd[1]); // close the original write side of the pipe
- execvp(args[0],args); // finally execute the command
- }
- else
- if(pid>0)
- {
- close(pipefd[1]);
- memset(path,0,MAX);
- while(lenght=read(pipefd[0],path,MAX-1)){
- printf("Data read so far %s\n", path);
- if(send(new_socket,path,strlen(path),0) != strlen(path) ){
- perror("Failed");
- }
- //fflush(NULL);
- printf("Data sent so far %s\n", path);
- memset(path,0,MAX);
- }
- //close(pipefd[0]);
- //removed so server will not terminate
- }
- else
- {
- printf("Error !\n");
- exit(0);
- }
- printf("Enter 1 if you want to exit or 0 if you don't: ");
- fgets(exit_status,MAX_SIZE,stdin);
- }
- }
- void *server_socket_file(void *arg) {
- server_arg1 *s1 = (server_arg1*)arg;
- int server_fd, new_socket;
- struct sockaddr_in address;
- int addrlen = sizeof(address);
- // Creating socket file descriptor
- if ((server_fd = socket(AF_INET, SOCK_STREAM, 0))
- == 0) {
- perror("socket failed");
- exit(EXIT_FAILURE);
- }
- int enable = 1;
- if(setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&enable,sizeof(int)) < 0) {
- perror("error");
- }
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons(s1->portNum);
- if (bind(server_fd, (struct sockaddr*)&address,
- sizeof(address))
- < 0) {
- perror("bind failed");
- }
- if (listen(server_fd, 3) < 0) {
- perror("listen");
- }
- if ((new_socket
- = accept(server_fd, (struct sockaddr*)&address,
- (socklen_t*)&addrlen))
- < 0) {
- perror("accept");
- }
- printf("Server Connected\n");
- }
- int main(int argc, char const* argv[])
- {
- server_arg *s = (server_arg*)malloc(sizeof(server_arg));
- server_arg1 *s1 = (server_arg1*)malloc(sizeof(server_arg1));
- pthread_t id_1;
- pthread_t id_2;
- pthread_t id_3;
- if (pthread_create(&id_3, NULL,(void *) terminal_thread, NULL) != 0) {
- perror("pthread_create");
- }
- s->portNum = 9191;
- pthread_create(&id_1,NULL,(void *)server_socket_ls,(void *)s);
- s1->portNum = 6123;
- pthread_create(&id_2,NULL,(void *)server_socket_file,(void *)s1);
- pthread_join(id_1,NULL);
- pthread_join(id_2,NULL);
- pthread_join(id_3,NULL);
- pthread_exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement