HOW shoul i arrange the code so that socket creation will take only once.please suggest.
code behind is like this
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = System.DateTime.Now.ToLongTimeString();
// socket creation part
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 4321);
s.Bind(ipep);
IPAddress ip = IPAddress.Parse("226.1.1.1");
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
new MulticastOption(ip, IPAddress.Any));
// data receive and display
DataSize = s.Receive(databuffer);
raw = System.Text.Encoding.ASCII.GetString(databuffer);
Label2.Text = raw;
}
chandra kumarPosted Aug 26, 2013, 10:18 AM
Only one usage of each socket address (protocol/network address/port) is normally permitted
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted
Source Error:
Line 25: Line 26: IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 4321); Line 27: s.Bind(ipep); Line 28: IPAddress ip = IPAddress.Parse("226.1.1.1"); Line 29: s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Sockets;
public partial class _Default : System.Web.UI.Page
{
byte[] databuffer = new byte[28];
int DataSize;
string raw;
public static Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
protected void Page_Load(object sender, EventArgs e)
{
// socket creation operation
if (!IsPostBack)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 4321);
s.Bind(ipep);
IPAddress ip = IPAddress.Parse("226.1.1.1");
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
new MulticastOption(ip, IPAddress.Any));
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
DataSize = s.Receive(databuffer);
raw = System.Text.Encoding.ASCII.GetString(databuffer);
Label2.Text = raw;
}
}
XML part
Iftikar HussainPosted Aug 26, 2013, 5:18 AM
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//do operation
}
}
Regards,
Iftikar
chandra kumarPosted Aug 26, 2013, 5:17 AM
Iftikar HussainPosted Aug 26, 2013, 4:30 AM
You have to call your function on Timer_Tick instead of Page_load
// socket creation part
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 4321);
s.Bind(ipep);
IPAddress ip = IPAddress.Parse("226.1.1.1");
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
new MulticastOption(ip, IPAddress.Any));
// data receive and display
DataSize = s.Receive(databuffer);
raw = System.Text.Encoding.ASCII.GetString(databuffer);
Label2.Text = raw;
Regards,
Iftikar