Monday, 28 April 2014

I've been hearing about HTML5 for quite some time now, and I especially liked the idea of web sockets. Giving us the ability to send data directly to the user, instead of doing something like polling with AJAX requests seems like a very neat idea.
So the other day, I decided that I would try to make a small server and test it out on my localhost. As far as I can tell, only Google Chrome supports web sockets at the moment, but I sure hope that it will pick up.
This screenshot is an example of how web sockets could be used (the code is in the attached zip file).

Naturally, I started the development with a Google search; this however didn't really help much. I found a Python and a PHP implementation, but not much about C#. This is my motivation for writing this article; I'll walk though some of the code for the server and a bit about how to interact with it from JavaScript.

Background

The server is heavily based on sockets. I don't think you would need to be very experienced in working with sockets, but a bit of knowledge shouldn't hurt. The code is not entirely simple, I'm using a small amount of delegates and lambda expressions, but again, it’s not very hard either. I guess you'll be fine.

Using the Code

The code is organized into a couple of classes in a class library, which could be included as a project in your solution or compiled into an assembly. It’s hard to create a ready-made demo application as you need a webserver, a browser and the web socket server to run simultaneously in order for it to work. But I'll try to explain how to get it all working. The project contains two main classes: WebSocketServer and WebSocketConnection. The WebSocketServer is responsible for handling all the client connections and the initial handshaking. The WebSocketConnection handles the individual connections. The server listens for connections and creates the connection objects upon connection, and the connection objects are used to interact with the clients (sending/receiving data).
The following is an example of how you could start the server:

var wss = new WebSocketServer(8181, 
                              "http://localhost:8080",
                              "ws://localhost:8181/service");
wss.Start();

The constructor takes three arguments:
public WebSocketServer(int port, string origin, string location)

  • The port on which to listen for connections
  • The origin of the connections (the location of the web page)
  • The location of the server.
The last two arguments are used when "authenticating" the connections through the handshake between the server and the client. The inner workings of the protocol is described here: http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75
To enable you to see what is happening in the server, a very rude logging mechanism is implemented: You can attach a TextWriter to the server, and specify the logging level with the ServerLogLevel enum:


wss.Logger = Console.Out;
wss.LogLevel =ServerLogLevel.Verbose;

There are three different log levels:
ServerLogLevel.Verbose //tells you just about everything 
                       //(including all the data received)
ServerLogLevel.Subtle  //tells you when clients connect/disconnect 
                       //and when they are sending data to the server 
ServerLogLevel.Nothing //well.. nothing.

An even better way keep an eye on what is going on, is to attach methods to the events that the server and the connections are invoking:
wss.ClientConnected += new ClientConnectedEventHandler(OnClientConnected);
 
///
 
static void OnClientConnected(WebSocketConnection sender, EventArgs e)
{
  Console.WriteLine("client connected");
  sender.Disconnected += new WebSocketDisconnectedEventHandler(OnDisconnected);
  sender.DataReceived += new DataReceivedEventHandler(OnDataReceived);
}
 
static void OnDataReceived(WebSocketConnection sender, DataReceivedEventArgs e)
{
  Console.WriteLine("data received");
}
 
static void OnDisconnected(WebSocketConnection sender, EventArgs e)
{
  Console.WriteLine("client disconnected");
}
The "sender" in these methods is always the WebSocketConnection object associated with the connection. The only method that has data in the EventArgs is the OnDataReceived method, the EventArgs(DataReceivedEventArgs) in this method contains the size of the data and the data itself, these are accessible through properties on the object (Size and Data). The data transferred between the client and the server is UFT8 encoded strings (as specified in the protocol).
The above is pretty much the simplest usage example of the classes, but in order to get any clients to connect we need some JavaScript and a browser to run it. But before we continue to the JavaScript, I'd like to just attach a few extra words to the classes:
As I said, the WebSocketSever object listens for new clients and creates the WebSocketConnection objects as new clients connect. The objects are kept in a List, and this list is updated as the clients disconnect. The listener socket is accessible though the ListenerSocket property, and the List though the Connections property. Apart from the Start method, the server contains two public methods; SendToAll and SendToAllExceptOne. These methods can be used to send data to all the connected clients, and to send data to all the connected clients except a specified client (this could be used in a scenario where a client wishes to send data to everybody else). These methods simply call the public Send method on the WebSocketConnection objects in the List. This method sends a string to the client. The WebSocketConnection has one more public function: Close, this method just closes the connecting socket. The socket is available thought the ConnectionSocket property.
The following is an outline of the public methods, properties and events of the two classes:

WebSocketServer:

