a few basic things...

This is a discussion on a few basic things... within the C Programming forums, part of the General Programming Boards category; I'm sorry to ask such a newbie question, but I've sort of jumped ahead and skipped some integral peices in ...

  1. #1
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94

    Unhappy a few basic things...

    I'm sorry to ask such a newbie question, but I've sort of jumped ahead and skipped some integral peices in my c knowlege. Basically, this is a small c server, and when it recieves a string, I want to compare it to a known string (ie "hello") and do something. But it says that I can't compare something of different types (I'm using gcc).

    Here's the code, and thanks:
    Code:
    /* A simple server in the internet domain using TCP
       The port number is passed as an argument */
    #include <stdio.h>
    #include <sys/types.h> 
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    void error(char *msg)
    {
        perror(msg);
        exit(1);
    }
    
    int main(int argc, char *argv[])
    {
      int sockfd, newsockfd, portno, clilen; //make objects
      char buffer[256]; //make string
      //strcpy(mystring,"my val");
    
      struct sockaddr_in serv_addr, cli_addr; //make struct
      int n; //make int
    
         if (argc < 2) {
             fprintf(stderr,"usage: %s <port>\n",argv[0]);
             exit(1);
         }
         sockfd = socket(AF_INET, SOCK_STREAM, 0); //create the socket
         if (sockfd < 0) //errors?
           error("ERROR opening socket"); //...
         bzero((char *) &serv_addr, sizeof(serv_addr)); //### don't know (yet)
    
         portno = atoi(argv[1]); //setup the inet struct, btw argv[1]: first argument
         serv_addr.sin_family = AF_INET; // ...
         serv_addr.sin_addr.s_addr = INADDR_ANY; //...
         serv_addr.sin_port = htons(portno); //...
    
         if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) //bind to port
           error("ERROR on binding"); //make sure no error
         listen(sockfd,5); //listen 
         clilen = sizeof(cli_addr); //### don't know
         newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen); //accept connection
         if (newsockfd < 0) //see if it works
           error("ERROR on accept"); //tru dat
         bzero(buffer,256); //### Don't Know
         n = read(newsockfd,buffer,255); //get recieved data
         if (n < 0) error("ERROR reading from socket"); //make sure no error
         
         //### my new section ###
         if (strcmp(buffer,"hello")=0) {
              n = write(newsockfd,"Hello to you too!",17); // write to socket
             if (n < 0) error("ERROR writing to socket"); //check for error
         }
    
         if (strcmp(buffer,"goodbye")=0) {
              n = write(newsockfd,"See ya later!",13); // write to socket
             if (n < 0) error("ERROR writing to socket"); //check for error
         }
    
         if (strcmp(buffer,"test")=0) {
              n = write(newsockfd,"Workin' on this end!",20); // write to socket
             if (n < 0) error("ERROR writing to socket"); //check for error
         }
    
    
         //### end my section ###
    
         n = write(newsockfd,"I got your message",18); // write to socket
         if (n < 0) error("ERROR writing to socket"); //check for error
         return 0; 
    }
    Sorry that was a little long, I didn't want to omit anything.

    THANKS AGAIN!
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    For one thing:
    (strcmp(buffer,"hello")=0)

    should be:
    (strcmp(buffer,"hello")==0)

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    if(blah==blah)
    You need 2 = signes for comparisons. You have
    Code:
    if (strcmp(buffer,"goodbye")=0)
    [EDIT}Sorry beat me when typing[/EDIT]

  4. #4
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    Sorry for asking that, I suppose I should have tryed that... Is there a time when you would use just one =, or is it always ==?

    Also, anyone know of a good tutorial or book on c (*nix)?

    Thanks again
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  5. #5
    c99
    c99 is offline
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    crepincdotcom.

    = is the assignment operator.
    == is the comparison operator.

    A tutorial - http://publications.gbdirect.co.uk/c_book/
    Last edited by c99; 02-24-2004 at 04:21 PM.
    R.I.P C89

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Try this

    The book is not available new anymore, but you can get it used through Amazon or wherever. As far as I can tell the book is almost identical to its counterpart Teach Yourself C Programming in 21 days except this has stuff geared towards compiling on Linux in the first chapter and the book concludes with some stuff about programming with GTK+

    Kernighan and Ritchie's The C Programming Language, Second Edition is also recommended by many people, though it is a little harder for someone absolutely new to programming. If you get a copy of that, you might want to use the book in conjuction with Steve Summit's class notes

    HTH

    ~/

  7. #7
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    Thanks guys, and amazing timing too!

    By the way kermit nice signature... I'm way into security things and was wondering... in the above code, since the buffer was set at 255 charactors, i thought that if I sent it like 500 A's, it would crash/segmentation fault/etc. However I did that, and nothing happened. I also tryed it without the "bzero..." line (thinking that maybe that truncuates it). but to no avail. Is there a way I can purposly leave the above code open to an overflow so that I may examin what happens?

    Wow that was long and off topic, but if anyone has tutorials for that as weel, I'd greatly appreciate it.

    Thanks again,
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    It is relatively easy to leave code susceptible to buffer overflow, but are you sure you really want to do that? Once your program recieves more data than it was designed to handle it starts overwriting parts of memory that it does not even own. The neat part is, that you don't know where that is happening, as it is random. And when you have an overflow, you never know what sort of behaviour to expect. Buffer overflows are not good.

    ~/
    Last edited by kermit; 02-24-2004 at 05:27 PM.

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    ...in the above code, since the buffer was set at 255 charactors, i thought that if I sent it like 500 A's, it would crash/segmentation fault/etc.
    Code:
         bzero(buffer,256); // Fill the buffer with zeros
         n = read(newsockfd,buffer,255); // Get a maximum 255 characters.
    Because you get a maximum of 255 characters your buffer does not overflow and you leave at least one character that is guaranteed to be zero and terminate the string.

    If you want to experiment with buffer overflows change the 255 in the read() call to a number larger than the size of your buffer.

  10. #10
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    Cool thanks, my code is fixed . and yes I know buffer overflows are bad, I'm just going to leave this one open purposfully, and see if I can't find a shellcode that will give me a shell... etc. It seems to me like it's pretty easy to fix your code from overflows, so why are there so many? *cough* sendmail *cough*

    Thanks again
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob with basic q's
    By SimplyComplex in forum C++ Programming
    Replies: 8
    Last Post: 11-19-2006, 12:17 PM
  2. Basic things I need to know
    By maxorator in forum C++ Programming
    Replies: 53
    Last Post: 10-15-2006, 04:39 PM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  4. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 07:41 PM
  5. Basic Window Creation, Dev C++ 4.9.9.0 Linking Error
    By Tronic in forum Windows Programming
    Replies: 2
    Last Post: 11-27-2004, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21