Find the answer to your Linux question:
Results 1 to 2 of 2
hey, all.. i'm trying to code an IRC bot for our channel.. i thought i had the socket code finished, but it seems that it's borked when i try to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078

    call to gethostbyname() segmentation fault


    hey, all.. i'm trying to code an IRC bot for our channel.. i thought i had the socket code finished, but it seems that it's borked when i try to convert the hostname string to an IP address... here's main() up to the bork... there are a couple of other places it doesn't get to because of the segfault, but i think that this is an error that occurs in multiple places in my code...

    Code:
    int main(int argc, char *argv[])
    {
    	int s=0;
    	FILE *fp=NULL;
    	char *nick=NULL;
    	char *chan=NULL;
    	struct hostent *hent=NULL;
    	struct sockaddr_in server;
    		
    	if&#40;&#40;s=socket&#40;AF_INET,SOCK_STREAM,0&#41;&#41;<0&#41;
    	&#123;
    		perror&#40;"socket&#40;&#41;"&#41;;
    		exit&#40;1&#41;;
    	&#125;
    
    	if&#40;&#40;hent=gethostbyname&#40;&#40;char&#41; *argv&#91;1&#93;&#41;&#41;==NULL&#41;
    	&#123;
    		perror&#40;"Error getting hostname."&#41;;
    		exit &#40;1&#41;;
    	&#125;
    i know it's probably horrible socket code, but, hey, i'm just learning this stuff...
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  2. #2
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    okay.. problem solved.. i changed it to not dereference the pointer and it worked... kind of... i get connection refused everytime i try to connect to a server.. which to me says that it's at least trying..
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •