RSS

Monthly Archives: March 2012

.NET Remoting – Server Activated Single Call

Create an object for each request of single client or multiple clients. This is stateless

Server Application

Student.cs

[Serializable]
public class Student
{
public int stuId;
public string stuName;
public int total;
public float avg;

}

GradeApp.cs

public class GradeApp : MarshalByRefObject
{
public GradeApp()
{
Console.WriteLine("A new client connected");
}

public float calcAvg(int total)
{
return total / 8;
}
public Student getAvg(int total)
{
Student s = new Student();
s.avg = calcAvg(total);
return s;
}
}

Program.cs

TcpChannel tcp = new TcpChannel(8001);
ChannelServices.RegisterChannel(tcp,false);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(GradeApp),"server",WellKnownObjectMode.SingleCall);
Console.ReadKey();

Client Application

Program.cs

TcpChannel tcp = new TcpChannel(0);
ChannelServices.RegisterChannel(tcp, false);

string url = "tcp://localhost:8001/server";
RemotingConfiguration.RegisterWellKnownClientType(typeof(GradeApp),url);

GradeApp ga = new GradeApp();
Student st1 =  ga.getAvg(756);
Console.WriteLine("Average is : " + st1.avg;)
Console.WriteLine("Average is : " + st1.avg;)
Student st2 = ga.getAvg(746);
Console.WriteLine("Average is : " + st2.avg);
Console.ReadKey();
Advertisements
 
Leave a comment

Posted by on March 21, 2012 in C#.Net

 

Tags: , ,

.NET Remoting – Server Activated Singleton

Use the same   object for every client. This is statefull.

Server Application

Student.cs

[Serializable]
public class Student
{
public int stuId;
public string stuName;
public int total;
public float avg;

}

GradeApp.cs

public class GradeApp : MarshalByRefObject
{
public GradeApp()
{
Console.WriteLine("A new client connected");
}

public float calcAvg(int total)
{
return total / 8;
}
public Student getAvg(int total)
{
Student s = new Student();
s.avg = calcAvg(total);
return s;
}
}

Program.cs

TcpChannel tcp = new TcpChannel(8001);
ChannelServices.RegisterChannel(tcp,false);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(GradeApp),"server",WellKnownObjectMode.Singleton);
Console.ReadKey();

Client Application

Program.cs

TcpChannel tcp = new TcpChannel(0);
ChannelServices.RegisterChannel(tcp, false);

string url = "tcp://localhost:8001/server";
RemotingConfiguration.RegisterWellKnownClientType(typeof(GradeApp),url);

GradeApp ga = new GradeApp();
Student st1 =  ga.getAvg(756);
Console.WriteLine("Average is : " + st1.avg;)

Student st2 = ga.getAvg(746);
Console.WriteLine("Average is : " + st2.avg);
Console.ReadKey();
 
Leave a comment

Posted by on March 21, 2012 in C#.Net

 

Tags: , ,

.NET Remoting – Client Activated Objects

Create an object for each client. Use the same object for multiple requests from the same client.

Server Application

Student.cs

[Serializable]
public class Student
{
public int stuId;
public string stuName;
public int total;
public float avg;

}

GradeApp.cs

public class GradeApp : MarshalByRefObject
{
public GradeApp()
{
Console.WriteLine("A new client connected");
}

public float calcAvg(int total)
{
return total / 8;
}
public Student getAvg(int total)
{
Student s = new Student();
s.avg = calcAvg(total);
return s;
}
}

Program.cs

TcpChannel tcp = new TcpChannel(8001);
ChannelServices.RegisterChannel(tcp,false);

RemotingConfiguration.RegisterActivatedServiceType(typeof(GradeApp));
Console.ReadKey();

Client Application

Program.cs

TcpChannel tcp = new TcpChannel(0);
ChannelServices.RegisterChannel(tcp, false);

 string url = "tcp://localhost:8001";
RemotingConfiguration.RegisterActivatedClientType(typeof(GradeApp),url);

GradeApp ga = new GradeApp();
 Student st1 =  ga.getAvg(756);
Console.WriteLine("Average is : " + st1.avg);
 
Leave a comment

Posted by on March 21, 2012 in C#.Net

 

Tags: , ,

PHP User Class

<?php
class User
{
private $user_id;
private $username;
private $password;
private $email;
protected static $table_name="lbs_user";

public function __construct($username="",$password="",$email="")
{
$this->username = $username;
$this->password = $password;
$this->email    = $email;
}

/* Add a new user.
* Returns true on success, false on error */

public function getUserId()
{
return  $this->user_id;
}

public function create()
{
global $gdbObj;
$username = $gdbObj->escape_value($this->username);
$password = $gdbObj->escape_value($this->password);
$email    = $gdbObj->escape_value($this->email);

$sql = "INSERT INTO ".self::$table_name." (username,password,email) values('$username','$password','$email')";
if($gdbObj->query($sql))
{
$this->user_id = $gdbObj->insert_id();
return true;
}
else
{
return false;
}

}

/* Update user profile.
* Returns true on success, false on error */
public function update()
{

}

/* Remove an user.
* Returns true on success, false on error */
public function delete()
{

}

}
$guserObj = new User();
?>


 
1 Comment

Posted by on March 18, 2012 in OOP, PHP

 

Tags:

PHP/MySQL Sample Database Class

<?php
class MySqlDatabase
{
private $_connection;
public $last_query;
private $magic_quotes_active;
private $real_escape_string_exists;

public function __construct()
{
$this->open_connection();
/* Returns the current configuration setting of magic_quotes_gpc.
Returns 0 if magic_quotes_gpc is off, 1 otherwise. */
$this->magic_quotes_active = get_magic_quotes_gpc();
$this->real_escape_string_exists = function_exists("mysql_real_escape_string");
}

public function open_connection()
{
$this->_connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if(!$this->_connection)
{
die('Database connection failed'.mysql_error());
}
else
{
$db_select = mysql_select_db(DB_NAME,$this->_connection);
if(!$db_select)
{
die('Database selection failed'.mysql_error());
}
}
}

public function close_connection()
{
if(isset($this->_connection))
{
mysql_close($this->_connection);
unset($this->_connection);
}
}

/* For SELECT returns resultset on success, false on error.
* For INSERT, UPDATE, DELETE etc returns true on success, false on error */
public function query($sql)
{
$this->last_query = $sql;

$result = mysql_query($sql,$this->_connection);
$this->confirm_query($result);
return $result;
}

/* Make sure query executed successfully.
* Only for debugging purposes. */
private function confirm_query($result)
{

if(!$result)
{
$output = "Databse query failed".mysql_error()."<br>";
$output .= "Last query ".$this->last_query;

die($output);
}
}

/* Escape special characters in the string before sending to the database
* Consider PHP Version*/
public function escape_value($value)
{

if($this->real_escape_string_exists)// PHP version 4.3.0 or higher
{

if($this->magic_quotes_active)
{
/* Automatically add back slashes when magic quotes are active in php.ini
* First remove them. */
$value = stripslashes($value);
}
//Escapes special characters in a string
$value = mysql_real_escape_string($value);
}
else //before PHP version 4.3.0
{

if(!$this->magic_quotes_active)
{
// Add slashes manually
$value = addslashes($value);
}
}

return $value;
}

/* Returns an array of strings that corresponds to the fetched row.
* Returns FALSE if there are no more rows.*/
public function fetch_array($result_set)
{
return mysql_fetch_array($result_set);
}

/* Retrives the number of rows from the result set.
* Returns FALSE on failure. */
public function num_rows($result_set)
{
return mysql_num_rows($result_set);
}

/* Get the ID generated in the last query
* 0 if the previous query does not generate an AUTO_INCREMENT value.
* FALSE if no MySQL connection was established.  */
public function insert_id()
{
return mysql_insert_id($this->_connection);
}

/* Get number of affected rows in previous MySQL operation.
* Return -1 if the last query failed.  */
public function affected_rows()
{
return mysql_affected_rows($this->_connection);
}

}
//Creates an object from MySQLDatabase class
$gdbObj = new MySqlDatabase();
?>
 
8 Comments

Posted by on March 18, 2012 in OOP, PHP

 

Tags:

Track file upload errors in PHP

<?php
if(isset($_POST['btn_upload']))
{

$upload_errors = array(

UPLOAD_ERR_OK                 => "No errors.",
UPLOAD_ERR_INI_SIZE              => "Larger than upload_max_filesize.",
UPLOAD_ERR_FORM_SIZE             => "Larger than form MAX_FILE_SIZE.",
UPLOAD_ERR_PARTIAL             => "Partial upload.",
UPLOAD_ERR_NO_FILE             => "No file.",
UPLOAD_ERR_NO_TMP_DIR         => "No temporary directory.",
UPLOAD_ERR_CANT_WRITE         => "Can't write to disk.",
UPLOAD_ERR_EXTENSION             => "File upload stopped by extension."
);
$error = $_FILES['file_upload']['error'];
$message = $upload_errors[$error];
echo $message;
}
?>

 
Leave a comment

Posted by on March 10, 2012 in PHP

 

Tags:

Get uploaded file information in PHP

<?php
if(isset($_POST['btn_upload']))
{
echo '<pre>';
print_r($_FILES['file_upload']);
echo '</pre>';
}
?>

<form action="upload.php" method="post" enctype="multipart/form-data">
<input name="file_upload" type="file" />
<input name="btn_upload" type="submit" value="Upload" />
</form>

 
Leave a comment

Posted by on March 10, 2012 in PHP