// an event that is triggered every time a client connects to the server
event ClientConnectedEventHandler ClientConnected;
// a property to access the list of WebSocketConnections of the server
List<WebSocketConnection> Connections;
// a property to access the socket that is listening for new connections
Socket ListenerSocket; 
// a property that can be used to achieve simple logging
TextWriter Logger; 
// a property that determines the level of verbose-ness of the logging
ServerLogLevel LogLevel; 
// a property to access the port the sever is listening to
int Port; 
// sends a string to all connected clients
void SendToAll(string str); 
// sends a string to all connected clients except a specified client
void SendToAllExceptOne(string str, WebSocketConnection indifferent);
// starts the server
void Start(); 

WebSocketConnection:

// method that closes the connecting socket
void Close(); 
// property to access the connecting socket
Socket ConnectionSocket; 
// event that is triggered whenever data is received
event DataReceivedEventHandler DataReceived;
// event that is triggered when the client disconnects
event WebSocketDisconnectedEventHandler Disconnected; 
// a property to get a Guid for the client connection
Guid GUID; 
// method that sends a string to the client
void Send(); 
So! After all that it is time to look at the JavaScript. A very simple client would look a bit like this:
I use JQuery and put the code below inside the $(document).ready() function, but I guess you could do it in a lot of ways.
if('WebSocket' in window){
  connect('ws://localhost:8181/service');
}
 
function connect(host) {
  var ws = new WebSocket(host);
  ws.onopen = function () {
    alert('connected');
  };
 
  ws.onmessage = function (evt) {  
    alert('reveived data:'+evt.data);
  };
 
  ws.onclose = function () {
    alert('socket closed');
  };
};




Monday, 29 July 2013

HR management system project is used in companies for organizing employees inside organization. This system will look after employee recruitment, allocating new employees for a project, career module and admin module. HR management system is managed by HR team for managing employee salaries..etc as explained above. Here we provide Human resource management system project source files and project documentation and abstract for free.



Library Management System on Entity Framework


Hi All this is Library Management System Developed on DotNet using Entity Framework.

System Requirements :
windows xp7 or heigher
Visual Studio 2010 or heigher






Note: just open web project in visual studio and create database by entity data model and run the code.

click here below to download this full source code



Monday, 17 June 2013

connect to sql database using c#
add these library first
using System.Data;
using System.Data.SqlClient;


create string to connect with database
string  str = @"Data Source=(local);Initial Catalog=database;Persist Security Info=True;User ID=username;Password=password";



 SqlConnection con = new SqlConnection(); //create object to connect sql
    SqlCommand cmd = new SqlCommand(); // create object to send command in sql
    DataTable dt = new DataTable();  //create object to store data in temprary
      con.ConnectionString = str; //it open connection for database
        cmd.Connection = con; // it make connection between connection and command
        SqlDataAdapter da = new SqlDataAdapter(cmd); //adatapter object to talk database
            con.Open();//open connection
            cmd.CommandText = "insert into table(comuln1,column2)values('a','b')"; // specify command
            da.Fill(dt);//fire command
con.close();//close connection

//note: if id column is auto then no need to specify id column name to insert


now read database from sql database
create connection as above

System.Data.SqlClient.SqlDataReader SqlDR; // create object to read database
       
  con.Open();
            cmd.CommandText = "SELECT column1,column2 FROM table";
            System.Data.SqlClient.SqlDataReader SqlDR2;
            SqlDR = cmd.ExecuteReader();

            while (SqlDR.Read())
            {

                complainno = SqlDR["column1"].ToString();

            }
            con.Close();



create database in windows application

here is in image given below  you can view how to create sql datbase in windows application.





READ GROUP EMAIL USING C#.

first Create table for user id and password.
like this below

tablae name : emailusers :
1. id int unique
2. userid nvarchar 50
3. pass nvarchar 50



when Timer starts. //set timer as you need
read all userid and password from table that you create by for loop like this below
create connection and retreive all users with pass in grid.
then
do this code
int a;
a=grid.rows.count; //grid is id of gridview
for(int i=0;i<a;i++)
{
string id =grid.rows[i].cell[0].value.tostrig()
string uid = grid.rows[i].cell[1].value.tostrig()
string pass =grid.rows[i].cell[2].value.tostrig()

readmail(uid,pass); //call read mail function

}

public string (string uid,string pass)
{
//use link below to see code to read email inbox
http://ramveersingh.wordpress.com/2011/05/17/get-unread-mail-count-of-gmail-in-c/
//and insert node data from xml in your table
//and all email will insert in your table from all accounts using this code.
//its very easy.

return uid;

}

Saturday, 8 June 2013

Send and Receive SMS in .NET using GSM modem


This source code is for Send and Receive SMS in .NET using GSM modem. You can use this SMS Application program to send SMS from your PC using GSM modem connected to your computer. Its complete working project code for sending and receiving sms using C#.Net. You can implement this project source code to your project and this is very much useful for college students.

Steps to Send and Receive SMS in .NET using GSM modem

First, connect GSM modem to your PC.
Check port connection
Open the application.
Configure the port settings.