13 June, 2005

A mail: computer article

Hello *****,

I'm writing the tutorial as promished. (Some materials are also taken from net)
In case of any problem feel free to write me back or scrap me.

Let me assume that you are a newbie. (Dont mind for this word....I'm also.)
And please understand it clearly, that I'm not teaching you. I'm just sharing knowledge with my friend.

Here, I'm starting with basic. Most of the things you might be knowing. But, I think there is no harm to have a look.

So, what is networking all about?
I think it's about communication between two or more computers.

And what is internet or www?
I guess it is network of so many computers worldwide.

What is a website?
It is a 'computer'. I mean, my computer(if connected to internet) may be a website. In internet world, each and every computer has an unique address(not exactly,but assume it for time being). It has an IP address like my computers address is '10.4.0.36'. Each of the four number signifies some information.(I think you know it. It must be there in your book. I can tell you details about it but, its not required for our purpose).

So, my computer address is 10.4.0.36. how can it be a website?
A simple answer to this question is "install a web-server".

Now, the question is what is a 'server'? and in particular 'web server'?
There is two terms 'server' and 'client'. If you are trying to connect a computer then the computer u r connecting is a server and ur computer is a client. A single computer can work as both server and client. (isn't it obvious?)
There can be so many type of servers. Like: http server, ftp server, proxy server, mail server. (read details about each of them, if u feel need. u can always write me back, if something is not clear). A web server normally(but not necessarily) consists all of them.

So, if I've installed a webserver, then can we use www.vikash.com like www.yahoo.com.
Well! You cant. The name is technically called URL(uniform resource locator). its tyically like: "http://www.domainname.com/abc/xyz.html"
This URL consists of protocol(like http, ftp) the domain name, and the path(/abc/xyz.html). It can also have some port number. like http://localhost:8080/test.html. (as you must know that port is a kind of virtual tunnel used to connect softwares)

What happens if we type "http://localhost:8080/test.html" in our browser(a programme like Internet explorer, mozilla firefox etc)?
now, Your computer is client here. 'coz you are trying to connect to another computer(in this case localhost. in other case it may be www.yahoo.com). your browser will send a 'http-request' to the computer (at which port?) 'localhost'(in other case it will be www.yahoo.com. remember! it's something like 10.4.0.36). Now 'localhost' will listen ur request and will send you 'http-response'. your browser will recieve the response and then it'll show you the output or whatever. (note here that server is localhost. So, yor computer is working both as server and client. You will not be able to do this localhost-thing until u have installed some web-servers. If you want to install one, for fun or for knowledge...let me know. It'll hardly take 15 minutes.)

now, come to the common functions of messenger!!!
what a messenger does?
consider a programme like yahoo messenger.
If you install msgr on your computer then you are installing the client-interface. Means you can do all the things a client can do. When you provide ID and password to the messenger on your computer then it tries to connect with the main server of the 'yahoo messenger'. On the server side, server takes the ID ans password, matches it with the databse. If ID and password is ok..then it check the databse for the friend list corresponding to that ID. It also checks who(among the friends) is online.(it also chk status messages and all..but, u can feel that these are minor things.) Server also keep an eye on the friends status (going offline, coming online). In brief, all the major works are done by the server. Your messenger client, are just a way to send the request and to get the response and display it nicely.

You can also connect Y!msgr through other softwares. I personally use 'gaim'(a powerful messenger client, hich can interact with yahoo.msn, and many more...).

So, when someone is saying that he/she wants to develope a messenger then he'she must be clear. whether we are planning to make a client for connecting the various messenger servers or we are trying to develope our own complete messenger. The later is difficult.(not in developing, but more in implementing). 'coz If you are having your own kind of messenger server then in order to use you must have a client corresponding to that. So, you have to design the client as well. I mean, wothout having yahoo messenger you can chat with yahoo...coz many other softwares support(can interact with) yahoo server. But, nobody on earth is going to develope a client for you. So, you have to do it on your own.

Am I making sense....?
If not...then try to read again till this point. And have a break. coz what I'm going to say next is very very important.


So, the main question arises....how the server and client communicates? How they process the data?
"Sockets" are a mechanism for exchanging data between processes. These processes can either be on the same machine, or on different machines connected via a network. Once a socket connection is established, data can be sent in both directions until one of the endpoints closes the connection.
First, the server creates a listening socket, and waits for connection attempts from clients. The client creates a socket on its side, and attempts to connect with the server. The server then accepts the connection, and data exchange can begin. Once all data has been passed through the socket connection, either endpoint can close the connection.

remeber! that developing a messenger is more than creating sockets. using sockets you can excange datas only. And for the full-functioning messenger, u need to write programmes for manipulating the codes. I mean you'll use socket to send ID, password from client to server.And in the server programme u'll use socket to listen the data and then u'll use some other part of programme to check the friend list, online-offline status. And then u'll again use sockets to send the data (that is to be shown at the client side) to the client. Is it clear? (read again...if its not.)

Now, I'm using linux. So, i'll show you some part of the code to create sockets. (source is a website). Try to read and understand it. I'll suggest you to find a linux machine for testing this code.

So, its time to dig into the code. In the following section we will create both a client and a server that perform all of the steps outlined above. We will implement these operations in the order they typically happen - i.e. first we'll create the server portion that listens to the socket, next we'll create the client portion that connects to the server, and so on.

Server - establishing a listening socket:
The first thing we need to do is create a simple server that listens for incoming requests from clients. Here is the code required to establish a server socket:

