C# TCP通讯客户端源码


                                            C# TCP通讯客户端源码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Management;

namespace TCPIP接口读卡器C井2010例子代码
{
public partial class Form8 : Form
{
private TcpClient tcpClient = null;
private IPAddress ipaddress;
int remortprot;
public Boolean connok;
private Thread thread;

Form1 f1;

public Form8()
{
InitializeComponent();

}

private void Form8_Load(object sender, EventArgs e)
{
f1 = (Form1)this.Owner;
f1.Refresh();

string connip = f1.textBox2.Text + "." + f1.textBox3.Text + "." + f1.textBox4.Text + "." + f1.textBox5.Text;
ipaddress = IPAddress.Parse(connip);
remortprot = Convert.ToInt32(f1.textBox8.Text);
}


private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "建立与读写器的TCP连接")
{
try
{
tcpClient = new TcpClient();
tcpClient.Connect(ipaddress, remortprot); //阻塞式同步通讯工作方式
if (tcpClient.Connected)
{
connok = true;
button1.Text = "断开与读写器的TCP连接";
panel1.Enabled = true;

thread = new Thread(new ThreadStart(this.ReceiveHandle));
thread.Start();
}
}
catch
{
MessageBox.Show("TCP连接失败,请确定MODBUS TCP读写器已经开启!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
connok = false;
button1.Text = "建立与读写器的TCP连接";
panel1.Enabled = false;
tcpClient.Close();
}
}

private void ReceiveHandle()
{
string recestr;
int i;
try
{
while (connok)
{
byte[] bytes = new byte[100];
NetworkStream stream;
stream = tcpClient.GetStream(); //获取网络流
lock (stream) ; //为了保证数据的完整性以及安全性 锁定数据流
int bytesRead = stream.Read(bytes, 0, bytes.Length); //读取返回信息

recestr = "FromIP:" + (ipaddress + ":" + remortprot.ToString().Trim() + " ").Substring(0, 22) + "电脑接收:";
for (i = 0; i < bytesRead; i++)
{
recestr = recestr + bytes[i].ToString("X2") + " ";
}
f1.listBox1.Items.Add(recestr);
f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;

if (bytes[0] == 0x55 && bytes.Length>11)
{
switch (bytes[1])
{
case 0x05: ;
DialogResult dr = MessageBox.Show("读写器已执行一次读卡操作,是否要读出寄存器内数!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
beginbuf.Value = 7;
button4_Click(button4, EventArgs.Empty);
}
break;
case 0x06: ;
recestr = "";
for (i = 9; i <= 9 + bytes[8];i++ )
{
recestr = recestr + bytes[i].ToString("X2") + " ";
}
textBox1.Text = recestr;

break;
case 0x08:;
if (beginbuf.Value >= 10)
{
DialogResult dr1 = MessageBox.Show(" 存放IC卡扇区数据的寄存器数据更改成功,是否要驱动读卡器执行写卡操作?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr1 == DialogResult.OK)
{
beginbuf.Value = 7;
button5_Click(button5, EventArgs.Empty);
}
}
break;
}

}
}
}
catch
{

}
}
private void button32_Click(object sender, EventArgs e)
{
byte[] sendbuf = new byte[12];
sendbuf[0] = 0x55;
sendbuf[1] = 0x01;
sendbuf[2] = 0x00; //MODBUS TCP协议标识
sendbuf[3] = 0x00;
sendbuf[4] = 0x00;
sendbuf[5] = 0x06; //指令长度
sendbuf[6] = 0x00;
sendbuf[7] = 0x06; //功能码
sendbuf[8] = 0x00;
sendbuf[9] = 0x02; //寄存器地址
sendbuf[10] = 0x00;
sendbuf[11] = (byte)(comboBox7.SelectedIndex + 1);

try
{
NetworkStream stream;
stream = tcpClient.GetStream(); //获取网络流
//lock (stream) ; //为了保证数据的完整性以及安全性 锁定数据流
stream.Write(sendbuf, 0, sendbuf.Length);//将数据写入网络流
string recestr = "";
recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString() .Trim() + " ").Substring(0, 22) + "电脑发送:";
for (int i = 0; i < 12; i++)
{
recestr = recestr + sendbuf[i].ToString("X2") + " ";
}
f1.listBox1.Items.Add(recestr);
f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
}
catch
{
connok = false;
tcpClient.Close();
button1.Text = "建立与读写器的TCP连接";
panel1.Enabled = false;
}
}

private void Form8_Load()
{
f1 = (Form1)this.Owner;
f1.Refresh();

string connip = f1.textBox2.Text + "." + f1.textBox3.Text + "." + f1.textBox4.Text + "." + f1.textBox5.Text;
ipaddress = IPAddress.Parse(connip);
remortprot = Convert.ToInt32(f1.textBox8.Text);
}

private void button2_Click(object sender, EventArgs e)
{
byte[] sendbuf = new byte[12];
sendbuf[0] = 0x55;
sendbuf[1] = 0x02;
sendbuf[2] = 0x00; //MODBUS TCP协议标识
sendbuf[3] = 0x00;
sendbuf[4] = 0x00;
sendbuf[5] = 0x06; //指令长度
sendbuf[6] = 0x00;
sendbuf[7] = 0x06; //功能码
sendbuf[8] = 0x00;
sendbuf[9] = (byte)(0x46 + comboBox8.SelectedIndex);

sendbuf[10] = (byte)(swithdely.Value / 256); //延时
sendbuf[11] = (byte)(swithdely.Value % 256);

try
{
NetworkStream stream;
stream = tcpClient.GetStream(); //获取网络流
lock (stream) ; //为了保证数据的完整性以及安全性 锁定数据流
stream.Write(sendbuf, 0, sendbuf.Length);//将数据写入网络流
string recestr = "";
recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + " ").Substring(0, 22) + "电脑发送:";
for (int i = 0; i < 12; i++)
{
recestr = recestr + sendbuf[i].ToString("X2") + " ";
}
f1.listBox1.Items.Add(recestr);
f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
}
catch
{
connok = false;
tcpClient.Close();
button1.Text = "建立与读写器的TCP连接";
panel1.Enabled = false;
}

}

private void button34_Click(object sender, EventArgs e)
{
byte[] sendbuf = new byte[12];
sendbuf[0] = 0x55;
sendbuf[1] = 0x03;
sendbuf[2] = 0x00; //MODBUS TCP协议标识
sendbuf[3] = 0x00;
sendbuf[4] = 0x00;
sendbuf[5] = 0x06; //指令长度
sendbuf[6] = 0x00;
sendbuf[7] = 0x06; //功能码
sendbuf[8] = 0x00;
sendbuf[9] = (byte)(0x46 + comboBox8.SelectedIndex);

sendbuf[10] = 0x00; //延时
sendbuf[11] = 0x00;

try
{
NetworkStream stream;
stream = tcpClient.GetStream(); //获取网络流
lock (stream) ; //为了保证数据的完整性以及安全性 锁定数据流
stream.Write(sendbuf, 0, sendbuf.Length); //将数据写入网络流
string recestr = "";
recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + " ").Substring(0, 22) + "电脑发送:";
for (int i = 0; i < 12; i++)
{
recestr = recestr + sendbuf[i].ToString("X2") + " ";
}
f1.listBox1.Items.Add(recestr);
f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
}
catch
{
connok = false;
tcpClient.Close();
button1.Text = "建立与读写器的TCP连接";
panel1.Enabled = false;
}
}

