Is there a problem with the memory allocation???


 
Thread Tools Search this Thread
Top Forums Programming Is there a problem with the memory allocation???
# 1  
Old 04-14-2009
Lightbulb Is there a problem with the memory allocation???

I have a scenario like the client has to search for the active server.There will be many servers.But not all server are active.And at a time not more than one server will be active.

The client will be in active state always i.e, it should always search for an active server until it gets one.I have all the servers' ip address in a file.what i thought is like creating a loop that will try to connect with all ip address in the file.if any of the ip address is active it can connect with that server and get the information from a file in the server.It was working properly sometimes and sometimes it displays blank.I think there is some memory problem with the variable "buff".Can someone help me out in this??

i checked with the server program.Its working properly.I think the poblem is with the client program only.





Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#define MAX 100
#define IPSIZE 20

void *get_in_addr(struct sockaddr *sa)
{
    if (sa->sa_family == AF_INET) {
    return &(((struct sockaddr_in *) sa)->sin_addr);
    }
    return &(((struct sockaddr_in6 *) sa)->sin6_addr);
}

char buff[100];
char buf[100];
char *cont;

int main()
{
    int sockfd, new_fd, numbytes;
    struct addrinfo hints, *servinfo, *p;
    struct sockaddr_storage their_addr;
    int yes = 1, len = 16, i = 0;
    char names[20];
    char line[75];
    char opr[2];
    char s[INET6_ADDRSTRLEN];
    char **lines_array;
    int rv, line_counter = 0;;
    FILE *fp;
    struct sockaddr *name;
    socklen_t size;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    fp = fopen("ipadd.ini", "r");
    while (fgets(line, sizeof(line), fp) != NULL) {
    line_counter++;
    }
    lines_array = (char **) malloc((line_counter) * IPSIZE);
    if (fp == NULL) {
    printf("cannot open file");
    return 1;
    } else {
    rewind(fp);
    while (fgets(line, sizeof(line), fp) != NULL) {
        lines_array[i++] = strdup(line);
    }
    }
    printf("Line counter %d", line_counter);
    /* for (i = 0; i <line_counter; i++) 
       {
       printf("\n%s", lines_array[i]);
       }

       free(lines_array); */
    int su = 0;

    while (su == 0) {
    i = 0;

    while (i < line_counter) {
        if ((rv =
         getaddrinfo(lines_array[i], "3456", &hints,
                 &servinfo)) == -1) {
        fprintf(stderr, "getaddrinfo:%s", gai_strerror(rv));
        continue;
        }

        for (p = servinfo; p != NULL; p = p->ai_next) {

        if ((sockfd =
             socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("\n client:socket");
            continue;
        }
        printf(" socket %d\n", sockfd);

        if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            close(sockfd);
            perror("\nclient:connect");
            continue;
        }

        break;
        }


        if (p == NULL) {
        fprintf(stderr, "client:failed");
        continue;
        }

        strcpy(names, lines_array[i++]);

        inet_ntop(p->ai_family,
              get_in_addr((struct sockaddr *) p->ai_addr), s,
              sizeof s);
        int count = 2;
        if (recv(sockfd, buf, sizeof buf, 0) == -1)
        perror("ee");
        len = 0;

        printf("message from server:%s", buf);

        if ((send(sockfd, names, sizeof names, 0)) == -1)
        perror("err");


        if ((recv(sockfd, buf, sizeof buf, 0)) == -1)
        perror("p");
        printf("\n The no of lines in file is %s", buf); 
        len = count = atoi(buf);
        printf("\ncount %d", count);//till this it works good
//-----------------------------------------------------------------------------
        while (count != 0) {
        
        if ((recv(sockfd, buff, sizeof buff, 0)) == -1)
            perror("k");
        printf("\n%s", buff);// Here is the problem:prints blank
        count--;       //The server sends the information         
                               //successfully but the client receives it  
                               //correctly sometimes,but not always. 
        }

//-------------------------------------------------------------------------------------------        
        if (len > 0)
        return 2;
    }
    i = 0;


    }

    return 0;
}

# 2  
Old 04-14-2009
Quote:
Originally Posted by vigneshinbox
[...]I think there is some memory problem with the variable "buff". Can someone help me out in this??

I checked with the server program. It's working properly. I think the problem is with the client program only.
You think, which means you don't know. What did you do to check for the error? Off the top of my head I'd suggest looking at the packets transferred (using tcpdump/wireshark/...) and running both the client and the server in a debugging environment, maybe even with a few more print statements to explain what's going on.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Memory allocation problem

I am using ubuntu. I have written a program to calculate prime factors. it works perfectly fine till entered number is less than 9989 (or so ) but when one enters a number higher than that, for example 15000, it does not work. Can anyone guide me whats the problem ? although new codes are welcome,... (2 Replies)
Discussion started by: Abhishek_kumar
2 Replies

2. Programming

C++/ROOT Memory Allocation?

Hello, I am new to C++ programming, so I'm still getting a feel for things. I recently wrote a simple C++ program (to be used as a ROOT Macro) to conduct a statistical analysis of a varied version of the Monty Hall problem (code below). Basically, the programs runs a few simple calculations to... (7 Replies)
Discussion started by: Tyler_92
7 Replies

3. Programming

memory allocation for string in C

hi in the following code, how the memory is allocated for a1 which holds the values of a2 after cpy function call. #include <stdio.h> #include <string.h> void cpy(char* d, const char* s){ while(*d++=*s++); } main(){ char* a1; char* a2="done"; cpy(a1,a2); ... (3 Replies)
Discussion started by: mprakasheee
3 Replies

4. Programming

Memory Allocation Query

When we dynamically allocate the memory say 100 integers say int *x = new int(1000); then does entire chunk of memory gets allocated at once after the completion of the statement? I mean will the the concept of page fault come into picture over here? (3 Replies)
Discussion started by: rupeshkp728
3 Replies

5. Programming

Memory allocation in C

Hi Experts I need some help in static memory allocation in C. I have a program in which I declared 2 variables, one char array and one integer. I was little surprised to see the addresses of the variables. First: int x; char a; printf("%u %u\n', &x, a); I got the addresses displayed... (2 Replies)
Discussion started by: unx_freak
2 Replies

6. Programming

memory allocation in subroutine

Hi everyone, I'm not new to C programming, but I'm having question regarding the memory allocation of a pointer variable which, for instance, will be declared in main(), but its memory will be allocated in subroutine. To clearify my question, I provide a small working example: #include... (1 Reply)
Discussion started by: MIB_Maik
1 Replies

7. Programming

Memory allocation problem

I have a program that will fetch some particular lines and store it in a buffer for further operations.The code which is given below works but with some errors.I couldn't trace out the error.Can anybody help on this plz?? #include <stdio.h> #include <stdlib.h> #include<string.h> #define... (1 Reply)
Discussion started by: vigneshinbox
1 Replies

8. Programming

Dynamic memory allocation

Hi, I am trying to process line by line of a file. But I should not be allocating static allocation for reading the contents of the file. The memory should be dynamically allocated. The confusion here is how do I determine the size of each line, put it into a buffer with the memory allocated... (11 Replies)
Discussion started by: naan
11 Replies

9. HP-UX

HP-UX memory usage allocation

Hi all, I have a HP-UX Server with 4 gigabytes of physical RAM. When I use the 'Glance' utility to see what my memory utilization is, my memory usage shows up maxed out at 99%. I shut off all the known processes that I'm running on that box and the memory utilization is still at 78% (with Swap... (3 Replies)
Discussion started by: dehuang83
3 Replies

10. UNIX for Dummies Questions & Answers

memory allocation

I would like to know how I could allocate some more memory to a process. Please note that I am not the root user. (1 Reply)
Discussion started by: sagar
1 Replies
Login or Register to Ask a Question