这个是写的通信的,执行过程中到第317条记录的时候就提示内存不足了,
除了下面我用到的方法,还要怎么释放内存啊?
private void button4_Click(object sender, EventArgs e)
{
string ip = "192.168.1.5";
string port = "8686";
IPAddress serverIp = IPAddress.Parse(ip);
int serverPort = Convert.ToInt32(port);
IPEndPoint iep = new IPEndPoint(serverIp, serverPort);
SqlCeConnection conn = new SqlCeConnection(@"Data Source = canyin111.sdf");
conn.Open();
SqlCeCommand cmd = new SqlCeCommand("delete from beizhu",conn);
cmd.ExecuteNonQuery();
conn.Close();
cmd.Dispose();
conn.Dispose();
n = 0;
beizhushu = 0;
shuaxinshuzu();
sendMessage[0] = 0x1d;
sendMessage[1] = 0x1b;
sendMessage[2] = 0x00;
sendMessage[3] = 0x38;
sendMessage[4] = 0x00;
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(iep);
socket.Send(sendMessage, 5, 0);
socket.Receive(byteMessage);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
if (byteMessage[1] == 0x02)
{
beizhushu = Convert.ToInt32(byteMessage[3] * 256 + byteMessage[4]);
if(beizhushu>0)
{
shuaxinshuzu();
sendMessage[0] = 0x1f;
sendMessage[1] = 0x02;
sendMessage[2] = 0x1b;
sendMessage[3] = 0x00;
sendMessage[4] = 0x3c;
sendMessage[5] = 0x00;
Socket socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket1.Connect(iep);
socket1.Send(sendMessage, 6, 0);
socket1.Receive(byteMessage);
socket1.Shutdown(SocketShutdown.Both);
socket1.Close();
beizhugengxin();
}
else
{
MessageBox.Show("下载完毕!");
}
}
GC.Collect();
}
private void beizhugengxin()
{
int k = 1;
int changdu = 0;
while (byteMessage[k] != 0x1b)
{
changdu++;
k++;
}
string bianhao = Encoding.Default.GetString(byteMessage, k - changdu, changdu);
k++;
changdu = 0;
while (byteMessage[k] != 0x1b)
{
k++;
changdu++;
}
string beizhu = Encoding.Default.GetString(byteMessage, k - changdu, changdu);
try
{
SqlCeConnection conn = new SqlCeConnection(@"Data Source = canyin111.sdf");
conn.Open();
SqlCeCommand cmd = new SqlCeCommand("insert into beizhu values('" + bianhao + "','" + beizhu + "')", conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Dispose();
conn.Close();
n++;
if (n < beizhushu)
{
//if(n%100==0)
//{
// MessageBox.Show(n.ToString());
//}
shuaxinshuzu();
//循环上传结果
sendMessage[0] = 0x21;
sendMessage[1] = 0x02;
sendMessage[2] = 0x1b;
sendMessage[3] = Convert.ToByte(n / 256);
sendMessage[4] = Convert.ToByte(n % 256);
sendMessage[5] = 0x1b;
ushort jiaoyan = 0;
for (int ii = 0; ii < 6; ii++)
{
jiaoyan += sendMessage[ii];
}
sendMessage[6] = Convert.ToByte(jiaoyan / 256);
sendMessage[7] = Convert.ToByte(jiaoyan % 256);
sendMessage[8] = 0x00;
string ip = "192.168.1.5";
string port = "8686";
IPAddress serverIp = IPAddress.Parse(ip);
int serverPort = Convert.ToInt32(port);
IPEndPoint iep = new IPEndPoint(serverIp, serverPort);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(iep);
socket.Send(sendMessage, 9, 0);
socket.Receive(byteMessage);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
GC.Collect();
beizhugengxin();
}
else if (n == beizhushu)
{
MessageBox.Show("下载完毕!");
}
}
catch
{
//MessageBox.Show(ee.Message);
SqlCeConnection conn = new SqlCeConnection(@"Data Source = canyin111.sdf");
conn.Open();
SqlCeCommand cmd = new SqlCeCommand("select count(*) from beizhu", conn);
SqlCeDataReader dr= cmd.ExecuteReader();
string aaa=dr[0].ToString();
MessageBox.Show(aaa);
}
}