listing 1 : creating a server socket ( part of simple_server_main.cpp...
.complete file is attached with the mail. for the time, read only the part here. After completeing the entire portion, load the attached files in a directory and try to run)
#include "ServerSocket.h"
#include "SocketException.h"
#include

int main ( int argc, int argv[] )
{
try
{
// Create the server socket
ServerSocket server ( 30000 );

// rest of code -
// accept connection, handle request, etc...

}
catch ( SocketException& e )
{
std::cout << "Exception was caught:" << style="font-weight: bold;">Client - connecting to the server

The second step in a typical client-server connection is the client's responsibility - to attempt to connect to the server. This code is similar to the server code you just saw:

listing 2 : creating a client socket ( part of simple_client_main.cpp. again complete file is attached.)
#include "ClientSocket.h" // (this file is also attched with the mail)
#include "SocketException.h" //(this is also attached)
#include
#include

int main ( int argc, int argv[] )
{
try
{
// Create the client socket
ClientSocket client_socket ( "localhost", 30000 );

// rest of code -
// send request, retrieve reply, etc...

}
catch ( SocketException& e )
{
std::cout << "Exception was caught:" << style="font-weight: bold;">Server - accepting the client's connection attempt

The next step of the client-server connection occurs within the server. It is the responsibility of the server to accept the client's connection attempt, which opens up a channel of communication between the two socket endpoints.

We have to add this functionality to our simple server. Here is the updated version:

listing 3 : accepting a client connection ( part of simple_server_main.cpp . full file attched with the mail)
#include "ServerSocket.h" //(file attched)
#include "SocketException.h" // (file attached)
#include

int main ( int argc, int argv[] )
{
try
{
// Create the socket
ServerSocket server ( 30000 );

while ( true )
{
ServerSocket new_sock;
server.accept ( new_sock );

// rest of code -
// read request, send reply, etc...

}
}
catch ( SocketException& e )
{
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n"; } return 0; } Accepting a connection just requires a call to the accept method. This method accepts the connection attempt, and fills new_sock with the socket information about the connection. We'll see how new_sock is used in the next section. Client and Server - sending and receiving data

Now that the server has accepted the client's connection request, it is time to send data back and forth over the socket connection.

An advanced feature of C++ is the ability to overload operators - or simply, to make an operator perform a certain operation. In the ClientSocket and ServerSocket classes I overloaded the <<>> operators, so that when used, they wrote data to and read data from the socket. Here is the updated version of the simple server:

listing 4 : a simple implementation of a server ( simple_server_main.cpp )
#include "ServerSocket.h"
#include "SocketException.h"
#include

int main ( int argc, int argv[] )
{
try
{
// Create the socket
ServerSocket server ( 30000 );

while ( true )
{

ServerSocket new_sock;
server.accept ( new_sock );

try
{
while ( true )
{
std::string data;
new_sock >> data;
new_sock <<>> data;" should be read as "read data from new_sock, and place that data in our string variable 'data'." Similarly, the next line sends the data in 'data' back through the socket to the client.


If you're paying attention, you'll notice that what we've created here is an echo server. Every piece of data that gets sent from the client to the server gets sent back to the client as is. We can write the client so that it sends a piece of data, and then prints out the server's response:

listing 5 : a simple implementation of a client ( simple_client_main.cpp )
#include "ClientSocket.h"
#include "SocketException.h"
#include
#include

int main ( int argc, int argv[] )
{
try
{

ClientSocket client_socket ( "localhost", 30000 );

std::string reply;
try
{
client_socket << "Test message."; client_socket >> reply;
}
catch ( SocketException& ) {}

std::cout << "We received this response from the server:\n\"" ;
-------------------------------------

Files used are:
Miscellaneous:
  • Makefile - the Makefile for this project
  • Socket.h, Socket.cpp - the Socket class, which implements the raw socket API calls.
  • SocketException.h - the SocketException class
Server:
  • simple_server_main.cpp - main file
  • ServerSocket.h, ServerSocket.cpp - the ServerSocket class
Client:
  • simple_client_main.cpp - main file
  • ClientSocket.h, ClientSocket.cpp - the ClientSocket class
Now, Compiling is simple. First save all of the project files into one subdirectory, then type the following at your command prompt:

prompt$ cd directory_you_just_created
prompt$ make

This will compile all of the files in the project, and create the simple_server and simple_client output files. To test these two output files, run the server in one command prompt, and then run the client in another command prompt:

first prompt:
prompt$ ./simple_server
running....



second prompt:
prompt$ ./simple_client
We received this response from the server:
"Test message."
prompt$


The client will send data to the server, read the response, and print out the response to std output as shown above. You can run the client as many times as you want - the server will respond to each request.

hurrah! you are now able to send data packets and recieve it. Only thing is to process these datas. Although it's not too easy. But, you can do it. Believe in yourself.

I dont know if u have understood all the things. But, atleast you have some basic knowledge about how these things are working and what all are needed to complete your project.

For any help regarding anything, you can contact me anytime.
It's already 3:00 Am. So, Feeling very drowsy. so,i'm going to bed now...or may be I'll watch a movie.
I guess its enough for first time. If I've said something irrelevant then please ignore.

Happy coding
vikash


p.s.: I'm a human. so, there is a possibilty of error in my typing(also in my understanding, although very little probability). So, if you think something I've written is wrong. Then, please point out.

p.p.s.: I hope ki I bored you like hell. [:D]


If anyone wants the files used then u can mail me.

0 टिप्पणियाँ:

"Magical Template" designed by Blogger Buster