public Int32 Receive(byte[] bData, Int32 iDataLen) { Int32 iLen = 0; int iErrCode = Constant.ERR_OK; try { lock (ns) { if (ns.CanRead) { iLen = ns.Read(bData, 0, iDataLen); } else { iErrCode = Constant.ERR_OBJECT_DISPOSED_EXCEPTION; } } }catch (System.IO.IOException ioe) { CLog.SaveException("---Tcp Receive---", ioe.Message); iErrCode = Constant.ERR_IO_EXCEPTION; } catch (ObjectDisposedException ode) { CLog.SaveException("---Tcp Receive---", ode.Message); iErrCode = Constant.ERR_OBJECT_DISPOSED_EXCEPTION; } catch (Exception e) { CLog.SaveException("---Tcp Receive---", e.Message); iErrCode = Constant.ERR_EXCEPTION; } return(iLen); }
public Int32 Send(byte[] bData) { int iErrCode = Constant.ERR_OK; try { lock (ns) { if (ns.CanWrite) { ns.Write(bData, 0, bData.Length); } else { iErrCode = Constant.ERR_OBJECT_DISPOSED_EXCEPTION; } } } catch (System.IO.IOException ioe) { CLog.SaveException("---Tcp Send---", ioe.Message); iErrCode = Constant.ERR_IO_EXCEPTION; } catch (ObjectDisposedException ode) { CLog.SaveException("---Tcp Send---", ode.Message); iErrCode = Constant.ERR_OBJECT_DISPOSED_EXCEPTION; } catch (Exception e) { CLog.SaveException("---Tcp Send---", e.Message); iErrCode = Constant.ERR_EXCEPTION; } return(iErrCode); }
private Int32 Send(string sIP, int iPort) { IPEndPoint ipepnew = new IPEndPoint(IPAddress.Parse(sIP), iPort); Int32 irlt = 0; try { lock (UdpSkt) { irlt = UdpSkt.SendTo(strScan, 14, SocketFlags.None, ipepnew);//将数据发送到指定的终结点 } } catch (Exception e) { CLog.SaveException("---UDP Send---", e.Message); } if (irlt > 0) { return(Constant.ERR_OK); } else { return(Constant.ERR_ERROR); } }
public void listensocket() { while (true) // 持续不断的监听客户端的连接请求; { // 开始监听客户端连接请求,Accept方法会阻断当前的线程; Socket sokConnection; //IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(m_sServerIp), ServerPort); //sokConnection.Bind(endpoint); //sokConnection.Listen(10); try { sokConnection = socketWatch.Accept(); } catch (SocketException se) { CLog.SaveLog("客户端关闭!"); CLog.SaveException("---Listen---", se.Message); CLog.SaveLog("-----------------------------"); return; } //Socket sokConnection = socketWatch.Accept(); // 一旦监听到一个客户端的请求,就返回一个与该客户端通信的 套接字; // 将与客户端连接的 套接字 对象添加到集合中; dictSkt.Add(sokConnection.RemoteEndPoint.ToString(), sokConnection); CLog.SaveLog("客户端连接成功![" + sokConnection.RemoteEndPoint.ToString() + "]"); Thread thr = new Thread(RecMsg); thr.IsBackground = true; thr.Start(sokConnection); } }
static void Main() { bool ret; System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret); if (ret) { try { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmMain()); } catch (Exception e) { CLog.SaveException("---Program Exception---", e.Message); } } else { MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning); Application.Exit();//退出程序 } }
void RecMsg(object sokConnectionparn) { Socket sokClient = sokConnectionparn as Socket; while (true) { // 定义一个2M的缓存区; byte[] arrMsgRec = new byte[1024 * 1024 * 2]; // 将接受到的数据存入到输入 arrMsgRec中; int length = -1; try { length = sokClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度; } catch (SocketException se) { CLog.SaveLog("异常(Receive Message):" + se.Message); CLog.SaveException("---Receive Message---", se.Message); // 从 通信套接字 集合中删除被中断连接的通信套接字; dictSkt.Remove(sokClient.RemoteEndPoint.ToString()); break; } catch (Exception e) { CLog.SaveLog("异常(Receive Message):" + e.Message); CLog.SaveException("---Receive Message---", e.Message); // 从 通信套接字 集合中删除被中断连接的通信套接字; dictSkt.Remove(sokClient.RemoteEndPoint.ToString()); break; } string strMsg = System.Text.Encoding.ASCII.GetString(arrMsgRec, 0, length); try { DealCMD(strMsg, sokClient); } catch (SocketException se) { CLog.SaveLog("异常(DealCMD):" + se.Message); CLog.SaveException("---DealCMD---", se.Message); } catch (Exception e) { CLog.SaveLog("异常(DealCMD):" + e.Message); CLog.SaveException("---DealCMD---", e.Message); } } }
private void btn_RefreshMsgNames_Click(object sender, EventArgs e) { CLog.SaveLog("Line[" + LineName + "] Refresh Message list."); mainForm.startWait("Refresh message list"); Application.DoEvents(); try { //StringBuilder sbNames = new StringBuilder(255 * 100); //GetMsgNameList(ipPrinter, sbNames); string sNames = m_Printer.GetMsgNameList(); String strNames = sNames;// sbNames.ToString(); String[] strName = null; //for (int i = 0; i < count; i++) //{ strName = strNames.Split(new Char[] { ',' }); cbobox_MsgList.Items.Clear(); int index = 0; while (true) { if (strName[index].Length > 0) { cbobox_MsgList.Items.Add(strName[index]); } else { break; } index++; } } catch (Exception ex) { CLog.SaveException("---Get MessageList---", ex.Message); //iErrCode = Constant.ERR_OBJECT_DISPOSED_EXCEPTION; } if (cbobox_MsgList.Items.Count > 0) { cbobox_MsgList.SelectedIndex = 0; } mainForm.StopWait(); }
public Int32 Disconnect() { int iErrCode = Constant.ERR_OK; try { if (tc != null) { tc.Close(); } tc = null; } catch (Exception e) { CLog.SaveException("---Tcp DisConnect---", e.Message); iErrCode = Constant.ERR_EXCEPTION; } return(iErrCode); }
public int SendStrings(string strStrings) { CLog.SaveLog("Line[" + LineName + "] Send Strings[" + strStrings + "]"); //byte[] btsendStrings = System.Text.Encoding.ASCII.GetBytes(strStrings); int irlt = Constant.ERR_ERROR; try { irlt = m_Printer.SendDynamicString(strStrings); if (irlt == Constant.ERR_OK) { DispMsg(strStrings);//rTxtbox_Send.AppendText(strStrings);// .Text = rTxtbox_Send.Text. Insert(strStrings); } }catch (Exception ex) { CLog.SaveException("---Send Dynamic String Exception---", ex.Message); irlt = Constant.ERR_SEND_STRINGS; } return(irlt); }
private void recv() { IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)sender; byte[] data = new byte[1024]; int iRecv = 0; try { UdpSkt.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 300); iRecv = UdpSkt.ReceiveFrom(data, ref Remote); } catch (Exception e) { CLog.SaveException("---UDP Recv---", e.Message); } if (iRecv > 0) { return; } }
//private bool bFind; public CUDP(string sLocalIP, Int32 iPort = 19000) { //ipLocalep = new IPEndPoint(IPAddress.Parse(sLocalIP), iPort); try { UdpSkt = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); UdpSkt.Bind(new IPEndPoint(IPAddress.Parse(sLocalIP), iPort));//绑定本地IP端口 UdpSkt.ReceiveTimeout = 500; uint IOC_IN = 0x80000000; uint IOC_VENDOR = 0x18000000; uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12; UdpSkt.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null); } catch (SocketException se) { CLog.SaveException("---UDP Init---", se.Message); } catch (Exception e) { CLog.SaveException("---UDP Init---", e.Message); } //setsockopt(m_sockClient, SOL_SOCKET, SO_RCVTIMEO, (char*)&iNetTimeout, sizeof(int)); }
public Int32 Connect(string sRemoteIP, Int32 iPrinterSN, Int32 iRemotePort = 17888) { int iErrCode = 0; iSN = iPrinterSN; if (tc == null) { tc = new TcpClient(); } byte[] inValue = new byte[] { 1, 0, 0, 0, 0x88, 0x13, 0, 0, 0xE8, 0x03, 0, 0 };// 首次探测时间5 秒, 间隔侦测时间1 秒 tc.Client.IOControl(IOControlCode.KeepAliveValues, inValue, null); try { tc.Connect(sRemoteIP, iRemotePort); ns = tc.GetStream(); } catch (SocketException se) { CLog.SaveException("---Tcp Connect---", se.Message); iErrCode = Constant.ERR_SOCKET_EXCEPTION; } catch (Exception e) { CLog.SaveException("---Tcp Connect---", e.Message); iErrCode = Constant.ERR_EXCEPTION; } if (tc.Connected) { return(Constant.ERR_OK); } else { return(Constant.ERR_ERROR); } }
void ReciveMsg() { receiveEnd = false; while (true) { EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号 byte[] buffer = new byte[1024]; UdpSkt.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 13000); int length = 0; try { lock (UdpSkt) { length = UdpSkt.ReceiveFrom(buffer, ref point);//接收数据报 } } catch (SocketException e) { //超时 CLog.SaveException("---UDP Receive---", e.Message); break; } if (length > 0) { Int32 isn = buffer[4] + buffer[5] * 256 + buffer[6] * 256 * 256 + buffer[7] * 256 * 256 * 256; string IPPort = (point as IPEndPoint).ToString(); string[] strArray = IPPort.Split(new Char[] { ':' }); string strIP = strArray[0];//.Substring(1, strArray[0].Length - 2); lstFind.Add(strIP + "-" + isn.ToString()); //break; } } receiveEnd = true; }