数据持久化的模块你可以看看java如何写入mysql数据库,通常是使用jdbc连接数据库,开始事务,然后执行insert into的sql语句,然后提交事务即可。网上例子很多,你可以参考。
#region TCP协议public void BindTcp()
{
try
{
//执行tcp 通讯
textBox2.ScrollBars = ScrollBars.Vertical//设置滚动条
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)//创建一个新的tcp实例
client.Connect(IPAddress.Parse("192.168.1.1"), 4545)//设置ip,端口
//执行线程
th = new Thread(xintiao)
th.Start()
th = new Thread(start1)
th.Start()
}
catch (Exception)
{
MessageBox.Show("执行tcp协议异常")
throw
}
}
// 接收数据+显示
public void start1()
{
try
{
while (true)
{
string s = Receive(client, 10000 * 2)
string rec = "Receive:" + s + "\r\n"
if (s.Substring(0, 4) == "0231")
{
Start(s)
}
textBox2.Text += rec
}
}
catch (Exception)
{
MessageBox.Show("显示数据异常")
}
}
//心跳
public void xintiao()
{
try
{
while (true)
{
Thread.Sleep(5000)
Send("00209999001000000000")
}
}
catch (Exception)
{
MessageBox.Show("发送心跳异常")
throw
}
}
//发送
private static Encoding encode = Encoding.Default
public void Send(string data)
{
try
{
client.Send(encode.GetBytes(data))
textBox2.Text += "\r\n" + "Send:" + data + "\r\n"
}
catch (Exception)
{
MessageBox.Show("发送异常")
}
}
//接收
private string Receive(Socket socket, int timeout)
{
string result = string.Empty
socket.ReceiveTimeout = timeout
List<byte>data = new List<byte>()
byte[] buffer = new byte[1024]
int length = 0
try
{
while ((length = socket.Receive(buffer)) >0)
{
for (int j = 0j <lengthj++)
{
data.Add(buffer[j])
}
if (length <buffer.Length)
{
break
}
}
}
catch
{
textBox2.Text += "超时,断开链接"
}
if (data.Count >0)
{
result = encode.GetString(data.ToArray(), 0, data.Count)
}
return result
}
#endregion
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)