private void button35_Click(object sender, EventArgs e)
{
byte[] sendbuf = new byte[12];
sendbuf[0] = 0x55;
sendbuf[1] = 0x04;
sendbuf[2] = 0x00; //MODBUS TCP协议标识
sendbuf[3] = 0x00;
sendbuf[4] = 0x00;
sendbuf[5] = 0x06; //指令长度
sendbuf[6] = 0x00;
sendbuf[7] = 0x06; //功能码
sendbuf[8] = 0x00;
sendbuf[9] = 0x01;

if(checkBox1.Checked )
{
sendbuf[10] = 0x80;
sendbuf[11] = (byte)(comboBox9.SelectedIndex + 2 * comboBox10.SelectedIndex);
}
else
{
sendbuf[10] = 0x00;
sendbuf[11] = (byte)(comboBox9.SelectedIndex + 2 * comboBox10.SelectedIndex);
}


try
{
NetworkStream stream;
stream = tcpClient.GetStream(); //获取网络流
lock (stream) ; //为了保证数据的完整性以及安全性 锁定数据流
stream.Write(sendbuf, 0, sendbuf.Length); //将数据写入网络流
string recestr = "";
recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + " ").Substring(0, 22) + "电脑发送:";
for (int i = 0; i < 12; i++)
{
recestr = recestr + sendbuf[i].ToString("X2") + " ";
}
f1.listBox1.Items.Add(recestr);
f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
}
catch
{
connok = false;
tcpClient.Close();
button1.Text = "建立与读写器的TCP连接";
panel1.Enabled = false;
}
}

