Search This Blog

Wednesday, April 7, 2010

Using PHP and C to read data from client and send to server

Once i faced a problem like we have to connect the device(RS232 or serial port) to the client machine and we have to get the data from the client machine to the server when user performs an action like click of a button or blur from a control etc


As if we are using PHP,Apache on linux platform on server and windows for client machine and also the application in which i was trying to add this feature runs on browser 



So what i did is created a 'C' Program complied on different platforms, Program in the RS232 will send the data from the client to the server in particular time interval and the program on the Server will store the incoming data in a text file on the server when user performs action on the client then client request to the server for data and the server gives the data to the client (this is achieved using Ajax) then the data is shown in the desired field after user confirms that it is taken further (stored to database ) 

Data integrity is achieved using the time stamp comparison of data generated from the client and the request given time (time when user performs action)

One thing i want to clarify is while we run applications through browser its not allowed to access the system resources directly 

C program code hints 

Server Code:
sd = socket(AF_INET, SOCK_STREAM, 0);


   /* bind server port */
   servAddr.sin_family = AF_INET;
   servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
   servAddr.sin_port = htons(SERVER_PORT);


   if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) {
   }


   listen(sd,5);
.
.
.
cliLen = sizeof(cliAddr);
newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen); 
.
.
.



strftime(time_data,100," %d-%m-%Y %H:%M:%S ",timeinfo);
        fp=fopen("data4.txt","a");
         fputs(time_data,fp);
fputs(line,fp);
fclose(fp);

Client Code :



h = gethostbyname(HOST);
if(h!=NULL)
{
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr,
h->h_addr_list[0],h->h_length);
servAddr.sin_port = htons(SERVER_PORT);
sd = socket(AF_INET, SOCK_STREAM, 0);


/* bind any port number */
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(0);


rc = bind(sd, (struct sockaddr *) &localAddr,
sizeof(localAddr));
rc = connect(sd, (struct sockaddr *) &servAddr,
sizeof(servAddr));
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
.
.

res = read(fd,buf,255);   
buf[res]=0;



rc = send(sd, buf, strlen(buf) + 1, 0);


.
.
close(sd);



Thursday, April 1, 2010

Installing ANT (Windows)

1. Download ANT
2. Unzip the zip file ( C:/ant)
3. Set ANT_HOME environment variable to the Ant location
(MyComputer -->Properties --> Advanced Tab --> Environment Variables --> Add --> Set New Variable Name as "ANT_HOME" and set New Variable Value as C:/ant)
4. Now set Path variable to point to your ANT bin directory
(User Variables for Administrator --> Click on Path variable --> Append the ANT bin location ,  %ANT_HOME%\bin)
5. Open a command prompt and type "ant" , In case of any failure try the below command and see the value for ANT_HOME
c:\echo %ANT_HOME%         


Sunday, March 28, 2010

Maintainable JavaScript



What it means
Code should be Understandable to others as well as for us after sometime, Intiuitve,Adaptable, Extendable, Debuggable
Maintainability is writing or making code easy to understand and easy to debug

Why it is needed


  1. Mostly we are maintaining the code rather writing a new piece.

  2. It takes more time to figure out where we have to look out to solve a problem comparitively to create a solution for the problem

  3. It will be helpful for Co-worker present and in future.

  4. It will be helpful for Development community if we work for opensource projects
How it can be achieved

  1. Indenting Code which is easy as well as very important

  2. Commenting the code atleast at the top of each function and classes , while we use large code chunks , if we write any new algorithm or if we write any hack for display purpose on various browser

  3. Variable name should be nouns and function name should start with verbs with their usage.

  4. In client side we have three components they are JavaScript (behaviour),CSS(Presentation) and HTML (Structure )

  5. Maintain these separate dont mingle these things in one file which will leads to confusion.

  6. Keep Bussiness logic out of event handler.

  7. Dont extend objects which are native (Array,String,RegExp)

  8. Dont override methods which are from native objects

  9. Functions and variables should be attached to an object

  10. Dont use global variables instead use var Constants={name:value}

  11. Throw your own errors

  12. Avoid null comparisons (variable =='null' instead use variable instanceof Array or typeof variable =='string')

  13. Use constants when it is UI string ,repeated value ,URL and values which may change the future

What is HTTPS



  1. In many ways, https is identical to http, because it follows the same basic protocols.
  2. Use of https in a URI scheme rather than http indicates that an encryptedconnection is desired.
  3. Https works by transmitting normal http interactions through an encrypted system, so that in theory, the information cannot be accessed by any party other than the client and end server
  4. Https uses 443 port by default it can be changed using httpd.conf on apache server similar to port 80 on apache
  5. There are two common types of encryption layers: Transport Layer Security (TLS) and Secure Sockets Layer (SSL), both of which encode the data records being exchanged.
  6. Concept how https works : When using an https connection, the server responds to the initial connection by offering a list of encryption methods it supports. In response, the client selects a connection method, and the client and server exchange certificates to authenticate their identities. After this is done, both parties exchange the encrypted information after ensuring that both are using the same key, and the connection is closed
  7. In order to host https connections, a server must have a public key certificate, which embeds key information with a verification of the key owner's identity
  8. Most certificates are verified by a third party like goDaddy.
  9. Https is used in many situations, such as banking and where information security plays more vital role
  10. eg :https://mail.google.com/


    Please leave your comments 

Thursday, March 18, 2010

Install Tamil font in kubuntu

Install tamil font package 

 gt@gt-laptop:~$ sudo apt-get install language-support-ta

Install Scim (Input language can be changed using system tray flag icon)


sudo apt-get install scim

Default Font directory





cd /usr/local/share/fonts/


Create Dir 

mkdir truefonts

Copy tamil fonts here after downloading 

Update new fonts 

sudo mkfontscale && sudo mkfontdir && sudo fc-cache

Restart X server or reboot system

Done
....