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...
dakc@dakc-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
dakc@dakc-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...#includein 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...
daskc@dakc-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
dakc@dakc-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...
dakc@dakc-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
dakc@dakc-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
dakc@dakc-laptop:~/C-codes/socketserver$ ./simple_server
running....
Then open another terminal and run the simple client... I run it 3 times...
dakc@dakc-laptop:~/C-codes/socketserver$ ./simple_client
We received this response from the server:
"Test message."
dakc@dakc-laptop:~/C-codes/socketserver$ ./simple_client
We received this response from the server:
"Test message."
dakc@dakc-laptop:~/C-codes/socketserver$ ./simple_client
We received this response from the server:
"Test message."
dakc@dakc-laptop:~/C-codes/socketserver$
Now, I can start modifying the code to learn how it really works. . 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!!
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...
dakc@dakc-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
dakc@dakc-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
Then, here's the second 'make' trial...
daskc@dakc-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
dakc@dakc-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...
dakc@dakc-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
dakc@dakc-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
dakc@dakc-laptop:~/C-codes/socketserver$ ./simple_server
running....
Then open another terminal and run the simple client... I run it 3 times...
dakc@dakc-laptop:~/C-codes/socketserver$ ./simple_client
We received this response from the server:
"Test message."
dakc@dakc-laptop:~/C-codes/socketserver$ ./simple_client
We received this response from the server:
"Test message."
dakc@dakc-laptop:~/C-codes/socketserver$ ./simple_client
We received this response from the server:
"Test message."
dakc@dakc-laptop:~/C-codes/socketserver$
Now, I can start modifying the code to learn how it really works. . 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!!