private void button3_Click(object sender, EventArgs e)
{
byte[] sendbuf = new byte[12];
sendbuf[0] = 0x55;
sendbuf[1] = 0x05;
sendbuf[2] = 0x00; //MODBUS TCP协议标识
sendbuf[3] = 0x00;
sendbuf[4] = 0x00;
sendbuf[5] = 0x06; //指令长度
sendbuf[6] = 0x00;
sendbuf[7] = 0x06; //功能码
sendbuf[8] = 0x00;
sendbuf[9] = 0x07;
sendbuf[10] = 0x00;
sendbuf[11] = 0x08;

try
{
NetworkStream stream;
stream = tcpClient.GetStream(); //获取网络流
lock (stream) ; //为了保证数据的完整性以及安全性 锁定数据流
stream.Write(sendbuf, 0, sendbuf.Length);//将数据写入网络流
string recestr = "";
recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + " ").Substring(0, 22) + "电脑发送:";
for (int i = 0; i < 12; i++)
{
recestr = recestr + sendbuf[i].ToString("X2") + " ";
}
f1.listBox1.Items.Add(recestr);
f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
}
catch
{
connok = false;
tcpClient.Close();
button1.Text = "建立与读写器的TCP连接";
panel1.Enabled = false;
}
}

private void button4_Click(object sender, EventArgs e)
{
byte[] sendbuf = new byte[12];
sendbuf[0] = 0x55;
sendbuf[1] = 0x06;
sendbuf[2] = 0x00; //MODBUS TCP协议标识
sendbuf[3] = 0x00;
sendbuf[4] = 0x00;
sendbuf[5] = 0x06; //指令长度
sendbuf[6] = 0x00;
sendbuf[7] = 0x03; //功能码
sendbuf[8] = 0x00;
sendbuf[9] = (byte)beginbuf.Value ;
sendbuf[10] = 0x00;
sendbuf[11] = (byte)bufnum.Value ;

try
{
NetworkStream stream;
stream = tcpClient.GetStream(); //获取网络流
lock (stream) ; //为了保证数据的完整性以及安全性 锁定数据流
stream.Write(sendbuf, 0, sendbuf.Length);//将数据写入网络流
string recestr = "";
recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + " ").Substring(0, 22) + "电脑发送:";
for (int i = 0; i < 12; i++)
{
recestr = recestr + sendbuf[i].ToString("X2") + " ";
}
f1.listBox1.Items.Add(recestr);
f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
}
catch
{
connok = false;
tcpClient.Close();
button1.Text = "建立与读写器的TCP连接";
panel1.Enabled = false;
}
}

