C#与打印机、扫描枪、指示灯通讯TCP/IP通讯Server端和Client端
namespace WindowsFormsApplication1
{
class TcpIpServer
{
public EndPoint RemoteEndPoint { get; private set; } //当前客户端的网络结点
Thread threadwatch = null;//负责监听客户端的线程 Socket socket = null;//负责监听客户端的套接字
// Dictionary<ip和端口, Socket> 定义一个集合,存储客户端信息
public Dictionary<EndPoint, Socket> dic = new Dictionary<EndPoint, Socket> { };
private StringBuilder msg = new StringBuilder();
public string Msg
{
get { return msg.ToString(); }
private set
{
msg.AppendLine(value);
//MessageBox.Show(value + "
");
}
}
private TcpIpServer() { }
public TcpIpServer(int port = 7891)
{
//定义一个套接字用于监听客户端发来的消息,包含三个参数(IP4寻址协议,流式连接,Tcp协议)
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//将IP地址和端口号绑定到网络节点point上
IPEndPoint point = new IPEndPoint(IPAddress.Any, port);//设置服务器端口,IP是本程序所在PC的内网IP
//监听绑定的网络节点
socket.Bind(point);
//将套接字的监听队列长度限制为20
socket.Listen(20);
//创建一个监听线程
threadwatch = new Thread(watchconnecting);
//将窗体线程设置为与后台同步,随着主线程结束而结束
threadwatch.IsBackground = true;
//启动线程
threadwatch.Start();
//启动线程后显示相应提示
Msg = ("开始监听客户端传来的信息!" + "
");
}
//监听客户端发来的请求
private void watchconnecting()
{
Socket connection = null;
while (true) //持续不断监听客户端发来的请求
{
try
{
connection = socket.Accept();
}
catch (Exception ex)
{
Msg = (ex.Message); //提示套接字监听异常
break;
}
//让客户显示"连接成功的"的信息
string sendmsg = "连接服务端成功!你的IP是" + connection.RemoteEndPoint;
byte[] arrSendMsg = Encoding.UTF8.GetBytes(sendmsg);
connection.Send(arrSendMsg);
RemoteEndPoint = connection.RemoteEndPoint; //客户端网络结点号
Msg = ("成功与" + RemoteEndPoint + "客户端建立连接!
"); //显示与客户端连接情况
dic.Add(RemoteEndPoint, connection); //添加客户端信息
//创建一个通信线程
ParameterizedThreadStart pts = new ParameterizedThreadStart(recv);
Thread thread = new Thread(pts);
thread.IsBackground = true;//设置为后台线程,随着主线程退出而退出
thread.Start(connection);//启动线程
}
}
private string strSRecMsg = "";
///
/// 接收客户端发来的信息
///
///客户端套接字对象
private void recv(object socketclientpara)
{
Socket socketServer = socketclientpara as Socket;
while (true)
{
//创建一个内存缓冲区 其大小为1024*1024字节 即1M
byte[] arrServerRecMsg = new byte[1024 * 1024];
//将接收到的信息存入到内存缓冲区,并返回其字节数组的长度
try
{
int length = socketServer.Receive(arrServerRecMsg);
//将机器接受到的字节数组转换为人可以读懂的字符串
strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length);
//将发送的字符串信息附加到文本框txtMsg上
Msg = ("客户端:" + GetCurrentTime() + socketServer.RemoteEndPoint + "的消息:" + strSRecMsg + "
");
MessageBox.Show("客户端消息:"+strSRecMsg);
SentMsg(socketServer.RemoteEndPoint,strSRecMsg+"LLL");
}
catch (Exception ex)
{
Msg = ("客户端:" + GetCurrentTime() + socketServer.RemoteEndPoint + "已经中断连接" + "
"); //提示套接字监听异常
//listBoxOnlineList.Items.Remove(socketServer.RemoteEndPoint.ToString());//从listbox中移除断开连接的客户端
socketServer.Close();//关闭之前accept出来的和客户端进行通信的套接字
//return null;
break;
}
}
}
//获取当前系统时间
private string GetCurrentTime()
{
string timeStr = System.DateTime.Now.ToString("yyyy年MM月dd日hh时mm分ss秒fff毫秒。");
return timeStr;
}
/// <summary>
/// 发送信息到客户端
/// </summary>
/// <param name="smallname"></param>
/// <param name="sendMsg">要发送的信息</param>
public void SentMsg(EndPoint endPoint, string sendMsg)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sendMsg); //将要发送的信息转化为字节数组,因为Socket发送数据时是以字节的形式发送的
dic[endPoint].Send(bytes); //发送数据
Msg = (GetCurrentTime() + endPoint + "的消息:" + sendMsg + "
");
}
}
}
``
二、客户端类
int port = 20108;
`
private void IniClient(string type)
{
string result = type;
if (string.IsNullOrWhiteSpace(type)) return;
#region 1.指示灯系统 try
{
string ip = InitClass.ReadString("zhishideng", "address", "");
if (InitClass.ReadString("zhishideng", "port", "") != "")
{
port = Convert.ToInt32(InitClass.ReadString("zhishideng", "port", ""));
}
//1,创建socket
Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//2,发起建立连接的请求
IPAddress ipaddress = IPAddress.Parse(ip); //可以把一个字符串的ip地址转化成一个ipaddress的对象
EndPoint point = new IPEndPoint(ipaddress, port);
tcpClient.Connect(point); //通过ip:端口号 定位一个要连接到的服务器端
global_info.connect_tcp = true;
if (global_info.connect_tcp)
{
label_status.Invoke(new Action(() => { label_status.Text = "与指示灯系统连接成功"; }));
//向服务器端发送消息
string message2 = result+""; //读取用户的输入 把获取的瓶子信息以L00格式发送到服务器端
tcpClient.Send(Encoding.UTF8.GetBytes(message2)); //把字符串转化成字节数组,然后发送到服务器端
//接收服务端消息
byte[] data = new byte[1024];
int length = tcpClient.Receive(data); //这里传递一个byte数组,实际上这个data数组用来接收数据
//length返回值表示接收了多少字节的数据
string strReader = Encoding.UTF8.GetString(data, 0, length); //只把接收到的数据做一个转化
if (strReader.Substring(3,1)=="1")
{
label_status.Invoke(new Action(() => { label_status.Text = strReader; }));
// MessageBox.Show("返回消息:" + strReader);
////出库
//Agv_chuku();
}
else
{
string reader1 = result.Substring(0, 3) + "0";
MessageBox.Show("来自指示灯系统的消息:" + reader1);
}
}
}
catch (Exception)
{
MessageBox.Show("连接控制台指示灯系统失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
label_status.BeginInvoke(new Action(() => { label_status.Text = "连接失败"; }));
return;
}
#endregion
}`
三、注意事项
Timer线程内修改主线程需要用到 委托,这种写法是委托的一种。
label_status.BeginInvoke(new Action(() => { label_status.Text = "连接失败"; }));
以上是 C#与打印机、扫描枪、指示灯通讯TCP/IP通讯Server端和Client端 的全部内容, 来源链接: utcz.com/z/515138.html