Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

haviong trouble in C with IRC.


ghost's Avatar
0 0

i am trying to make a small VERY VERY basic IRC client…… it is console based and i am trying to get it to send() and recv()…..i get the the first send and the first recv…..but the then it stops and when i press return it says segmentation fault……i don't know why. I use memset to free the msg and buf arrays…..but it doesnt do any good. Here is my source.

––––––––––––––––––––––irc.c–––––––––––––––––––––––––––––––––– #include "prog.h" #include <string.h> #include <stdio.h> #include <stdlib.h> #include <errno.h>

int main(int argc, char *argv[]) { int sockfd, data; char buf[30000]; char *name, *msg; struct hostent *host; struct sockaddr_in addr;

    name= argv[2];
    if(argc &lt; 2)
    {
            printf(&quot;Usage: irc [server_hostname]&#92;n&quot;);
            exit(1);
    }

    if((host = gethostbyname(argv[1])) == NULL)
    {
            printf(&quot;ERROR: could not resolve host&#92;n&quot;);
            exit(1);
    }

    if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    {
            printf(&quot;ERROR: could not connect to server&#92;n&quot;);
            exit(1);
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(IRC_PORT);
    addr.sin_addr = *((struct in_addr *)host-&gt;h_addr);
    memset(addr.sin_zero, &#39;&#92;0&#39;, sizeof addr.sin_zero);

    if(connect(sockfd, (struct sockaddr *)&addr, sizeof addr) == -1)
    {
            printf(&quot;ERROR: could not connect()&quot;);
            exit(1);
    }

    send(sockfd, (&quot;/nick %s&quot;,name), MAXDATASIZE-1, 0);
    while(msg != &quot;exit&quot;)
    {

    printf(&quot;Welcome to %s&#92;n&#92;n&quot;,argv[1]);

    scanf(&quot;|&gt;: %s&quot;, msg);
    send(sockfd, msg, sizeof(msg-1), 0);
    memset(msg, 0, sizeof msg);

    recv(sockfd, buf, MAXDATASIZE-1, 0);
    printf(&quot;%s&quot;,buf);
    memset(buf, 0, sizeof buf);
    }
            close(sockfd);

    return 0;

}


spyware's Avatar
Banned
0 0

I'm not a C++ programmer, but shouldn't you be having a loop or something? Cause now you are just running the entire thing ONCE.


ghost's Avatar
0 0

there is a loop……while( msg != "exit" );