February 12, 2008

After years of programming in Microsoft Windows, now I'm exploring new programming skill in linux. Previously, I've been struggling to run linux on my laptop. I tried Red Hat, Fedora, Mandrake... but they are not stable in my old laptop. Until I found Ubuntu... Now, I'm really satisfied to settle down with this great, easy to use and pretty stable open source Operating System for my laptop. So, I have Ubuntu Gutsy Gibbon 7.10 on my lappy now and I thought I'll forget windows soon...

Normally, people start learning programming with some simple typical Hello World sample. However, I'm searching for some challenge as my first try. So, I pick linux socket programming as my quick start. During my short journey on the web, I found this simple tutorial by Rob Tougher. Pretty cool and complete for my starting point... So, if you wanna try it too, feel free to get the code from Rob Tougher's tutorial page and compile it yourself. Modify the codes as you like and learn new things.

While working on the codes downloaded from that page, I found errors while compiling it. Maybe Rob leave this error for us to learn... Anyway, don't worry mates, I'll share the answer... heheheh...

g++ returns this error not because there is no 'cout' in std. But, because we didn't properly declare the std to use 'cout'. In order to fix this, I just add this line...#include <iostream> in Socket.cpp (you can place it on top, just after all #include codes) and try to 'make' again. I'm sure this error will disappear.

Then, here's the second 'make' trial...apogee@apogee-laptop:~/C-codes/socketserver$ makeg++ -c -o Socket.o Socket.cppg++ -c -o simple_server_main.o simple_server_main.cppsimple_server_main.cpp: In function ‘int main(int, int*)’:simple_server_main.cpp:7: error: ‘cout’ is not a member of ‘std’simple_server_main.cpp:35: error: ‘cout’ is not a member of ‘std’make: *** [simple_server_main.o] Error 1apogee@apogee-laptop:~/C-codes/socketserver$

Ups... still got the error... but this time, it is in simple_server_main.cpp... No problem, let's do the same on this file too... save them and here is the third 'make' trial...apogee@apogee-laptop:~/C-codes/socketserver$ makeg++ -c -o simple_server_main.o simple_server_main.cppg++ -o simple_server ServerSocket.o Socket.o simple_server_main.og++ -c -o ClientSocket.o ClientSocket.cppg++ -c -o simple_client_main.o simple_client_main.cppg++ -o simple_client ClientSocket.o Socket.o simple_client_main.oapogee@apogee-laptop:~/C-codes/socketserver$

SUCCESS!!! without errors anymore... And now, time for testing... open up a terminal and you will see this output when you type ./simple_serverapogee@apogee-laptop:~/C-codes/socketserver$ ./simple_server running....

Then open another terminal and run the simple client... I run it 3 times...apogee@apogee-laptop:~/C-codes/socketserver$ ./simple_client We received this response from the server:"Test message."apogee@apogee-laptop:~/C-codes/socketserver$ ./simple_client We received this response from the server:"Test message."apogee@apogee-laptop:~/C-codes/socketserver$ ./simple_client We received this response from the server:"Test message."apogee@apogee-laptop:~/C-codes/socketserver$

Now, I can start modifying the code to learn how it really works. Feel free to download my 'fixed version' of Rob Tougher's code. You can simply untar it by issuing this command in your newly created project folder...prompt:~$ tar -zxvf simple-socket.tgz

Programming is fun! give it a try if you haven't tried once! Good luck!!