The socket will involve few basic steps which allows you to connect to any server in the world wide web.Or service any client in the WWW.
The socket establish connection between two processes ,processes are nothing but programs and in this case what we are attempting is to connect one program say your browser request to another, the server side one..then you would be a client and a search program running on a google server would be the server side program.
check this out Programs
OK we will begin with the client side.
The client should know the server address which would be the IP address or the internet address also a single server may have multiple services similar to a street with many houses.To identify these services we have what is called a port number ..well actually port here is just a software entity.Which is used to refer to a service such as google translate or google docs etc.
To begin the client creates a socket by using the function socket(),the socket takes in certain arguments which indicate the type of address because ,out there in the real world there exist different address formats ip version 4,ip version 6 ,appletalk and so on.
so you got to mention which address format would the server client have,in our case ipv4 would do fine,the second argument is the type of socket,There exist two types
stream socket : The data transaction is in terms of a stream of characters
datagram socket: The data transaction here is in terms of chunks as though a
entire segment of message is transmitted at one go.
Here we will use stream socket,and for the third argument it takes in the protocol or the rules which should be followed for traveling in the network and here we set it 0 and allow the operating system to choose it for us
sockid = socket(AF_INET, SOCK_STREAM, 0);
AF_INET : ipv4
sockstream: we discussed right
0:OS is looking after this
The second thing the client does once the socket is created is to connect it to the server which we need,for which the client should know the address of the server
connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr))
look we are using the sockid which is termed the file descriptor which is something like the id to a socket ,allowing us to have as many sockets in a program as we wish.
Now don't get scared by the 2nd argument we wont go deep into it ..Its just a pointer to a data entity which contains all relevant information regarding the server which we desire to connect.
But how do the client know which server it needs to connect ?
server = gethostbyname(argv[1]);
we are passing the same as argument it may be an IP address or hostname some thing like mit.edu (this is just an example) would be fine.
Also we need to specify the port id with which the connection is to be made,
portno = atoi(argv[2]);
the 2nd argument of our program will do it for us
once the connection is made voila!! we can talk with the server using the READ and WRITE
read(sockfd,buffer,255);
write(sockfd,buffer,strlen(buffer));
Thats it!! we are done
so in short
1.make a socket socket()
2.connect with server for which IP address and port number is needed
3.use read and write
now to server side Server awaits your presence
The socket establish connection between two processes ,processes are nothing but programs and in this case what we are attempting is to connect one program say your browser request to another, the server side one..then you would be a client and a search program running on a google server would be the server side program.
check this out Programs
OK we will begin with the client side.
The client should know the server address which would be the IP address or the internet address also a single server may have multiple services similar to a street with many houses.To identify these services we have what is called a port number ..well actually port here is just a software entity.Which is used to refer to a service such as google translate or google docs etc.
To begin the client creates a socket by using the function socket(),the socket takes in certain arguments which indicate the type of address because ,out there in the real world there exist different address formats ip version 4,ip version 6 ,appletalk and so on.
so you got to mention which address format would the server client have,in our case ipv4 would do fine,the second argument is the type of socket,There exist two types
stream socket : The data transaction is in terms of a stream of characters
datagram socket: The data transaction here is in terms of chunks as though a
entire segment of message is transmitted at one go.
Here we will use stream socket,and for the third argument it takes in the protocol or the rules which should be followed for traveling in the network and here we set it 0 and allow the operating system to choose it for us
sockid = socket(AF_INET, SOCK_STREAM, 0);
AF_INET : ipv4
sockstream: we discussed right
0:OS is looking after this
The second thing the client does once the socket is created is to connect it to the server which we need,for which the client should know the address of the server
connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr))
look we are using the sockid which is termed the file descriptor which is something like the id to a socket ,allowing us to have as many sockets in a program as we wish.
Now don't get scared by the 2nd argument we wont go deep into it ..Its just a pointer to a data entity which contains all relevant information regarding the server which we desire to connect.
But how do the client know which server it needs to connect ?
server = gethostbyname(argv[1]);
we are passing the same as argument it may be an IP address or hostname some thing like mit.edu (this is just an example) would be fine.
Also we need to specify the port id with which the connection is to be made,
portno = atoi(argv[2]);
the 2nd argument of our program will do it for us
once the connection is made voila!! we can talk with the server using the READ and WRITE
read(sockfd,buffer,255);
write(sockfd,buffer,strlen(buffer));
Thats it!! we are done
so in short
1.make a socket socket()
2.connect with server for which IP address and port number is needed
3.use read and write
now to server side Server awaits your presence
No comments:
Post a Comment