December 11, 2009

PHP AJAX simple example

Today I'll write simple AJAX (Asynchronous JavaScript And XML) program using PHP language.
In this example, you will be able to display user a Welcome Message without refreshing the webpage.
The webpage have one Name textbox and a command button. When user enter his name in textbox and press the Hola button, it will display 'Hola User!' message.

If the name textbox is left blank, it will display message 'Hola Guest!'.

Here is the home.html code.


<html>
<head>
<script type="text/javascript" src="simple.js"></script>
</head>
<body>
Name: <input type="text" id="txtName"/>
<input type="button" id="btnHola" value="Hola" onClick="HolaMe(txtName.value)"></input
<p><span id="txtMsg"></span></p>
</body>
</html>

This html file need simple.js javascript file to process your request asynchronously. The simple.js file looks like below

var xmlhttp;
function HolaMe(str)
{
if(str.length==0)
{
document.getElementById("txtMsg").innerHTML="Hola Guest!";
return;
}
xmlhttp=GetXmlHttpObject();
if(xmlhttp==null)
{
alert("Your browser don't support XMLHTTP!");
return;
}
var url="hola.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=showHolaMsg;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function showHolaMsg()
{
if(xmlhttp.readyState==4)
{
document.getElementById("txtMsg").innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}

This javascript check if the textbox is empty it displays 'Hola Guest!' message. Otherwise it sends the user name to hola.php file.

The hola.php file is very simple. It just echo the formatted response.

<?php
$q=$_GET["q"];
$response="Hola ".$q."!";
echo $response;
?>

May 11, 2009

Computer Networks Fundamentals

Today we'll learn some computer networks fundamentals. You might have heard about following terms in day to day life, but this post will give you detail idea about them.

What is Protocol?
Protocol is an agreement between the communication parties about how communication is to proceed.
   
Connection-oriented service: The service first establish connection, uses the connection and then release the connection.

Connection-less service: Each message carries the full destination address and each one is routed independent of others.
  
OSI Reference Model/Open System Interconnect Model:
Physical Layer:  transmitting raw bits over a communication channel.
Data Link Layer: break up input data (raw bits) into data frames (few hundress/thousand bytes) and transmit the frames sequentially. If service is reliable, receiver send Acknowledgment frame.
Network Layer: controls the operation of the subnet.
Transport Layer: accept data from above, split it into smaller units pass these to the network layer and ensure that all arrived correctly at the other end. Also determine what kind of service to provide to Session Layer ultimately to end user of the network.
Session Layer: allows user on different machines to establish sessions between them. Offers various services including dialog control (keeping track of whose turn it is to transmit), token management (preventing two parties attempting the same critical operation at the same time), synchronization (checkpointing long transmissions to allow them to continue from where they were after a crash).
Presentation Layer: unlike lower layers, which are concerned with moving bits around, this layer is concerned with the syntax and semantics of information transmitted. To allow computers with different data representations to communication, the data structures can be defined in abstract way.
Application Layer: contains a variety of protocols needed by user like HTTP (Hyper Text Transfer Protocol). Other application protocols are used for file transfer, email and network news.
   
TCP/IP Reference Model:
Host to network Layer: This is not defined by TCP/IP and it varies from host to host and n/w to n/w. 
Internet Layer: is connectionless internetwork layer. Its job is to permit host to inject packets into any network and have them travel independently to the destination. They may arrive in different order and its job of higher layers to rearrange them. The internet layer define official packet format and protocol called IP (Internet Protocol). The job of Internet layer is to deliver IP packets to destination.
Transport Layer: allow peer entities to carry on a conversation. Two protocols are defined-
1. TCP (Transmission Control Protocol): reliable connection-oriented protocol allows a byte stream originating on one machine to be delivered on other machine. It fragment incoming byte stream into discrete messages and pass them on internet layer. At destination, TCP reassemble incoming messages. TCP also handle flow control to allow fast sender and slow receiver communication.
2. UDP (User Datagram Protocol): unreliable connection-less protocol. Used for once shot, client-server type request reply queries and application in which prompt delivery is more important than accurate delivery (e.g. speech and video transmission).
TCP/IP model does not have session or presentation layers.
Application Layer: Contains all higher level protocols like virtual terminal (TELNET), FTP, and email (SMPT). Virtual terminal allow user on one m/c to log onto distant m/c and work there. The FTP allows user to move data efficiently. SMTP is for email in which other protocols are added like Domain Name System (DNS) for mapping host names onto their network addresses, NNTP the protocol for moving USENET news articles around and HTTP the protocol for fetching pages on the World Wide Web and many more. 
 
DNS(Domain Name System): DNS is used for mapping host names and e-mail destinations to IP addresses.
Working-To map name onto an IP address, an application program calls the resolver with name as parameter. The resolver then sends a UDP packet to a local DNS server which then looks up the name and returns the IP address to the resolver, which then returns it to the caller. The program then can establish a TCP connection or send UDP packets.
 
DHCP(Dynamic Host Configuration Protocol): allows both manual and automatic IP address assignment. The DHCP server need not be on same LAN as the requesting host. Since DHCP server may not be reachable by broadcasting, a DHCP relay agent is needed on each LAN.
To find its IP, newly booted system broadcasts a DHCP DISCOVER packet. The DHCP relay agent on its LAN intercepts all DHCP broadcasts and sends the packet as a unicast packet to the DHCP server possibly on a distant network. The only information the relay agent need is IP address of DHCP server.
Automatic IP assignment use a technique called leasing. Just before lease expires the host must ask DHCP for renewal otherwise host may no longer be able to use same IP. This helps to avoid permanent loss of IP, if host fail to return it to DHCP.

References: Some of the content is referred from the book Computer Networks by A. Tannenbaum

April 25, 2009

Enable OpenMP in Visual Studio

To Enable OpenMP support in Visual Studio:





To use OpenMP, you must do the following


1. Include


2. Enable OpenMP compiler switch in Project Properties>>Configuration Properties>>C/C++>>Language>>OpenMP Support > Yes(/openmp)


3. That's all!



Applicable to: Visual Studio 2008