//这是socket模块里接收数据的回调,事件在这里引发
protected void ReceiveDataCallback(System.IAsyncResult ar)
{
Socket sock=(Socket)ar.AsyncState;
try
{
int nRec=sock.EndReceive(ar);
if(nRec>0)
{
string sReceived
=System.Text.Encoding.BigEndianUnicode.GetString(buff,0,nRec);
//这里引发了数据接受的事件,主要就是这里
if(this.ClientDataReceived!=null)
this.ClientDataReceived(this,
new ClientDataReceivedEventArgs(sReceived));
this.SetupReceiveCallback(sock);
}
else
{
if(this.ClientReceiveError!=null)
this.ClientReceiveError(this,
new ClientReceiveErrorEventArgs("Connection Lost."));
sock.Shutdown(SocketShutdown.Both);
System.Threading.Thread.Sleep(10);
sock.Close();
}
}
catch(Exception ex)
{
if(this.ClientUnknowError!=null)
this.ClientUnknowError(this,
new ClientUnknowErrorEventArgs(ex.Message));
}
}接着是其他模块的:
这是接收到消息的事件处理程序
private void client_ClientDataReceived(ClientSocket sender,ClientDataReceivedEventArgs e)
{
decipher.BeginDecipher(temp);//这是消息解释类
//这个类肯定没问题
}
//消息解释完了显示窗口
private void decipher_ChatterMessage(Eternal.Common.Decipher sender,Eternal.Common.Event.ChatterMessageEventArgs e)
{
//这里的chatter时一个窗体,显示出来就死掉
chatter=new Chatter(e.Message,ref this.client);
chatter.Show();
}
救命阿,这是我的毕业设计
protected void ReceiveDataCallback(System.IAsyncResult ar)
{
Socket sock=(Socket)ar.AsyncState;
try
{
int nRec=sock.EndReceive(ar);
if(nRec>0)
{
string sReceived
=System.Text.Encoding.BigEndianUnicode.GetString(buff,0,nRec);
//这里引发了数据接受的事件,主要就是这里
if(this.ClientDataReceived!=null)
this.ClientDataReceived(this,
new ClientDataReceivedEventArgs(sReceived));
this.SetupReceiveCallback(sock);
}
else
{
if(this.ClientReceiveError!=null)
this.ClientReceiveError(this,
new ClientReceiveErrorEventArgs("Connection Lost."));
sock.Shutdown(SocketShutdown.Both);
System.Threading.Thread.Sleep(10);
sock.Close();
}
}
catch(Exception ex)
{
if(this.ClientUnknowError!=null)
this.ClientUnknowError(this,
new ClientUnknowErrorEventArgs(ex.Message));
}
}接着是其他模块的:
这是接收到消息的事件处理程序
private void client_ClientDataReceived(ClientSocket sender,ClientDataReceivedEventArgs e)
{
decipher.BeginDecipher(temp);//这是消息解释类
//这个类肯定没问题
}
//消息解释完了显示窗口
private void decipher_ChatterMessage(Eternal.Common.Decipher sender,Eternal.Common.Event.ChatterMessageEventArgs e)
{
//这里的chatter时一个窗体,显示出来就死掉
chatter=new Chatter(e.Message,ref this.client);
chatter.Show();
}
救命阿,这是我的毕业设计
把chatter=new Chatter(e.Message,ref this.client)换成显示一个空的FORM应该没问题吧?
问题应该出在Chatter的new方法上了.
问题是,这个窗口类在button的click事件中可以new,并且show
一点问题都没有
在datareceive里就不行了。
因此我认为是我写的socket类和button类之间有一些不一样,是指button类的事件消息是正确的(废话)。但我这里的socket的事件消息肯定存在问题。这里是采用异步,后来我全部改成同步+多线程,问题依旧
谢谢诸位大哥帮忙。要不我的毕业设计要完蛋了
main之前有[STAThread]