状况...
服务器端和客户端都是自己写..
然后服务器端运行.客户端第一次可以正常连接,然后点击客户端关闭,(相当于非法关闭程序)
然后服务器段就开始死循环...
提示:"未将对象引用设置到对象的实例。"
麻烦各位大大帮忙看看哪里逻辑架构有问题..或者有什么地方不对的..
服务器端代码private void button1_Click(object sender, EventArgs e)
{
listener = new TcpListener(myip, myport);
listener.Start();
Thread thread = new Thread(new ThreadStart(recive));
thread.Start();
}
private void recive()
{
while (true) //mystock.Poll(-1,SelectMode.SelectRead)
{
try
{
mystock = listener.AcceptSocket();
}catch(Exception ee)
{
MessageBox.Show(ee.Message);
}
if (mystock.Connected)
{
Thread thread = new Thread(new ThreadStart(round));
thread.Start();
}
}
}
private void round()
{
while (true)
{
if (mystock.Connected)
{
netstream = new NetworkStream(mystock);
}
byte[] messagebyte = new byte[1024];
//接收数据
try
{
//具体读和写的部分,这部分应该没问题.就没上代码
}
catch (Exception ee)
{
Debug.Print(ee.Message);
if (ee.Message == "无法从传输连接中读取数据: 远程主机强迫关闭了一个现有的连接。。")
{
Debug.Print("22");
//netstream.Close();
//mystock.Close();
//listener.Stop();
//server
//button1_Click(object sender, EventArgs e);
}
if (ee.Message == "未将对象引用设置到对象的实例。")
{
//round();
Debug.Print("33");
//目前客户端直接关闭后就在这里死循环了.....嘿嘿... }
}
//MessageBox.Show(ee.ToString()); //Thread thread = new Thread(new ThreadStart(recive));
//thread.Start();
//mystock.Close();
//break;
//mystock.Shutdown(SocketShutdown.Both);
}
}=======================================================================================================
客户端部分
//这个是启动的一个部分
try
{
myclient = new TcpClient();
try
{
myclient.Connect(myip, myport);
Thread thread = new Thread(new ThreadStart(sendandrecive));
thread.Start(); }catch(SocketException exp)
{
this.Close();
}
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}//调用的读写部分
private void sendandrecive()
{
//收发信息.都无问题....
}
服务器端和客户端都是自己写..
然后服务器端运行.客户端第一次可以正常连接,然后点击客户端关闭,(相当于非法关闭程序)
然后服务器段就开始死循环...
提示:"未将对象引用设置到对象的实例。"
麻烦各位大大帮忙看看哪里逻辑架构有问题..或者有什么地方不对的..
服务器端代码private void button1_Click(object sender, EventArgs e)
{
listener = new TcpListener(myip, myport);
listener.Start();
Thread thread = new Thread(new ThreadStart(recive));
thread.Start();
}
private void recive()
{
while (true) //mystock.Poll(-1,SelectMode.SelectRead)
{
try
{
mystock = listener.AcceptSocket();
}catch(Exception ee)
{
MessageBox.Show(ee.Message);
}
if (mystock.Connected)
{
Thread thread = new Thread(new ThreadStart(round));
thread.Start();
}
}
}
private void round()
{
while (true)
{
if (mystock.Connected)
{
netstream = new NetworkStream(mystock);
}
byte[] messagebyte = new byte[1024];
//接收数据
try
{
//具体读和写的部分,这部分应该没问题.就没上代码
}
catch (Exception ee)
{
Debug.Print(ee.Message);
if (ee.Message == "无法从传输连接中读取数据: 远程主机强迫关闭了一个现有的连接。。")
{
Debug.Print("22");
//netstream.Close();
//mystock.Close();
//listener.Stop();
//server
//button1_Click(object sender, EventArgs e);
}
if (ee.Message == "未将对象引用设置到对象的实例。")
{
//round();
Debug.Print("33");
//目前客户端直接关闭后就在这里死循环了.....嘿嘿... }
}
//MessageBox.Show(ee.ToString()); //Thread thread = new Thread(new ThreadStart(recive));
//thread.Start();
//mystock.Close();
//break;
//mystock.Shutdown(SocketShutdown.Both);
}
}=======================================================================================================
客户端部分
//这个是启动的一个部分
try
{
myclient = new TcpClient();
try
{
myclient.Connect(myip, myport);
Thread thread = new Thread(new ThreadStart(sendandrecive));
thread.Start(); }catch(SocketException exp)
{
this.Close();
}
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}//调用的读写部分
private void sendandrecive()
{
//收发信息.都无问题....
}
int i = 0; if ((i = netstream.Read(messagebyte, 0, messagebyte.Length)) != 0)
{//服务器端收发部分}
else
{
//netstream.Close();
Debug.Print("44");
break;
}
现在不会死循环了.在用户第一次登陆又非法关闭后.可是当同一ip用户再次登陆时候.显示...
在 System.NullReferenceException 中第一次偶然出现的“update_server.exe”类型的异常
未将对象引用设置到对象的实例。是round()中第一个catch到的...我感觉就是第一次连接断开后.服务器端我没做什么处理..貌似有问题,是不是?
且不说你提的问题。
从你的代码来看,实在太糟糕了,不出现死循环才怪。你的代码里竟然没有出现break while的代码,你的第一个线程是一个死循环,只要接收到数据,就会起一个round的线程,而这个线程也是一个死循环(从你的注释的代码,虽然break,但又回去调用receive,但receive却是个死循环),这样下去,旧的线程没有释放,又不断new出新的线程,你的程序不crash才怪。