0

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;
}
5
  • Probably valgrind would be more help than gdb, since " I suspect there is some overflow somewhere but i can't figure out where". This valgrind -v --leak-check=full --show-leak-kinds=all --track-origins=yes call gives lots of feedback detail. – DinoCoderSaurus Sep 29 '16 at 12:31
  • Tried it but did not find anything useful. Now I noticed that sometimes i just have to restart the program and then it works :S ... Feels so random – Jake Sep 29 '16 at 13:35
  • For projects outside CS50's problem sets, SO likely provides better help than this forum does. EDIT: I just saw you posted another question, my suggestion applies to that as well, and any other future questions. – kluvin Sep 29 '16 at 17:23
  • This is my finalproject for the cs50 course – Jake Sep 29 '16 at 20:44
  • Since you're using malloc(), one thing you may want to add is a free() statement. – andy5995 Sep 30 '16 at 17:54

You must log in to answer this question.

Browse other questions tagged .