Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Personal Projects?

  • 15-09-2013 11:24pm
    #1
    Closed Accounts Posts: 3,981 ✭✭✭


    In an effort to get back into Linux system programming I'm writing a web server in C which has basic functionality right now. I am adding to it as time goes on, simply by going through the HTTP RFC and adding features.

    I mostly code in Java & python in work (with a little "C" when using LoadRunner, I use quotes because it's not *really* C), so I'm staying away from those languages for my personal stuff. Besides, C is my favourite language anyway!

    Are you working on any personal projects? If so, what is it? :)


Comments

  • Banned (with Prison Access) Posts: 311 ✭✭Lbeard


    [-0-] wrote: »
    In an effort to get back into Linux system programming I'm writing a web server in C which has basic functionality right now. I am adding to it as time goes on, simply by going through the HTTP RFC and adding features.


    I've never programmed professionally (I've worked in other areas of IT), I would be interested in learning how to write code on linux (I do know C - though I'm patchy on it - I was able to do far more than 'hello world' ) . But I don't really feel like getting bogged down in ... in the bog - I hated programming on Windows. I hack at c files on linux when it goes wrong for me, but I'd like to be able to do more. There are few little projects I'd have in mind. Some simulations, and maybe a modified hardware driver.

    What's the best approach to getting started? I know that's a terribly vague question. Just what are the pratfalls. What editor should I use? Any time I've seen someone programming linux, they used a Visual Studio on a PC, and then compiled on the linux box. I've never used Visual Studio (or haven't gotten into it) Is it worth using, I'm consider knuckling down to some PHP/MYSQL - should I learn to use Visual Studio?


  • Closed Accounts Posts: 3,981 ✭✭✭[-0-]


    Lbeard wrote: »
    I've never programmed professionally (I've worked in other areas of IT), I would be interested in learning how to write code on linux (I do know C - though I'm patchy on it - I was able to do far more than 'hello world' ) . But I don't really feel like getting bogged down in ... in the bog - I hated programming on Windows. I hack at c files on linux when it goes wrong for me, but I'd like to be able to do more. There are few little projects I'd have in mind. Some simulations, and maybe a modified hardware driver.

    What's the best approach to getting started? I know that's a terribly vague question. Just what are the pratfalls. What editor should I use? Any time I've seen someone programming linux, they used a Visual Studio on a PC, and then compiled on the linux box. I've never used Visual Studio (or haven't gotten into it) Is it worth using, I'm consider knuckling down to some PHP/MYSQL - should I learn to use Visual Studio?

    The best approach really is just to get stuck in. Familiarize yourself with C. You don't have to know it inside out, but know the different data types, arrays, pointers, functions.

    This is a great book on C. There's a newer version coming out in December. http://www.amazon.com/gp/product/0672326965

    This is a good book on Linux System Programming: http://www.amazon.com/gp/product/1449339530
    It's pretty new too so it tackles the latest kernel.

    This is the bible. A fantastic reference book: http://www.amazon.com/Linux-Programming-Interface-System-Handbook/dp/1593272200

    I have all three books, and I'm using them to get by. Learning as you go is your best bet. I fail to see the point in trying to sit down, read all three books and then try to code. I used to write device drivers back in the day, so I'm picking this up quickly enough. Like riding a bike really.

    I had the following book but I left it in Ireland when I moved to the states: http://www.amazon.com/Beginning-Linux-Programming-Neil-Matthew/dp/0470147628/

    This book was a great book for learning how to use the development tools, makefiles and so on.

    I also ordered the following book on GCC and I'm awaiting it's arrival: http://www.amazon.com/gp/product/0954161793/

    My aim is simply to read one of these books for about an hour a day, and then spend a while writing some code. It doesn't matter if it's one line of code, or even just some pseudo code.

    I started the web server two days ago, and I haven't spent long on it. Here's how the code looked before I made it a daemon. This simply reads a config file, and outputs a few lines to syslog:
    #include <stdio.h>
    #include <stdlib.h> // used for exit(int)
    #include <unistd.h> // used for fopen(char *, char*)
    #include <syslog.h>
    void read_config(char *);
    void daemonize();
    void init_logging();
    typedef struct {
            unsigned short int port;
            char dir[128];
    } config;
    config conf;
    char * file = "httpd.conf";
    int main(void) {
            init_logging();
            read_config(file);
            daemonize();
            closelog();
            return 0;
    
    }
    
    // read config
    // as of now config is PORT=8888 followed by DIR=directory
    // we use fscanf (from stdio.h) to read it
    
    void read_config(char * f) {
            FILE * cfile;
            cfile = fopen(f, "r+");
            if(cfile == NULL){
                    syslog(LOG_ERR, "Error opening config file %s", file);
                    closelog();
                    exit(1);
            }
            else {
                    printf("Config file: %s successfully opened!\n", file);
                    if(fscanf(cfile, "PORT=%d\nDIR=%128s\n", &conf.port, conf.dir) == 2) {
                            if(conf.port > 8888) {
                                    syslog(LOG_ERR, "Error! port number is greater than 8888. Exiting. :(");
                                    closelog();
                                    fclose(cfile);
                                    exit(1);
                            }
                            printf("Found port number in config, it's %d\n", conf.port);
                            printf("Found directory name in config, it's %s\n",conf.dir);
                 }
                 else {
                            syslog(LOG_ERR, "Error reading config file. Exiting. :(");
                            closelog();
                            fclose(cfile);
                            exit(1);
                      }
            }
            // if we get here, we're done loading the config file so lets close it
            fclose(cfile);
    
    }
    void init_logging() {
            openlog("httpd", LOG_USER| LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0);
            syslog(LOG_INFO, "httpd started by user %d", getuid());
    }
    //become a daemon
    //fork process
    //setsid
    //chdir and open port
    //accept connections
    void daemonize() {
    
    }
    

    As regards to an editor, I'm just using vi with syntax highlighting on for now. I hope this helps.


  • Banned (with Prison Access) Posts: 311 ✭✭Lbeard


    [-0-] wrote: »
    The best approach really is just to get stuck in. Familiarize yourself with C. You don't have to know it inside out, but know the different data types, arrays, pointers, functions.

    I was once very familiar with it. Windows programming really drove me up the wall though. The whole Microsoft API, seemed to me to be so convoluted and murky, if your code worked, you wouldn't have a great idea why it did. But things drove me up the wall. Like trying to find out how to create the exe as a com - creating a daemon - accessing the peripherals - staying up nearly all night reading lots of documentation that would take me nowhere. Writing a simple program to send a UDP datagram. And it not working. Just really annoying stuff. Or just doing a skeleton device driver, that I could work from.

    I think to do any heavy duty windows program, you needed a debug version of Windows (one with the source code) and to know it in depth, or at least knowing what to avoid. Fine If was a professional windows applications programmer. A lot of professional programmers never go that deeply into it - but it's even horrible for doing database apps. Torture is torture.

    And working in IT, I really developed a hatred for IT in general.

    But I striking difference I see with Linux, is everything seems to be there an accessible.

    This is a great book on C. There's a newer version coming out in December. http://www.amazon.com/gp/product/0672326965
    I think I had an earlier version of that book.
    This is a good book on Linux System Programming: http://www.amazon.com/gp/product/1449339530
    It's pretty new too so it tackles the latest kernel.

    This is the bible. A fantastic reference book: http://www.amazon.com/Linux-Programming-Interface-System-Handbook/dp/1593272200
    They look interesting.
    I have all three books, and I'm using them to get by. Learning as you go is your best bet.
    I fail to see the point in trying to sit down, read all three books and then try to code.
    I've a lot of heuristics for learning things these days. One is if the book is ponderous, seems to be fleshed out for the sake of it, it may not be worth reading. Just browsing the folders - and just reading the standard functions.

    One thing that always got me about books is how they never seem to cover how real C programmers, or the programmers of other languages programmer.
    I used to write device drivers back in the day, so I'm picking this up quickly enough. Like riding a bike really.
    There was some projects I wanted to do on Windows, that should have been relatively straightforward. I want to do a pseudo device driver - that could appear and be installed as a MIDI device (Musical Interface Digital Interface), but send and receive over either ethernet, or IP. It should be pretty simple and straight forward. The MIDI protocol is a very simple datagram - all an application does with a MIDI device driver is listen for or send the datagram. I imagined it would be simply a case, of creating a skeleton multimedia device driver, getting the in/out stream packaging in a UDP datagram, and sending it off to wherever - the network API. I couldn't get it to work, and no one to help me. And I got a splitting headache.

    But interestingly that is a little project no one has done, at I've never been able to find it. MIDI is the universal standard for musical devices - and software that does musical things. There are so many reasons a little MIDI/UDP thingy could be so useful. Cables are a pain in the arse. If you have a lot of midi hardware devices you need specialised hardware routing equipment. And run cables all over the place.

    It would be a handy Linux device driver too. Any MIDI apps on Linux would be integrated with MS or other platform MIDI apps without headaches. MIDI/UDP/Wireless would be a killer.
    My aim is simply to read one of these books for about an hour a day, and then spend a while writing some code. It doesn't matter if it's one line of code, or even just some pseudo code.
    Yeah, I had a little for the first time in years the other night. Got my program to work, but lots of little weirdness with arrays - I just looked my source file and it was scrambled, I'm not sure what did it.

    One thing I've noticed about GCC is it's really unforgiving. Instant punishment. The little things I was doing with arrays, elsewhere you might get away with it.

    I'm not sure where I went wrong. Following code snippets on line I used char *str, and treated it as an array. Though without allocating it any memory - or knowing precisely where it was putting the strings - and occasionally getting very strange results.
    I started the web server two days ago, and I haven't spent long on it. Here's how the code looked before I made it a daemon. This simply reads a config file, and outputs a few lines to syslog:
    You have neat easy to read code. Post the rest. It's very difficult to learn from standard production code.

    As regards to an editor, I'm just using vi with syntax highlighting on for now. I hope this helps.
    I used nano. It has syntax highlighting. but it's not perfect - gcc gives me line number errors, and I can't see how to switch on the line numbers on nano. The benefit of a properly integrated dev environment, is that it is integrated, a crash bug bouncing you back to the line of the crash. Or stepping you through the code for warnings or just stepping through.


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Lbeard wrote: »
    I was once very familiar with it. Windows programming really drove me up the wall though. The whole Microsoft API, seemed to me to be so convoluted and murky, if your code worked, you wouldn't have a great idea why it did. But things drove me up the wall. Like trying to find out how to create the exe as a com - creating a daemon - accessing the peripherals - staying up nearly all night reading lots of documentation that would take me nowhere. Writing a simple program to send a UDP datagram. And it not working. Just really annoying stuff. Or just doing a skeleton device driver, that I could work from.

    If you have never programmed professionally how can you hold such a strong opinion? Your program didn't work therefore windows sucks? I had problems with winsock too many years ago, but I figured it out.

    Having done lots of c++ on linux and windows I can tell you there really isn't that much difference. Everyone uses abstraction libraries like boost these days. Sure if you are coding stuff like device drivers you may have to delve into the internals more but most development does not do this.

    The stuff you are saying sounds like it's from 20 years ago. You don't have to worry about using an exe instead of a com today.... just like you don't need to worry about linuxs obsolete binary formats pre-ELF. Well.. unless you have to maintain a legacy system ofc... Can even go a step further and say noone uses C/C++ anymore! It's all about the high-level languages these days, java, python, c# etc.

    The best c++ IDE is Visual Studio (2012!!!), and it's windows only. Eclipse and the various alternatives are painful to use in comparison. If you don't want to use an IDE then this isn't an issue, but I can't imagine working on large complex projects without one. For learners just using gcc and command line is better tho, it's more explicit.

    Your points about "needing a special debug version of windows" (to debug I assume) are half right half wrong. It works the same on linux, you need debug libraries if you want to debug inside those libraries. On windows this will usually mean debug versions of the dll, on linux this will mean debug versions of the so (shared object).

    One final tip: Yes gcc can be unforgiving - I have spent much time poring over it's cryptic output. Try clang - a compatible alternate compiler. It's output is a LOT better I find - the error messages actually pinpoint the problem rather than being a vague symptom as with msbuild and gcc.


  • Registered Users, Registered Users 2 Posts: 2,021 ✭✭✭ChRoMe


    srsly78 wrote: »

    One final tip: Yes gcc can be unforgiving - I have spent much time poring over it's cryptic output. Try clang - a compatible alternate compiler. It's output is a LOT better I find - the error messages actually pinpoint the problem rather than being a vague symptom as with msbuild and gcc.

    Interesting to know, I have to admit the compiler messages for C++ are probably the biggest reason I loathe to use the language.

    My current person project is implementing Goal Orientated Action Planning to setup a AI sandbox visualized with box2d.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 11,264 ✭✭✭✭jester77


    I got myself a raspberry pi a while back. I don't do much sys admin/site ops type stuff in my current job, so have been using it to play around with. IP Tables, Geo blocking, nginx, openvpn, dnsmasq are some of the stuff I have gotten stuck into so far with a small bit of scripting to solve issues I have at home, like auto rebooting my dodgy router when it decides to go offline.

    I must say that the raspberry pi rocks, best few €'s I've spent in a long time.


  • Closed Accounts Posts: 3,981 ✭✭✭[-0-]


    Lbeard wrote: »

    You have neat easy to read code. Post the rest. It's very difficult to learn from standard production code.

    It's early days but here you go: https://github.com/petermalone/httpd

    I do not guarantee that this code is vulnerability free ( there are several vulns in it - can you find them? :) ), and its for educational purposes only.

    I haven't cleaned up the code yet, so I hope it's not too unreadable. This is version 0.1, it won't be finished for ages.


  • Closed Accounts Posts: 3,981 ✭✭✭[-0-]


    Lbeard, you should also check out what Robert Love has to say on the matter.

    http://www.quora.com/C-programming-language/What-are-some-must-read-books-for-a-C-programmer


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    ChRoMe wrote: »
    Interesting to know, I have to admit the compiler messages for C++ are probably the biggest reason I loathe to use the language.

    The Clang front end for LLVM does a decent job of sorting this. Best compiler out there, supports the most of C++11 out of all of them. Still a bit of a work in progress on Windows though.


Advertisement