Saturday, April 4, 2015

Server Socket (TCP) With Microsoft Azure

When we working on the real time applications Microsoft azure provides the facilities to run our all kind of applications from oldest to latest technologies. With my earlier post i explained how to create SignalR applications for real time operations

Today I'm gonna introduce create Server socket with the Azure .


1. Create new Azure Cloud project
















2. Create new Azure Worker Role  with the solution. I named it as Socket Worker 














3. Then you need to add endpoint to Worker role. If you are working on TCP Just add TCP endpoint in the worker role  as follows


















External clients use public port to send traffic to service using public port

actually they send traffic through this port .  Here is how people send traffic http://mysitedomain:2020/

Azure load balancer send the traffic (re route the traffic to Worker Role) through the private port to the Virtual Machine that run the worker role

(More)

4. Then Create your regular TCP Listener (It  may be UDP/TCP/Http/Https, Azure support all  ) Code inside the run

Replace the Run() as following with auto reset event

private AutoResetEvent connectionWaitHandle = new AutoResetEvent(false);
public override void Run()
       {
           TcpListener listener = null;
           try
           {
               //Create TCp Listener to Listen endpoint define in the Worker Role
               listener = new TcpListener(RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["TcpEndpoint"].IPEndpoint);
               listener.ExclusiveAddressUse = false;
               listener.Start();
           }
           catch (SocketException)
           {
               Trace.Write("Echo server could not start.""Error");
               return;
           }
 
           //Listen to the all requests 
           while (true)
           {
               //Handle requests Asynchronously 
               IAsyncResult result = listener.BeginAcceptTcpClient(HandleAsyncConnection, listener);
               connectionWaitHandle.WaitOne();
           }
       }

5. Then create Async request handle with following. **All important areas are commented


      /// <summary>
/// Handle Async requests 
/// </summary>
/// <param name="result"></param>
private void HandleAsyncConnection(IAsyncResult result)
{
    // Accept connection 
    TcpListener listener = (TcpListener)result.AsyncState;
    TcpClient client = listener.EndAcceptTcpClient(result);
    connectionWaitHandle.Set();
 
    // Setup reader/writer 
    NetworkStream netStream = client.GetStream();
    StreamReader reader = new StreamReader(netStream);
    // Show application 
    string input = string.Empty;
    while (true)
    {               
 
        //Get the input line 
        input = reader.ReadLine();
 
        Trace.TraceInformation(input);
    }
    // Done! 
    client.Close();
}

Run and enjoy  :)







Full Code In Media Fire 
http://bit.ly/SocketAzure













0 comments: