27 January, 2011

python xmlrpc : a simple client server connection mechanism with greater possibilities

A byte of theory . . .

XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a transport. With it, a client can call methods with parameters on a remote server (the server is named by a URI) and get back structured data. This module supports writing XML-RPC client code; it handles all the details of translating between conformable Python objects and XML on the wire.

A byte of surprise . . .


Here the xmlrpc library of the python does the magic, by leaving the  programmer give maximum attention to logic by freeing him from the concepts of the sockets, binding, listening, accepting, etc ... which are the primary constraints while developing a  network based program. Remote Procedure Call (RPC) mechanism allows the client to call any function defined and registered as 'remote' in the server.

A byte of reality . . .


xmlrpc library offers communication between the client and server in such a way that, we can define functions in one machine (say a server) and access those functions from the other machine (say a client). Here the communication occurs in two ways.
    >< client calling the remote server function can 
           pass arguments to it. So the server recieves this 
           arguments 
   ><  Server can transport some data as the return 
           value of the function that the client called

A byte of practical . . .


    Inorder to make the client and server communicate together we need to create the server object and client object with the ip and port of the server. Here i am running the client and server on the same machine. You can replace 127.0.0.1 in sample code with the ip of the server where you need to connect the clients. Port should be greater than the 1024.

server program

   import xmlrpclib
   from SimpleXMLRPCServer import SimpleXMLRPCServer
   def add(a, b) :
         return a+b
   server=SimpleXMLRPCServer(("127.0.0.1",9009))
   server.register_function(add)
   server.serve_forever()


In server program we have to create the object of the server with a method called SimpleXMLRPCServer() with parameters as ip and port. Then the function has to be registered inorder to access it remotely. The serve_forever() function is used to put the server wait for the client request infinitely.
client program
    
    import xmlrpclib
    server = xmlrpclib.ServerProxy('http://127.0.0.1:9009')
    a=input("No 1: ")
   
b=input("No 2: ")
    result = server.add(a,b)
    print "Result  :  "+result

In the client program we will use the ServerProxy() method to specify the details(port, ip) of the server which is having a function named add() which can be called remotely. So two integers a,b are passed as parameters to the remote function. Remote function calculates the sum and returns the result to the called function.

I felt this xmlrpc library as the most awesome mechanism provided by the python. It makes the programming to the very simplest extent so that the users can give maximum attention to developing logic, instead of worrying about the networking constraints and errors related to it...



 
Read rest of entry

08 November, 2010

C program in UNIX to for TCP communication through sockets

Here is a C program in  Unix that introduces the basics of tcp communication by creating sockets. Here the cleint program and server programs communicates by passing messages to the sockets. 

Inorder to run this program store each program in  separate C files and compile it. Inorder to make it working run the programs simultaneosly in two terminals. 

tcp client program
/*program to create tcp client*/
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<limits.h>
#include<fcntl.h>
#include<sys/shm.h>
main()
{
    int fd1,sid,f2,f3,f4,f1;
    char msg1[25],msg2[25];
   
    struct sockaddr_in client,server;
    client.sin_family=AF_INET;
    client.sin_port=htons(0);
    client.sin_addr.s_addr=htonl(INADDR_ANY);
    server.sin_family=AF_INET;
    server.sin_port=htons(1235); //please change this number while copying....
    server.sin_addr.s_addr=htonl(INADDR_ANY);
    sid=socket(AF_INET,SOCK_STREAM,0);
    if(sid<0)
        printf("socket not created\n");//please change this number while copying....
    else
    {
        printf("socket created\n");
        f1=bind(sid,(struct sockaddr*)&client,sizeof(struct sockaddr_in));
        if(f1==-1)
            printf("socket not bind\n");
        else
        {
            printf("socket binded\n");           
            f2=connect(sid,(struct sockaddr*)&server,sizeof(struct sockaddr_in));
            if(f2==-1)
                printf("connection request not send\n");
            else
            {               
                printf("\n\nconnection established\n\n");
                printf("Enter msg\n");
                scanf("%s",msg1);
                send(sid,msg1,sizeof(msg1),0);
                printf("msg send\n");
                recv(sid,msg2,sizeof(msg2),0);
                printf("received\n");
                printf("msg received is:%s\n",msg2);
            }
        }
    }
}







tcp server program

/*program to create tcp server*/
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
int main()
{
    int sid,f1,f2,f3,f4,f5,p;
    char msg1[25],msg2[25];
    struct sockaddr_in client,server;
    server.sin_family=AF_INET;
    server.sin_port=htons(1235);  //please change this number while copying....
    server.sin_addr.s_addr=htonl(INADDR_ANY);
    sid=socket(AF_INET,SOCK_STREAM,0);
    if(sid<0)
        printf("socket not created\n");
    else
    {
        printf("socket created\n");
        f1=bind(sid,(struct sockaddr*)&server,sizeof(struct sockaddr_in));
        if(f1<0)
            printf("socket not bind\n");
        else
        {
            printf("socket bind\n");
            f2=listen(sid,5);
            p=sizeof(struct sockaddr_in);
            f3=accept(sid,(struct sockaddr*)&client,&p);
            if(f3<0)
                printf("connection not accepted\n");
            else
            {
                printf("connection accepted\n");
                recv(f3,msg1,sizeof(msg1),0);
                printf("Message recieved from client:%s\n",msg1);
                printf("Enter the msg\n");
                scanf("%s",msg2);
                f5=send(f3,msg2,sizeof(msg2),0);
                printf("Message send\n");
            }
        }
    }
    return 1;
}
Read rest of entry
 

Terminal Diary | techblog Copyright © 2009 Gadget Blog is Designed by jintu jacob Powered by Blogger