Simple Linux Socket Programming in Ubuntu

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...

Here's the error on my first try...

apogee@apogee-laptop:~/C-codes/socketserver$ make
g++ -c -o ServerSocket.o ServerSocket.cpp
g++ -c -o Socket.o Socket.cpp
Socket.cpp: In member function ‘int Socket::recv(std::string&) const’:
Socket.cpp:135: error: ‘cout’ is not a member of ‘std’
make: *** [Socket.o] Error 1
apogee@apogee-laptop:~/C-codes/socketserver$


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$ make
g++ -c -o Socket.o Socket.cpp
g++ -c -o simple_server_main.o simple_server_main.cpp
simple_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 1
apogee@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$ make
g++ -c -o simple_server_main.o simple_server_main.cpp
g++ -o simple_server ServerSocket.o Socket.o simple_server_main.o
g++ -c -o ClientSocket.o ClientSocket.cpp
g++ -c -o simple_client_main.o simple_client_main.cpp
g++ -o simple_client ClientSocket.o Socket.o simple_client_main.o
apogee@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_server

apogee@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!!

Comments

dirn said…
Thank you for the post. I've been searching for this kind of info...
Thijs said…
Yeah, thanks a lot for the link to the tutorial and your solutions. It all helped me develop my skills.
Admin said…
What would recommend for writing this software but to provide a GUI to the user ala Visual Basic?

Popular posts from this blog

How to Create Hyperlink on Blogger Post

How to Add a Sudo User on AlmaLinux 9.2 (Turquoise Kodkod): A Step-by-Step Guide

How to Show and Hide Text in Blog Post