private void button7_Click(object sender, EventArgs e)
{
byte wrib1;
byte wrib2;
string wreitstr = textBox1.Text.Trim();

try
{
wrib1 = Convert.ToByte(Convert.ToInt32(wreitstr.Substring(0, 2), 16));
wrib2 = Convert.ToByte(Convert.ToInt32(wreitstr.Substring(3, 2), 16));
}
catch (Exception ex)
{
MessageBox.Show("请在右边栏中输入要写入的16进制数据!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

byte[] sendbuf = new byte[12];
sendbuf[0] = 0x55;
sendbuf[1] = 0x07;
sendbuf[2] = 0x00; //MODBUS TCP协议标识
sendbuf[3] = 0x00;
sendbuf[4] = 0x00;
sendbuf[5] = 0x06; //指令长度
sendbuf[6] = 0x00;
sendbuf[7] = 0x06; //功能码
sendbuf[8] = 0x00;
sendbuf[9] = (byte)beginbuf.Value;
sendbuf[10] = wrib1;
sendbuf[11] = wrib2;

try
{
NetworkStream stream;
stream = tcpClient.GetStream(); //获取网络流
lock (stream) ; //为了保证数据的完整性以及安全性 锁定数据流
stream.Write(sendbuf, 0, sendbuf.Length);//将数据写入网络流
string recestr = "";
recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + " ").Substring(0, 22) + "电脑发送:";
for (int i = 0; i < 12; i++)
{
recestr = recestr + sendbuf[i].ToString("X2") + " ";
}
f1.listBox1.Items.Add(recestr);
f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
}
catch
{
connok = false;
tcpClient.Close();
button1.Text = "建立与读写器的TCP连接";
panel1.Enabled = false;
}
}

private void button6_Click(object sender, EventArgs e)
{
byte[] Writbuf = new byte[48];
int WritNum =(int)(bufnum.Value * 2);
int i;

string wreitstr = textBox1.Text.Trim();

try
{
for (i = 0; i < WritNum;i++ )
{
Writbuf[i] = Convert.ToByte(Convert.ToInt32(wreitstr.Substring(i*3, 2), 16));
}
}
catch (Exception ex)
{
MessageBox.Show("请在右边栏中输入要写入的16进制数据,每个寄存器要写入2个字节数据!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

byte[] sendbuf = new byte[WritNum+13];
sendbuf[0] = 0x55;
sendbuf[1] = 0x08;
sendbuf[2] = 0x00; //MODBUS TCP协议标识
sendbuf[3] = 0x00;
sendbuf[4] = 0x00;
sendbuf[5] = 0x06; //指令长度
sendbuf[6] = 0x00;
sendbuf[7] = 0x10; //功能码
sendbuf[8] = 0x00;
sendbuf[9] = (byte)beginbuf.Value;
sendbuf[10] = 0x00;
sendbuf[11] = (byte)bufnum.Value;
sendbuf[12] = (byte)WritNum;

for (i = 1; i <= WritNum; i++)
{
sendbuf[12 + i] = Writbuf[i - 1];
}

try
{
NetworkStream stream;
stream = tcpClient.GetStream(); //获取网络流
lock (stream) ; //为了保证数据的完整性以及安全性 锁定数据流
stream.Write(sendbuf, 0, sendbuf.Length);//将数据写入网络流
string recestr = "";
recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + " ").Substring(0, 22) + "电脑发送:";
for (i = 0; i <= 12 + WritNum; i++)
{
recestr = recestr + sendbuf[i].ToString("X2") + " ";
}
f1.listBox1.Items.Add(recestr);
f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
}
catch
{
connok = false;
tcpClient.Close();
button1.Text = "建立与读写器的TCP连接";
panel1.Enabled = false;
}
}

private void button5_Click(object sender, EventArgs e)
{
byte[] sendbuf = new byte[12];
sendbuf[0] = 0x55;
sendbuf[1] = 0x09;
sendbuf[2] = 0x00; //MODBUS TCP协议标识
sendbuf[3] = 0x00;
sendbuf[4] = 0x00;
sendbuf[5] = 0x06; //指令长度
sendbuf[6] = 0x00;
sendbuf[7] = 0x06; //功能码
sendbuf[8] = 0x00;
sendbuf[9] = 0x07;
sendbuf[10] = 0x00;
sendbuf[11] = 0x04;

try
{
NetworkStream stream;
stream = tcpClient.GetStream(); //获取网络流
lock (stream) ; //为了保证数据的完整性以及安全性 锁定数据流
stream.Write(sendbuf, 0, sendbuf.Length);//将数据写入网络流
string recestr = "";
recestr = "SendTo:" + (ipaddress + ":" + remortprot.ToString().Trim() + " ").Substring(0, 22) + "电脑发送:";
for (int i = 0; i < 12; i++)
{
recestr = recestr + sendbuf[i].ToString("X2") + " ";
}
f1.listBox1.Items.Add(recestr);
f1.listBox1.SelectedIndex = f1.listBox1.Items.Count - 1;
}
catch
{
connok = false;
tcpClient.Close();
button1.Text = "建立与读写器的TCP连接";
panel1.Enabled = false;
}
}
}
}