Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,
I have worked on sockets in winforms and console applications
but i am having some problem in writing the same for ASP.Net MVC application.

My Problem is
* How to use System.Net.Sockets in ASp.Net MVC Application to create a simple chat app.
Any ideas, samples or example will be very helpful to me.


I want the socket work interactively...

Thanking you.

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace sgsmSocket.Controllers
{
public class HomeController : Controller
{
string res = "";
Socket server, client;
byte[] _buffer;
public HomeController()
{
StartServer();
}

private void StartServer()
{
try
{
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(new IPEndPoint(IPAddress.Any, 25001));
server.Listen(0);
server.BeginAccept(new AsyncCallback(AcceptCallback), null);
res += ">> Listening";
//Response.Write("Server Running");
}
catch (Exception err)
{
res += ">> Listening Error " + err.Message;
}
}

private void AcceptCallback(IAsyncResult ar)
{
try
{
client = server.EndAccept(ar);
_buffer = new byte[ client.ReceiveBufferSize];
res += ">> Accepted";
client.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);

}
catch (Exception err)
{
res += ">> Accept Error " + err.Message;
}
}

private void ReceiveCallback(IAsyncResult ar)
{
try
{
int received = client.EndReceive(ar);
Array.Resize(ref _buffer, received);
string text = Encoding.ASCII.GetString(_buffer);
Array.Resize(ref _buffer, client.ReceiveBufferSize);
res += ">> Received " + text;

client.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
catch (Exception err)
{
res += ">> Receiving Error " + err.Message;
}
}
//
// GET: /Home/
public ActionResult Index()
{
ViewBag.msg = res;
return View();
}
}
}
Posted
Updated 5-Apr-16 20:18pm
Comments
FARONO 6-Apr-16 1:41am    
You could have a look here http://www.dreamincode.net/forums/topic/33396-basic-clientserver-chat-application-in-c%23/ , but there are probably plenty other examples..
Awesh Vishwakarma 6-Apr-16 1:46am    
Sorry but this is in Console application, i am looking anything in ASP.Net MVC or Simple in ASP.Net.
BTW Thank you
Gautham Prabhu K 6-Apr-16 1:53am    
Most IIS hosting will not allow you to open a socket you will need to assign additional permission to you IIS to make it work and its not good idea security wise.
Awesh Vishwakarma 6-Apr-16 1:55am    
Could you please suggest me alternative approach for the same?
Per Söderlund 6-Apr-16 4:36am    
It sounds weird to me.
Asp.net MVC is already using network communications through http protocol.
But you want to make the server open another socket and use another protocol to communicate with clients that are not browsers or using websockets in browser?

Why not use ajax and mvc views with post and get?

1 solution

Try ASP.NET SignalR[^] - it simulates sockets (even uses WebSockets in the background) and provides "real time" functionality over web.
 
Share this answer
 
Comments
Per Söderlund 6-Apr-16 4:39am    
Good answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900