字号:    

unix-C#中间件的通信

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

namespace Server
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  //_______________功能服务____________________________
  private string clientCode;
  private string servCmd;
  private string phoneN;
  private string money;
  private string wasteN;
  private string state;
  private string account;
  private string sex;
  private string region;
  //__________________功能服务__________________________

  private Thread thread;
  private System.Text.UTF8Encoding encode;
  private System.Text.ASCIIEncoding encodeA;

  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.GroupBox groupBox2;
  private System.Windows.Forms.Button buttonStart;
  private System.Windows.Forms.Button buttonStop;
  private System.Windows.Forms.TextBox textBoxIP;
  private System.Windows.Forms.TextBox textBoxPort;
  private Socket socket;
  //  private Socket clientSocket;
  private string port;
  private string localIP;
  //  private System.Net.Sockets.Socket socket;
  private System.Net.Sockets.TcpListener tcpListener;

  private Thread ServiceThread;
  private System.Windows.Forms.StatusBar statusBar1;
  private System.Windows.Forms.ListBox listBoxMessage;
  private System.Windows.Forms.TextBox textBox1;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
   encode=new UTF8Encoding();
   this.listBoxMessage.Items.Clear();
   
   
   this.statusBar1.Text="";
   this.buttonStop.Enabled=false;
   this.encodeA=new ASCIIEncoding();
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
   this.groupBox2 = new System.Windows.Forms.GroupBox();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.listBoxMessage = new System.Windows.Forms.ListBox();
   this.textBoxIP = new System.Windows.Forms.TextBox();
   this.label4 = new System.Windows.Forms.Label();
   this.label3 = new System.Windows.Forms.Label();
   this.textBoxPort = new System.Windows.Forms.TextBox();
   this.buttonStart = new System.Windows.Forms.Button();
   this.buttonStop = new System.Windows.Forms.Button();
   this.statusBar1 = new System.Windows.Forms.StatusBar();
   this.groupBox2.SuspendLayout();
   this.SuspendLayout();
   //
   // groupBox2
   //
   this.groupBox2.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("groupBox2.BackgroundImage")));
   this.groupBox2.Controls.Add(this.textBox1);
   this.groupBox2.Controls.Add(this.listBoxMessage);
   this.groupBox2.Controls.Add(this.textBoxIP);
   this.groupBox2.Controls.Add(this.label4);
   this.groupBox2.Controls.Add(this.label3);
   this.groupBox2.Controls.Add(this.textBoxPort);
   this.groupBox2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.groupBox2.ForeColor = System.Drawing.Color.Lime;
   this.groupBox2.Location = new System.Drawing.Point(16, 8);
   this.groupBox2.Name = "groupBox2";
   this.groupBox2.Size = new System.Drawing.Size(448, 304);
   this.groupBox2.TabIndex = 0;
   this.groupBox2.TabStop = false;
   this.groupBox2.Text = "欢迎使用";
   this.groupBox2.Enter += new System.EventHandler(this.groupBox1_Enter);
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(8, 208);
   this.textBox1.Multiline = true;
   this.textBox1.Name = "textBox1";
   this.textBox1.ReadOnly = true;
   this.textBox1.Size = new System.Drawing.Size(424, 40);
   this.textBox1.TabIndex = 14;
   this.textBox1.Text = "";
   //
   // listBoxMessage
   //
   this.listBoxMessage.ItemHeight = 14;
   this.listBoxMessage.Location = new System.Drawing.Point(8, 24);
   this.listBoxMessage.Name = "listBoxMessage";
   this.listBoxMessage.Size = new System.Drawing.Size(424, 172);
   this.listBoxMessage.TabIndex = 13;
   this.listBoxMessage.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBoxMessage_MouseDown);
   this.listBoxMessage.SelectedIndexChanged += new System.EventHandler(this.listBoxMessage_MouseDown);
   //
   // textBoxIP
   //
   this.textBoxIP.AutoSize = false;
   this.textBoxIP.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.textBoxIP.Location = new System.Drawing.Point(56, 248);
   this.textBoxIP.Name = "textBoxIP";
   this.textBoxIP.Size = new System.Drawing.Size(184, 32);
   this.textBoxIP.TabIndex = 9;
   this.textBoxIP.Text = "192.168.0.3";
   this.textBoxIP.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
   //
   // label4
   //
   this.label4.BackColor = System.Drawing.SystemColors.Control;
   this.label4.ForeColor = System.Drawing.Color.Blue;
   this.label4.Location = new System.Drawing.Point(256, 256);
   this.label4.Name = "label4";
   this.label4.Size = new System.Drawing.Size(72, 32);
   this.label4.TabIndex = 12;
   this.label4.Text = "监听接口";
   this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   //
   // label3
   //
   this.label3.BackColor = System.Drawing.SystemColors.Control;
   this.label3.ForeColor = System.Drawing.Color.Blue;
   this.label3.Location = new System.Drawing.Point(8, 248);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(48, 32);
   this.label3.TabIndex = 11;
   this.label3.Text = "本地IP";
   this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   //
   // textBoxPort
   //
   this.textBoxPort.AutoSize = false;
   this.textBoxPort.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.textBoxPort.Location = new System.Drawing.Point(336, 256);
   this.textBoxPort.Name = "textBoxPort";
   this.textBoxPort.Size = new System.Drawing.Size(104, 32);
   this.textBoxPort.TabIndex = 10;
   this.textBoxPort.Text = "5001";
   this.textBoxPort.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
   //
   // buttonStart
   //
   this.buttonStart.BackColor = System.Drawing.SystemColors.Control;
   this.buttonStart.Location = new System.Drawing.Point(496, 48);
   this.buttonStart.Name = "buttonStart";
   this.buttonStart.Size = new System.Drawing.Size(72, 32);
   this.buttonStart.TabIndex = 6;
   this.buttonStart.Text = "启动";
   this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click);
   //
   // buttonStop
   //
   this.buttonStop.BackColor = System.Drawing.SystemColors.Control;
   this.buttonStop.Location = new System.Drawing.Point(496, 104);
   this.buttonStop.Name = "buttonStop";
   this.buttonStop.Size = new System.Drawing.Size(72, 32);
   this.buttonStop.TabIndex = 8;
   this.buttonStop.Text = "停止";
   this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
   //
   // statusBar1
   //
   this.statusBar1.Location = new System.Drawing.Point(0, 366);
   this.statusBar1.Name = "statusBar1";
   this.statusBar1.Size = new System.Drawing.Size(584, 24);
   this.statusBar1.TabIndex = 13;
   this.statusBar1.Text = "statusBar1";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.BackColor = System.Drawing.SystemColors.ControlLight;
   this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
   this.ClientSize = new System.Drawing.Size(584, 390);
   this.Controls.Add(this.statusBar1);
   this.Controls.Add(this.groupBox2);
   this.Controls.Add(this.buttonStart);
   this.Controls.Add(this.buttonStop);
   this.Name = "Form1";
   this.Text = "手机缴费系统服务器";
   this.groupBox2.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());

   
  }

  private void groupBox1_Enter(object sender, System.EventArgs e)
  {
  
  }

  private void buttonStart_Click(object sender, System.EventArgs e)
  { 
   IPAddress ip=IPAddress.Parse(this.textBoxIP.Text);
   this.port=this.textBoxPort.Text;
   this.localIP=this.textBoxIP.Text;
   IPEndPoint server=new IPEndPoint(ip,Int32.Parse(port));
   this.tcpListener=new TcpListener(server);
   this.tcpListener.Start();
   this.listBoxMessage.Items.Add(DateTime.Now.ToString()+",服务器已经启动!正在监听:"+this.localIP+"\t端口:"+this.port);
   this.ServiceThread=new Thread(new ThreadStart(StartService));
   this.ServiceThread.Start();  
   
   ////   socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
   //   socket.Bind(server);
   //   socket.Listen(100);
   //   clientSocket=socket.Accept();
   //   this.listBoxState.Items.Add("与客户"+clientSocket.RemoteEndPoint.ToString()+"建立连接");
   //   thread=new Thread(new ThreadStart(AcceptMessage));
   //   thread.Start();
   this.buttonStart.Enabled=false; 
   this.buttonStop.Enabled=true;
   this.statusBar1.Text="服务器已启动";
  }
  private void StartService()
  {
   
   while(true)
   {
    this.socket=this.tcpListener.AcceptSocket();
    if(this.socket!=null)
    {
     //     this.listBoxMessage.Items.Add("接收到一个连接请求!");
    }
    thread=new Thread(new ThreadStart(Service));
    thread.Start();
   }
  }
  private void Service()
  {
   string strmessage="";
   string rms="";
   Socket tempSocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
   tempSocket=this.socket;
   NetworkStream netStream=new NetworkStream(tempSocket);
   try
   {
     
    byte[] datasize=new byte[4];
    netStream.Read(datasize,0,4);
    
    string strsize=encodeA.GetString(datasize);
    int size=int.Parse(strsize);
    Byte[] message=new byte[size];
    int dataleft=size;         
    int start=0;
    while(dataleft>0)
    {
     int recv=netStream.Read(message,start,dataleft);
     start+=recv;
     dataleft-=recv;
    }
    strmessage=encode.GetString(message);
    
    this.listBoxMessage.Items.Add(DateTime.Now.ToString()+",长度"+size.ToString()+"  ,"+strmessage);
   }
   catch
   {
    //     this.listBoxMessage.Items.Add("与客户断开连接");
    this.listBoxMessage.Items.Add(DateTime.Now.ToString()+",读取客户信息出错");
     
   }
   try
   {
    //处理收到的信息
    string[] m=strmessage.Split('~');
    if(m[1]=="c0"&&m[2]=="00")
    {
     rms=this.P_C0_00(m);
/*****************************************************************************/
     this.listBoxMessage.Items.Add(DateTime.Now.ToString()+" "+"手机自助缴费客户端:("+m[0]+")手机号:"+m[2]+"缴费金额"+m[3]);
    }
    if(m[1]=="c0"&&m[2]=="r0")
    {
     this.P_C0_R0(m);
     tempSocket.Close();
     return;

    }
    if(m[1]=="c0"&&m[2]=="s0")
    {
     rms=this.P_C0_S0(m);
    }
    if(m[1]=="c0"&&m[2]=="j0")
    {
     rms=this.P_C0_J0(m);
    }
    

   }
   catch
   {
    this.listBoxMessage.Items.Add(DateTime.Now.ToString()+",读取客户信息2出错");
   }


   try
   {

    //回复消息

    int i=encode.GetByteCount(rms);
    byte[] datasize=new byte[4];
    datasize=this.encodeA.GetBytes(i.ToString().PadLeft(4,'0'));
    byte[] sendbytes=encode.GetBytes(rms);
   
    //     NetworkStream netStream=new NetworkStream(socket);
    netStream.Write(datasize,0,4);
    netStream.Write(sendbytes,0,sendbytes.Length);
    netStream.Flush();    
       
   }
   catch
   {
    this.listBoxMessage.Items.Add(DateTime.Now.ToString()+",回复客户信息出错");
     
   }
   tempSocket.Close();
   return;
  }

  private void buttonStop_Click(object sender, System.EventArgs e)
  {
   
   this.buttonStop.Enabled=false;
   try
   {
    this.tcpListener.Stop();
    this.ServiceThread.Abort();
   
    this.listBoxMessage.Items.Add(DateTime.Now.ToString()+",服务器已经停止!");
    this.statusBar1.Text="服务器停止服务!";
    //    socket.Shutdown(SocketShutdown.Both);
    //    socket.Close();
    //    if(clientSocket.Connected)
    //    {
    //     clientSocket.Close();
    //     thread.Abort();
    //    }
   }
   catch
   {
    MessageBox.Show("关闭出错!");
   }
   this.buttonStart.Enabled=true;
  }
  private void Form1_Closing(object sender,System.ComponentModel.CancelEventArgs e)
  {
   try
   {
    this.tcpListener.Stop();
    this.ServiceThread.Abort();
    
   }
   catch
   {
    MessageBox.Show("关闭出错!");
   }
   this.Dispose();
   this.Close();
   Application.Exit();
   
  }

  private void listBoxMessage_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   try
   {
    this.textBox1.Text=this.listBoxMessage.SelectedItem.ToString();
   }
   catch
   {
   }
  }

  private void listBoxMessage_MouseDown(object sender, System.EventArgs e)
  {
  
  }

  private string P_C0_00(string [] m)
  {
   if(m.Length!=5)
   {
    MessageBox.Show("1接收的数据不正确!");
   }
   this.clientCode=m[1];
   this.servCmd=m[2];
   this.phoneN=m[3];
   this.money=m[4];
   DataClass dc=new DataClass();
   this.wasteN=dc.wasteN(this.phoneN);
   this.state=dc.state(this.phoneN);
   this.account=dc.account(this.phoneN);
   this.sex=dc.sex(this.phoneN);
   this.region=dc.region(this.phoneN);

   string rms="S~"+Config.ServCode()+"~"+Config.JF_0()+"~"+this.state+"~"+this.wasteN+"~"+this.account+"~"+this.sex+"~"+this.region;
   return rms;
  }
  private string P_C0_R0(string []m)
  {
   if(m.Length!=4)
   {
    MessageBox.Show("接收的数据2不正确!");
   }
   this.clientCode=m[1];
   this.servCmd=m[2];
   this.state=m[3];
   DataClass dc=new DataClass();
   dc.JaoFei_affirm(state);//判断是否本次交费成功
   

   string rms="S~"+Config.ServCode()+"~"+Config.JF_0()+"~"+this.state;
   return rms;

  }
  private string P_C0_S0(string []m)
  {
   if(m.Length!=4)
   {
    MessageBox.Show("接收的数据2不正确!");
   }
   this.clientCode=m[1];
   this.servCmd=m[2];
   this.phoneN=m[3];
   DataClass dc=new DataClass();
   string info=dc.search(this.phoneN);

   string rms="S~"+Config.ServCode()+"~"+Config.Search_0()+"~"+info;
   return rms;

  }
  private string P_C0_J0(string []m)
  {
   if(m.Length!=5)
   {
    MessageBox.Show("接收的数据2不正确!");
   }
   this.clientCode=m[1];
   this.servCmd=m[2];
   this.phoneN=m[3];
   this.money=m[4];
   DataClass dc=new DataClass();
   string info=dc.xianjianJF(this.phoneN,this.money);

   string rms="S~"+Config.ServCode()+"~"+Config.Search_0()+"~"+info;
   return rms;
  }

 }
}

 

分类:Unix
?次阅读
 2007-09-14 14:44