Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #include <sys/resource.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <dirent.h>
- //OWN LIBRARIES
- #include "memory.h"
- #include "help.h"
- #include "connection.h"
- //CONSTANTS AND ERROR MESSAGES
- #define SOCKET 7777
- #define MEMORY_CAPACITY 1024
- #define ERROR_ARGUMENT "ERROR - incorrect arguments.\n"
- #define ERROR_SOCKET "ERROR - Socket couldn\'t be created.\n"
- #define ERROR_COMMAND "ERROR - Unknown command.\n"
- //MEMEROY POINTER
- char MEMORY[MEMORY_CAPACITY];
- //DECLARATION OF USED FUNCTIONS
- void info(); //extern function from help.h
- void memory_allocation(char mem[MEMORY_CAPACITY]); //exten function from memory.h
- void initialization(); //exten function from memory.h
- int starting_arguments(char *s, char *t, char *l, char *c, char *d, int argc, char **argv); //function for checking initial arguments for program
- int execute_command(char *command); //function for executing commands
- void print_command(char *buffer, int who); //print command + source of command (socket or standard input)
- void clear_buffer(char *buffer);
- void clear_socket(int socket);
- void listdir(const char *name, int indent);
- /*
- * 1-help
- * 2-halt
- * 3-quit
- * 4-info
- * 5-make*/
- void server() {
- int newsockfd = connection_server(SOCKET);
- char buffer[256];
- int n = 0, i,k=1;
- fd_set master;
- fd_set read_fds;
- FD_ZERO(&master);
- FD_ZERO(&read_fds);
- FD_SET(0, &master);
- FD_SET(newsockfd, &master);
- printf("Waiting for command...\n");
- while (1) {
- read_fds = master;
- //function for checking both inputs (socket and standard input)
- if (select(newsockfd + 1, &read_fds, NULL, NULL, NULL) == -1) {
- printf("select:");
- exit(1);
- }
- //READING FOM SOCKET
- if (FD_ISSET(newsockfd, &read_fds)) {
- int value;
- n = read(newsockfd, buffer, 256);
- printf("%d \n",buffer[0]);
- switch(buffer[0]){
- case(1):
- //help
- vypis_help();
- break;
- case(2):
- printf("halt");
- break;
- case(3):
- printf("quit");
- break;
- case(4):
- printf("info");
- k*=2;
- break;
- case(5):
- printf("make");
- break;
- default:
- break;
- }
- }
- }
- }
- int client() {
- int sockfd = connection_client(SOCKET), n, i;
- char command[20];
- char buffer[256];
- char position;
- FILE *f;
- //SERVER IS OFFLINE
- if (sockfd == -1)
- return 0;
- while (1) {
- bzero(command, 20);
- printf("Prikaz: ");
- scanf("%s", command);
- if(strcmp(command,"help")==0) {
- command[0] = 1;
- command[1] = '\0';
- }else{
- if(strcmp(command,"halt")==0){
- command[0] = 2;
- command[1] = '\0';
- }else{
- if(strcmp(command,"quit")==0){
- command[0] = 3;
- command[1] = '\0';
- }else{
- if(strcmp(command,"info")==0){
- command[0] = 4;
- command[1] = '\0';
- }else{
- if(strcmp(command,"make")==0) {
- command[0] = 5;
- command[1] = '\0';
- }
- }
- }
- }
- }
- n = write(sockfd, command, strlen(command));
- if (n < 0)
- printf("ERROR - writing to socket");
- if(command[0]==3)
- return 0;
- }
- return 0;
- }
- int main(){
- char c;
- vypis_help();
- scanf("%c",&c);
- if(c=='s'){
- server();
- }
- else{
- client();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement