I am trying to setup a server that can save some messages sent from a client. I took apart some code given in an example i found here.
This is where I am now.
Ive tried to change some stuff here and there and I find the errors to be quite strange. Sometimes i comment out a function that i do not even call and then it run. Feels random when it works. I suspect there is some overflow somewhere but i can't figure out where.
I can't find anything with gdb.
#include <stdio.h> #include <stdlib.h> #include <string.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 3 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; } int logmessage(char *t) { printf("log[%d] message :%s\n",counter,t); if(counter < LOGSIZE) { set_value(logtable[counter],t); } else { for(int i = 0 ; i < counter -1 ; i++) { //set_value(logtable[counter],t); //set_value(logtable[counter],t); } } printf("logged!"); return 0; } int showlog() { for(int i = 0 ; i < counter-1; i++) { printf("log[%d] = [%s]\n",i,logtable[i]->word); } return 0; } int main(int argc, char *argv[]) { char* msg = "No message recived !\n"; //char buffer[MAXRCVLEN + 1]; /* +1 so we can add null terminator */ char*buffer = malloc(1024); 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 */ socklen_t socksize = sizeof(struct sockaddr_in); memset(&serv, 0, sizeof(serv)); /* zero the struct before filling the fields */ 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 */ mysocket = socket(AF_INET, SOCK_STREAM, 0); /* bind serv information to mysocket */ bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr)); /* start listening, allowing a queue of up to 1 pending connection */ listen(mysocket, 1); int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize); while(consocket) { printf("Incoming connection from %s - sending welcome\n", inet_ntoa(dest.sin_addr)); if(msg != NULL) { len = recv(consocket, buffer, MAXRCVLEN, 0); buffer[len] = '\0'; printf("Received %s (%d bytes).\n", buffer, len); logmessage(buffer); send(consocket, "message recived", strlen("message recived"), 0); close(consocket); consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize); } else { send(consocket, msg, strlen(msg), 0); close(consocket); consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize); } } close(mysocket); return EXIT_SUCCESS; }
valgrind -v --leak-check=full --show-leak-kinds=all --track-origins=yes
call gives lots of feedback detail.