Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 空引用异常问题_C#_.net_Tcp - Fatal编程技术网

C# 空引用异常问题

C# 空引用异常问题,c#,.net,tcp,C#,.net,Tcp,在下面的代码中,我得到了空引用异常,我不明白为什么会得到它。请帮我解决它 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Windows.Forms; Net系统; 使用System.Net.Sockets; 使用系统线程; 使用System.Data.SqlClient; 使用系统配置; 命名空间TCPListener { 公共部分类Form1:Form {

在下面的代码中,我得到了空引用异常,我不明白为什么会得到它。请帮我解决它

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
Net系统;
使用System.Net.Sockets;
使用系统线程;
使用System.Data.SqlClient;
使用系统配置;
命名空间TCPListener
{
公共部分类Form1:Form
{
//声明我们的工作线程
私有线程workerThread=null;
公共表格1()
{
初始化组件();
//初始化并启动辅助线程
this.workerThread=new Thread(new ThreadStart(this.ReceiveTcpData));
this.workerThread.Start();
}
public void timer1_Tick(对象发送方,事件参数e)
{
}
公共TcpListener服务器=null;
public Int32 port=Convert.ToInt32(ConfigurationManager.AppSettings[“PUERTO”].ToString());
公共IPAddress localAddr=IPAddress.Parse(ConfigurationManager.AppSettings[“IP”].ToString());
public void OpenSocket()
{
尝试
{
//TcpListener服务器=新的TcpListener(端口);
服务器=新的TcpListener(localAddr,端口);
//开始侦听客户端请求。
server.Start();
}
捕获(SocketException e)
{
Common.CommonControls.writeToLogFile(“套接字错误:+e.Message”);
}
最后
{
Common.CommonControls.writeToLogFile(“INICIO DE ESCUCHA EN”+DateTime.Now);
}
}
私有void ReceiveTcpData()
{
//奥布杰托斯酒店
Entities.Program oPosiciones=新Entities.Program();
DataAccess.Program oPosicionesDA=新的DataAccess.Program();
尝试
{
//用于读取数据的缓冲区
字节[]字节=新字节[256];
字符串数据=null;
//进入监听循环。
//执行阻塞调用以接受请求。
//您还可以在此处使用server.AcceptSocket()。
//TcpClient client=server.AcceptTcpClient();
TcpClient cliente=新的TcpClient();
尝试
{
cliente=server.AcceptTcpClient();
}
catch(异常e){MessageBox.Show(e.ToString());}
数据=空;
//获取用于读写的流对象
NetworkStream=cliente.GetStream();
int i;
//循环以接收客户端发送的所有数据。
而((i=stream.Read(bytes,0,bytes.Length))!=0)
{
//将数据字节转换为ASCII字符串。
数据=System.Text.Encoding.ASCII.GetString(字节,0,i);
//处理客户端发送的数据。
data=data.ToUpper();
if(data.Substring(0,2)==“###”)
{
//SalidaMonitor(“Paquete recibido:LOGON REQUEST del Equipmo”);
cliente.Close();
this.workerThread.Interrupt();
返回;
}
byte[]msg=System.Text.Encoding.ASCII.GetBytes(数据);
//在监视器上显示数据
SalidaMonitor(“Paquete recibido”+日期时间.现在+”:“+数据);
//申报实体
oPosiciones.Paquete=数据;
//数据库操作
冬小麦属植物(冬小麦属);
//发回回复。
//stream.Write(msg,0,msg.Length);
//SalidaMonitor(“Paquete enviado:+msg”);
}
//停机和端部连接
cliente.Close();
}
捕获(SocketException e)
{
Common.CommonControls.writeToLogFile(“套接字错误:+e.Message”);
}
捕获(SqlException x)
{
Common.CommonControls.writeToLogFile(“SQL错误:+x.Message”);
}
捕获(异常y)
{
Common.CommonControls.writeToLogFile(“错误:+y.Message”);
}
最后
{
oposicationes=null;
oPosicionesDA=null;
this.workerThread.Interrupt();
}
}
专用void saldamonitor(字符串数据)
{
Invoke(新的MethodInvoker(委托{lstMensajes.Items.Add(data.ToString();}));
Invoke(新方法调用程序(委托{lstMensajes.SelectedIndex=lstMensajes.Items.Count-1;lstMensajes.SelectedIndex=-1;}));
}
私有void Form1\u加载(对象发送方、事件参数e)
{
OpenSocket();
}
私有void Form1\u Close(对象发送方,FormClosingEventArgs e)
{
server.Stop();
}
}
}

在上面的代码中,我在cliente=server.AcceptTcpClient()处遇到错误。我不明白为什么会这样。如果你需要任何信息,请告诉我。谢谢

好吧,如果它在那一行,那是因为
服务器
没有初始化。

问题

在窗体的构造函数中创建并启动新线程。 该线程将调用
ReceiveTcpData
方法,该方法使用
server
变量(此时该变量尚未初始化)为什么

因为
服务器
public Form1()
{
     InitializeComponent();

     // add this line.
     OpenSocket();

     // Initialise and start worker thread
     this.workerThread = new Thread(new ThreadStart(this.ReceiveTcpData));
     this.workerThread.Start();
}
public Form1()
{
     InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
     // add this line.
     OpenSocket();

     // Initialise and start worker thread
     this.workerThread = new Thread(new ThreadStart(this.ReceiveTcpData));
     this.workerThread.Start();
}