You must Sign In to post a response.
  • Category: ASP.NET

    Chat Application in asp.net c#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class Chat_App : System.Web.UI.Page
    {
    Socket sck;
    EndPoint epLocal, epRemote;
    byte[] bb;

    protected void Page_Load(object sender, EventArgs e)
    {

    sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);


    txtLocalIp.Text = GetLocalIp();
    txtRemoteIp.Text = "192.168.1.3";

    }
    private string GetLocalIp()
    {
    IPHostEntry host;
    host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (IPAddress ip in host.AddressList)
    {
    if (ip.AddressFamily == AddressFamily.InterNetwork)
    return ip.ToString();
    }
    return "127.0.0.1";
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
    ASCIIEncoding aEncodning = new ASCIIEncoding();

    byte[] sendingmessage = Encoding.ASCII.GetBytes(txtMsg.Text);
    sck.Connect(txtLocalIp.Text, Convert.ToInt32(txtLocalPort.Text));
    if(sck.Connected)
    {
    sck.Send(sendingmessage);
    list.Items.Add("Me:" + txtMsg.Text);
    }
    else
    {
    Response.Write("Not Connected");
    }



    }
    private void MessageCallBack(IAsyncResult aResult)
    {

    try
    {
    byte[] receviedata = new byte[1500];
    receviedata = (byte[])aResult.AsyncState;

    ASCIIEncoding aEncoding = new ASCIIEncoding();
    string recevedmg = aEncoding.GetString(receviedata);


    list.Items.Add("from:" + recevedmg);

    bb = new byte[1500];
    sck.BeginReceiveFrom(bb, 0, bb.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), bb);
    }
    catch
    {

    }
    }
    protected void btnConnect_Click(object sender, EventArgs e)
    {
    epLocal = new IPEndPoint(IPAddress.Parse(txtLocalIp.Text), Convert.ToInt32(txtLocalPort.Text));
    sck.Bind(epLocal);


    epRemote = new IPEndPoint(IPAddress.Parse(txtRemoteIp.Text), Convert.ToInt32(txtRemotePort.Text));
    sck.Connect(epRemote);

    bb = new byte[1500];
    sck.BeginReceiveFrom(bb, 0, bb.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), bb);

    }
    }
    There is no error in this code but if i send the message to another system using ip address its not
    working and im not getting where i have gone wrong...... pls pls help
  • #765515
    Your question is seems to be incomplete, without giving elaboration how can we help you to resolve the issue ? Please explain more about your issue
    From the exception giving by you it looks, this is the socket issue and can be resolved it by making sure the newSocket.Connected property was true before any Send/Receive calls. The error message is quite clear. You are calling send() on an unconnected socket and without providing a target address. Where are you sending to?

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765529
    Hi,
    1. Try using this:
    byte[] sendinmessage = Encoding.ASCII.GetBytes(txtMsg.Text);
    2. Which type of Socket you are using to send the data?
    Try this:
    Socket.Connect(IPAddress);
    if (Socket.Connected)
    {
    Socket.Send(sendinmessage);
    }
    else
    {
    //.. not connected ...
    }
    Hope it helps.!

  • #765539
    Hi Akshay,

    Your question is incomplete to answer.Please elaborate to provide you a better solution.


Sign In to post your comments