You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
4.0 KiB
119 lines
4.0 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Net.Sockets;
|
|
using System.Configuration;
|
|
using System.Net;
|
|
using QMAPP.FJC.BLL.Operation;
|
|
|
|
namespace AirbagSupportPackageService
|
|
{
|
|
public class RecieveData
|
|
{
|
|
TcpListener tcpsever = null;
|
|
|
|
public event AirbagSupportPackageService.LeafEvent.DataReceivedHandler DataReceived;
|
|
|
|
/// <summary>
|
|
/// 开启TCP监听
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public void StartTCPServer()
|
|
{
|
|
try
|
|
{
|
|
string serverIP = ConfigurationManager.AppSettings["ServerIP"];
|
|
string port = ConfigurationManager.AppSettings["Port"];
|
|
tcpsever = new TcpListener(IPAddress.Parse(serverIP), Convert.ToInt32(port));
|
|
tcpsever.Start();
|
|
tcpsever.BeginAcceptTcpClient(new AsyncCallback(Acceptor), tcpsever);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WriteLog.Write(ex.Message);
|
|
}
|
|
}
|
|
|
|
public void StopTCPServer()
|
|
{
|
|
tcpsever.Stop();
|
|
}
|
|
|
|
|
|
private void Acceptor(IAsyncResult o)
|
|
{
|
|
TcpListener server = o.AsyncState as TcpListener;
|
|
try
|
|
{
|
|
//初始化连接的客户端
|
|
LeafTCPClient newClient = new LeafTCPClient();
|
|
newClient.NetWork = server.EndAcceptTcpClient(o);
|
|
//string barcode = newClient.Name;
|
|
newClient.NetWork.GetStream().BeginRead(newClient.buffer, 0, newClient.buffer.Length, new AsyncCallback(TCPCallBack), newClient);
|
|
server.BeginAcceptTcpClient(new AsyncCallback(Acceptor), server);//继续监听客户端连接
|
|
}
|
|
catch (ObjectDisposedException ex)
|
|
{ //监听被关闭
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WriteLog.Write(ex.Message);
|
|
//MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void TCPCallBack(IAsyncResult ar)
|
|
{
|
|
try
|
|
{
|
|
|
|
LeafTCPClient client = (LeafTCPClient)ar.AsyncState;
|
|
if (client.NetWork.Connected)
|
|
{
|
|
|
|
NetworkStream ns = client.NetWork.GetStream();
|
|
byte[] recdata = new byte[ns.EndRead(ar)];
|
|
string barCode = new ASCIIEncoding().GetString(recdata);
|
|
|
|
if (recdata.Length > 0)
|
|
{
|
|
Array.Copy(client.buffer, recdata, recdata.Length);
|
|
|
|
barCode = new ASCIIEncoding().GetString(recdata);
|
|
barCode = barCode.Replace(" ", "").ToString();
|
|
|
|
try
|
|
{
|
|
WriteLog.Write(System.DateTime.Now.ToString() + ":" + barCode);
|
|
if (string.IsNullOrEmpty(barCode) == false && barCode.Length > 5)
|
|
{
|
|
ProductBLL productbll = new ProductBLL();
|
|
productbll.InsertAirbagSupportPackage(barCode);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WriteLog.Write(ex.Message);
|
|
}
|
|
|
|
if (DataReceived != null)
|
|
{
|
|
DataReceived.BeginInvoke(client.Name, recdata, null, null);//异步输出数据
|
|
}
|
|
ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client);
|
|
}
|
|
else
|
|
{
|
|
client.DisConnect();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WriteLog.Write(ex.Message);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|