找传奇、传世资源到传世资源站!

C# 打印机监控工具源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboard

from clipboardusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using System.Net;using System.Net.Sockets;using System.Threading;using System.Collections;namespace Monitor_printer_server{ public partial class Form1 : Form { private IPAddress hostIPAddress ; private IPEndPoint Server; private Socket listeningSocket;//侦听 //private Socket mySocket; private const int BufferSize = 1024 * 64;//缓冲大小 private byte[] buffer = new byte[BufferSize];//系统缓冲 string port;//端品名 private string adress = Application.StartupPath;//系统开启时的目录 public struct ser { public IntPtr id; public FileStream fstr; public string FileName; } public ser[] _sessionTable = new ser[100]; public Form1() { InitializeComponent(); } /// <summary> /// Form1_Load功能:开启程序时读取的信息; /// </summary> /// <param name="sender">系统自带</param> /// <param name="e">系统自带</param> private void Form1_Load(object sender, EventArgs e) { //获取主机的IP地址; IPHostEntry myHost = new IPHostEntry(); myHost = Dns.GetHostByName(Dns.GetHostName()); txtServer.Text = myHost.AddressList[0].ToString(); //判断IP.INI文件是否存在.如果存在启动侦听 if (File.Exists(adress "\\ip.ini") == true) { StreamReader srd = File.OpenText(adress "\\ip.ini"); txtServer.Text = srd.ReadLine(); txtPort.Text = srd.ReadLine(); srd.Close(); button1_Click(sender, e); button2_Click_1(sender, e); } } /// <summary> /// button1_Click功能:开始侦听程序 /// </summary> /// <param name="sender">系统自带</param> /// <param name="e">系统自带</param> private void button1_Click(object sender, EventArgs e) { button1.Enabled = false; try { StreamWriter srd; srd = File.CreateText("ip.ini"); srd.WriteLine(txtServer.Text); srd.WriteLine(txtPort.Text); srd.Close(); } catch (Exception ee) { MessageBox.Show(ee.Message); } try { hostIPAddress = IPAddress.Parse(txtServer.Text); port = txtPort.Text; } catch { MessageBox.Show("The IP address was not in a correct format."); } Server = new IPEndPoint(hostIPAddress, Int32.Parse(port)); listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listeningSocket.Bind(Server); //绑定该主机端口 listeningSocket.Listen(50); //监听端口,等待客户端连接请求。50是队列中最多可容纳的等待接受的传入连接数 listeningSocket.BeginAccept(new AsyncCallback(AcceptCallBack), listeningSocket);//开始一个异步操作来接受一个传入的连接尝试 //listBox1.Items.Insert(0, "服务器开始侦听"); button1.Enabled = false; 启动RToolStripMenuItem.Enabled = false; } /// <summary> /// AcceptCallBack功能开始一个异步连接 /// </summary> /// <param name="ar">表示异步操作的状态</param> private void AcceptCallBack(IAsyncResult ar)//ar表示异步操作的状态。 { try { Socket mySocket = (Socket)ar.AsyncState; //获取状态,在这里AsyncState返回的是BeginAccept方法的handler参数 Socket handler = mySocket.EndAccept(ar);//异步接受传入的连接尝试,并创建新的 Socket 来处理远程主机通信,获取结果 string year = DateTime.Now.Year.ToString(); string month = DateTime.Now.Month.ToString(); string day = DateTime.Now.Day.ToString(); string hour = DateTime.Now.Hour.ToString(); string min = DateTime.Now.Minute.ToString(); string sec = DateTime.Now.Second.ToString(); string ssec = DateTime.Now.Millisecond.ToString(); string FileName =adress "\\pic\\" year month day hour min sec ssec ".emf"; FileStream fstr = new FileStream(FileName, FileMode.Create, FileAccess.Write); Add(handler.Handle, fstr, FileName); handler.BeginReceive(buffer, 0, BufferSize, 0, new AsyncCallback(ReadCallBack), handler); listeningSocket.BeginAccept(new AsyncCallback(AcceptCallBack), listeningSocket);//开始一个异步操作来接受一个传入的连接尝试 } catch (Exception ee) { //MessageBox.Show("服务器信息AcceptCallBack" ee.Message); } } /// <summary> /// FindSession功能查找一个对应的流; /// </summary> /// <param name="session">一个新建连接的句柄</param> /// <returns>返回对应在缓存中的位置</returns> private int FindSession(IntPtr session) { for (int i = 0; i < 100; i ) { if (_sessionTable[i].id == session) { return i; } } return 100; } /// <summary> /// Add功能新建一个与客户端的连接 /// </summary> /// <param name="key">新建的句柄</param> /// <param name="fstr">对应的流文件</param> /// <param name="FileName">对应的文件名</param> /// <returns></returns> private int Add(IntPtr key, FileStream fstr, string FileName) { for (int i = 0; i < 100; i ) { if (_sessionTable[i].id == IntPtr.Zero) { _sessionTable[i].id = key; _sessionTable[i].fstr = fstr; _sessionTable[i].FileName = FileName; return i; } } return 100; } /// <summary> /// ReadCallBack功能程序异步挂起,将客户端传过来的数据保存_sessionTable[key].fstr流中 /// </summary> /// <param name="ar">表示异步操作的状态</param> private void ReadCallBack(IAsyncResult ar) { int bytesRead=0,key; Socket handler = (Socket)ar.AsyncState; key=FindSession(handler.Handle); try { bytesRead = handler.EndReceive(ar); //结束挂起的异步读取,返回接收到的字节数。 if (bytesRead == 0) { /*if (_sessionTable[key].fstr.Length == 0) { _sessionTable[key].fstr.Close(); _sessionTable[key].id = IntPtr.Zero; FileInfo a = new FileInfo(_sessionTable[key].FileName); _sessionTable[key].fstr.Close(); a.Delete(); return; }*/ _sessionTable[key].fstr.Close(); _sessionTable[key].id = IntPtr.Zero; return; } else { _sessionTable[key].fstr.Write(buffer, 0, bytesRead); } handler.BeginReceive(buffer, 0, BufferSize, 0, new AsyncCallback(ReadCallBack), handler); } catch (Exception eee) { MessageBox.Show("服务器信息:" eee.Message); _sessionTable[key].id = IntPtr.Zero; FileInfo a = new FileInfo(_sessionTable[key].FileName); _sessionTable[key].fstr.Close(); a.Delete(); } } /// <summary> /// Form1_FormClosing功能将程序关闭 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_FormClosing(object sender, FormClosingEventArgs e) { this.Dispose(); } /// <summary> /// button6_Click功能关闭侦听 /// </summary> /// <param name="sender">系统自带</param> /// <param name="e">系统自带</param> private void button6_Click(object sender, EventArgs e) { for (int i = 0; i < 100; i ) { // _sessionTable[i]. } listeningSocket.Close(); button1.Enabled = true; 启动RToolStripMenuItem.Enabled = true; // this.Dispose(); } /// <summary> /// 显示SToolStripMenuItem_Click功能将程序隐藏掉 /// </summary> /// <param name="sender">系统自带</param> /// <param name="e">系统自带</param> private void 显示SToolStripMenuItem_Click(object sender, EventArgs e) { this.Show(); this.WindowState = FormWindowState.Normal; } /// <summary> /// button2_Click_1功能将程序隐藏掉 /// </summary> /// <param name="sender">系统自带</param> /// <param name="e">系统自带</param> private void button2_Click_1(object sender, EventArgs e) { this.Hide(); this.WindowState = FormWindowState.Minimized; } private void 退出EToolStripMenuItem_Click(object sender, EventArgs e) { this.Dispose(); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复