Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #define MAXRCVLEN 500
- #define PORTNUM 2300
- #define LOGSIZE 50
- int counter = 0 ;
- typedef struct msg * msgref;
- typedef struct msg
- {
- char word[MAXRCVLEN + 2];
- char time[MAXRCVLEN];
- char user[MAXRCVLEN];
- }msg;
- msg* logtable[LOGSIZE];
- int set_value(msgref N , char* w)
- {
- char* temp = w;
- //temp[strlen(temp) - 1] = '\0';
- strcpy(N-> word, temp );
- return 0;
- }
- msgref create_msg(char* w)
- {
- msgref e = malloc(4000);
- set_value(e, w);
- return e;
- }
- int logmessage(char *t)
- {
- //printf("log[%d] message :%s\n",counter,t);
- if(counter < LOGSIZE)
- {
- logtable[counter] = create_msg(t);
- counter++;
- }
- else
- {
- printf("out of space\n");
- for(int i = 0 ; i < counter -1 ; i++)
- {
- printf("[%d]\n",i);
- set_value(logtable[i],logtable[i+1]->word);
- }
- set_value(logtable[counter-1],t);
- }
- printf("logged!\n");
- return 0;
- }
- int showlog()
- {
- for(int i = 0 ; i < counter; i++)
- {
- printf("log[%d] = [%s]\n",i,logtable[i]->word);
- }
- return 0;
- }
- int is_request(char* t)
- {
- char type[6];
- strncpy(type,t,4);
- printf("comparing request [%s]\n",type);
- if(strcasecmp(type,"GET ") == 0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- int main(int argc, char *argv[])
- {
- if(is_request("GET ") == 0)
- {
- printf("GET is = 0\n");
- }
- if(is_request("ABC ") == 1)
- {
- printf("ABC is = 1\n");
- }
- printf("Server starting!\n");
- //char buffer[MAXRCVLEN + 1]; /* +1 so we can add null terminator */
- char*buffer = malloc(MAXRCVLEN +1);
- struct sockaddr_in dest; /* socket info about the machine connecting to us */
- struct sockaddr_in serv; /* socket info about our server */
- int mysocket,len; /* socket used to listen for incoming connections */
- printf("Local vars initialized!\n");
- socklen_t socksize = sizeof(struct sockaddr_in);
- memset(&serv, 0, sizeof(serv)); /* zero the struct before filling the fields */
- memset(&dest, 0, sizeof(dest)); /* zero the struct before filling the fields */
- printf("Memmory been reset!\n");
- serv.sin_family = AF_INET; /* set the type of connection to TCP/IP */
- serv.sin_addr.s_addr = htonl(INADDR_ANY); /* set our address to any interface */
- serv.sin_port = htons(PORTNUM); /* set the server port number */
- printf("Connecton types are set!\n");
- mysocket = socket(AF_INET, SOCK_STREAM, 0);
- if(mysocket == -1)
- {
- printf("Socket error!\n");
- close(mysocket);
- return EXIT_SUCCESS;
- }
- printf("Socket been created with signal : [%d]\n",mysocket);
- /* bind serv information to mysocket */
- int mybind = bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
- if(mybind == -1)
- {
- printf("ERROR! binding port with signal [%d]\n",mybind);
- close(mysocket);
- return EXIT_SUCCESS;
- }
- /* start listening, allowing a queue of up to 1 pending connection */
- int g = listen(mysocket, 200);
- printf("listen to stocket with signal [%d]\n",g);
- int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
- printf("consocket has signal [%d]\n",consocket);
- while(1)
- {
- printf("Incoming connection from %s\n", inet_ntoa(dest.sin_addr));
- len = recv(consocket, buffer, MAXRCVLEN, 0);
- printf("recived data len = %d\n",len);
- buffer[len] = '\0';
- if(len > 0)
- {
- if(is_request(buffer) == 1)
- {
- printf("Is not request\n");
- printf("Received %s (%d bytes).\n", buffer, len);
- logmessage(buffer);
- showlog();
- send(consocket, buffer, strlen(buffer), 0);
- close(consocket);
- consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
- }
- else if(is_request(buffer) == 0)
- {
- printf("Is request\n");
- char*str = malloc(2000*20);
- strcpy(str,"");
- for(int i = 0 ; i < counter; i++)
- {
- printf("insert loggtable[%d] = %s\n",i,logtable[i]->word);
- strcat(str,logtable[i]->word);
- strcat(str,"<msg>\n");
- }
- printf("Sending data -->\n%s\n-end of data-",str);
- send(consocket, str, strlen(str), 0);
- close(consocket);
- consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
- free(str);
- }
- }
- else
- {
- printf("empty message\n");
- close(consocket);
- consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
- }
- }
- close(mysocket);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement