Thread: Where is the Segmentation Fault and How can I fix it?

  1. #1

    Where is the Segmentation Fault and How can I fix it?

    hmm. I get a segmentation fault somewhere in here:

    Code:
    	while(1){
    		memset(buf, '\0', MAXDATASIZE-1);
    		if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
            		perror("recv");
            		exit(1);
            	}
            	buf[numbytes] = '\0';
    		if (recvParser(buf, sockfd) == 0) {
    			printf("%s",buf);
    		}
    	}
    Code:
    int recvParser(char buf[MAXDATASIZE], int sockfd) {
    	char temp[MAXDATASIZE];
    	char *bpt;
    	temp[0] = '\0';
    	if (buf[0] != ':') {
    		bpt = strchr(buf, ':');
    		if (strstr(buf, "PING") >= 0) {
    			cout << bpt << endl;
    			strcat(temp, "PONG ");
    			strcat(temp, bpt);
    			if (send(gSock_fd, temp, strlen(temp)-1, 0) == -1) {
    				perror("send");
    				return -1;
    			}
    		}
    	} else {
    	}
    	return 0;
    }
    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  2. #2

    /me loves IRC

    Yup, thats what i'm writing. It'll hopefully be able to be a bot too. Right now, i'm writing the portion that watches for pings. i got that change made, but now my problem is that i can't figure out how to separate the input into separate lines (because all of my strncmps rely on having the real start of the line). Any ideas or insight? thanks.

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  3. #3
    here is my code (and yes, pings etc don't start with :, only messages start with :)
    Code:
    int recvParser(char buf[MAXDATASIZE], int sockfd) {
    	char temp[MAXDATASIZE+256];
    	char *bpt;
    	temp[0] = '\0';
    	if (buf[0] != ':') {
    		bpt = strchr(buf, ':');
    		if (bpt) {
    			if ((strstr(buf, "PING") - buf) == 0) {
    				cout << bpt << endl;
    				strcat(temp, "PONG ");
    				strcat(temp, bpt);
    				if (send(gSock_fd, temp, strlen(temp)-1, 0) == -1) {
    					perror("send");
    					return -1;
    				}
    			}
    		}
    	} else {
    	}
    	return 0;
    }
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  4. #4
    I want to see if the PING is at the beginning of the line. How would I do this then? Strncat? (If this is so, i would like the syntax... i can't find it)
    Thanks,

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  5. #5

    Strike That

    Howabout this:
    Code:
    int recvParser(char buf[MAXDATASIZE], int sockfd) {
    	char temp[MAXDATASIZE+256];
    	char *bpt;
    	temp[0] = '\0';
    	if (buf[0] != ':') {
    		bpt = (strchr(buf, ':') + 1);
    		if (bpt) {
    			if (strncmp(buf, "PING", 4) == 0) {
    				strcat(temp, "PONG ");
    				strcat(temp, bpt);
    				if (send(gSock_fd, temp, strlen(temp)-1, 0) == -1) {
    					perror("send");
    					return -1;
    				}
    			}
    		}
    	} else {
    	}
    	return 0;
    }
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  6. #6
    Thats why I have this:
    Code:
    	while(1){
    		memset(buf, '\0', MAXDATASIZE-1);
    		if ((numbytes=recv(sockfd, buf, MAXDATASIZE-2, 0)) == -1) {
            		perror("recv");
            		exit(1);
            	}
    		p1 = buf;
            	buf[numbytes] = '\n';
            	buf[numbytes+1] = '\0';
    		while( ( p2 = strchr( p1, '\n' ) ) ) {
    			*p2 =0;
    			++p2;
    			recvParser(p1, sockfd);
    			p1 = p2;
    		}
    		
    	}
    And, my next problem is figuring out how to paste together lines that ARENT whole. That could get tricky.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  7. #7
    I found these functions from when i used to do windows socket programming, and was wondering if they would still work on linux:
    Code:
    int recvLine(SOCKET client, char reply[MAXDATASIZE]){
    
       for (int i = 0; i<MAXDATASIZE; i++){
    
         char ch[1];
    
         if (recv(client,ch,sizeof(ch),0) == 0) return 0;
    
         if (ch[0] == '\r') continue;
    
         if (ch[0] == '\n') break;
    
         reply[i]=ch[0];
    
         //cout << (int)ch[0] << endl;
    
         reply[i+1]='\0';
    
       }
    
       return 1;
    
    }
    
    
    
    int sendLine(SOCKET client, char *buf, bool endLine = false){
    
        int total = 0;        // how many bytes we've sent
    
        int bytesleft = strlen(buf); // how many we have left to send
    
        int len = bytesleft;
    
        int n;
    
    
    
        while(total < len) {
    
            n = send(client, buf+total, bytesleft, 0);
    
            if (n == -1) { break; }
    
            total += n;
    
            bytesleft -= n;
    
        }
    
        if (endLine && n != -1){
    
          send(client, "\n\r", sizeof("\n\r"), 0);
    
        }
    
        return n==-1?len:-1; // return -1 on failure, len on success
    
    }
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  8. #8
    Nevermind. I got them to work. Thanks for your help. I'll start a new thread if anything else comes up.

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed