ninayat
January 24th, 2006, 12:28 AM
I am working on a web chat application that is using Sockets. when a client application recieves message from server application its method is call back named OnDataRecieved(IAsyncResult asyn). but when data is recieved on the client side it does not show tha value of data recieved in textbox, the reason behind is that page is not being refreshed. OnDataRecieved() method is as follows:
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncState ;
int iRx = theSockId.thisSocket.EndReceive (asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
bool chk = false;
//
if (szData=="|\0")
{
chk_list=false;
Object objData = '~' + txtname.Text + '\n';
byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString ());
if(m_clientSocket != null)
{
m_clientSocket.Send (byData);
}
}
else
{
if (chk_list==false)
{
listBox1.Items.Clear();
chk_list=true;
}
if (szData== "~\0")
{
chk = true;
chk_msgtype=true;
}
//
if (chk == false)
{
str+=szData[0];
}
if (szData=="\n\0")
{
if(chk_msgtype == true)
{
listBox1.Items.Add(str.ToString());
chk_msgtype=false;
}
else
{
if (richTextRxMessage.Text!="")
{
/*richTextRxMessage.Text += str;
a.Text=str;
Label1.Text = "1234";*/
Session.Add("message",str);
Response.Redirect("SocketClient.aspx");
//Response.Write(str);
}
else
richTextRxMessage.Text=str;
//richTextRxMessage.Text+="\n";
}
str="";
}
}
WaitForData();
}
when this method calls Response.Redirect("SocketClient.aspx"); it provides followingg error.
An unhandled exception of type 'System.Web.HttpException' occurred in system.web.dll
Additional information: Response is not available in this context.
I want my page to be postback after this method is called but response.redirect is not working. can anybody tell me y isnt it working and how can i perform the same task
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncState ;
int iRx = theSockId.thisSocket.EndReceive (asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
bool chk = false;
//
if (szData=="|\0")
{
chk_list=false;
Object objData = '~' + txtname.Text + '\n';
byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString ());
if(m_clientSocket != null)
{
m_clientSocket.Send (byData);
}
}
else
{
if (chk_list==false)
{
listBox1.Items.Clear();
chk_list=true;
}
if (szData== "~\0")
{
chk = true;
chk_msgtype=true;
}
//
if (chk == false)
{
str+=szData[0];
}
if (szData=="\n\0")
{
if(chk_msgtype == true)
{
listBox1.Items.Add(str.ToString());
chk_msgtype=false;
}
else
{
if (richTextRxMessage.Text!="")
{
/*richTextRxMessage.Text += str;
a.Text=str;
Label1.Text = "1234";*/
Session.Add("message",str);
Response.Redirect("SocketClient.aspx");
//Response.Write(str);
}
else
richTextRxMessage.Text=str;
//richTextRxMessage.Text+="\n";
}
str="";
}
}
WaitForData();
}
when this method calls Response.Redirect("SocketClient.aspx"); it provides followingg error.
An unhandled exception of type 'System.Web.HttpException' occurred in system.web.dll
Additional information: Response is not available in this context.
I want my page to be postback after this method is called but response.redirect is not working. can anybody tell me y isnt it working and how can i perform the same task