網上支付-用於與銀行SOCKET通信的程序

/* written by Jaron(賈俊) ,2003-11-04 */
/* 原出處:B/S WEB技術中文網 http://www.jaron.cn ;*/
/* 歡迎訪問我的網站: http://www.jaron.cn  http://blog.jaron.cn*/
/* 如對本文有疑問,請在網站留言 /*
/* 轉載請註明出處和保留此版權信息 */
/* 歡迎使用SiteManager網站管理系統 http://sitemanager.cnzone.net http://demo.jaron.cn ; */

// class1.cs

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace yzstudysrv
{
?///
?/// Class1 的摘要說明。
?///
?class Class1
?{
??///
??/// 應用程序的主入口點。
??///
??///

??private const int portnum=11000;
??????? private const string socketserver_address="192.168.1.223";
??[STAThread]
??static void Main(string[] args)
??{
???//
???//
???// TODO: 在此處添加代碼以啓動應用程序
???bool done=false;
???TcpListener listener=new TcpListener(IPAddress.Parse(socketserver_address),portnum);
???
???//TcpListener listener=new TcpListener(portnum);
???string sendbuff="999000";
???sqlaccess cszj;
???byte[] sbuff;
???while(!done)
???{
????listener.Start();
????Console.WriteLine("waiting");
????//TcpClient client=listener.AcceptTcpClient();
????TcpClient client=listener.AcceptTcpClient();
????Console.WriteLine("connected");
????NetworkStream ns=client.GetStream();
????byte[] sbytes=new byte[client.ReceiveBufferSize];
????ns.Read(sbytes,0,(int)client.ReceiveBufferSize);
????string rec=Encoding.Unicode.GetString(sbytes);
???????????????? Console.WriteLine(rec);
????if(Convert.ToInt32(rec.Substring(0,3))!=rec.Length)
?????sbuff=Encoding.Unicode.GetBytes("888111");//包長不對
????
????else
????{
???
????
?????cszj=new sqlaccess();
?????if(rec.Substring(3,3)=="001")//查詢
?????{
??????cszj.getquer(rec,sendbuff);
??????sbuff=Encoding.Unicode.GetBytes(sendbuff);
?????}

???
?????else
??????if(rec.Substring(3,3)=="002")//交費
?????{
??????cszj.getjfpack(rec,sendbuff);
??????sbuff=Encoding.Unicode.GetBytes(sendbuff);
?????}//hc
?????else
??????if(rec.Substring(3,3)=="003")//沖帳
?????{
??????cszj.getczpack(rec,sendbuff);
?????
??????sbuff=Encoding.Unicode.GetBytes(sendbuff);
?????}
?????else
??????sbuff=Encoding.Unicode.GetBytes("999000");//出錯
????}
????try
????{
?????ns.Write(sbuff,0,sbuff.Length);
?????ns.Close();
?????client.Close();
????}
????catch (Exception er)
????{

?????Console.WriteLine(er.ToString());
????}


???????????
????listener.Stop();
????//
???}
??}
?}
}

//sqlaccess.cs

using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data.SqlClient;
//using System.Data.
//using System.Data.SqlClient.SqlTransaction;

namespace yzstudysrv
{
?///
?/// sqlaccess 的摘要說明。
?///
?public class sqlaccess : System.ComponentModel.Component
?{
??private System.Data.SqlClient.SqlConnection sqlConnection1;
??private System.Data.SqlClient.SqlCommand sqlCommand1;
??private System.Data.SqlClient.SqlDataReader myreader;
??
??///
??/// 必需的設計器變量。
??///
??private System.ComponentModel.Container components = null;

??
??public sqlaccess(System.ComponentModel.IContainer container)
??{
???///
???/// Windows.Forms 類撰寫設計器支持所必需的
???///
???container.Add(this);
???InitializeComponent();

???//
???// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
???//
??}

??public sqlaccess()
??{
???///
???/// Windows.Forms 類撰寫設計器支持所必需的
???///
???InitializeComponent();

???//
???// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
???//
??}

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


??#region 組件設計器生成的代碼
??///
??/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
??/// 此方法的內容。
??///
??private void InitializeComponent()
??{
???this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
???this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
???//
???// sqlConnection1
???//
???this.sqlConnection1.ConnectionString = "workstation id=/"BOCYZ-LYQ/";packet size=4096;user id=sa;data source=DB;persist sec" +
????"urity info=False;initial catalog=YCEDU";
???this.sqlConnection1.InfoMessage += new System.Data.SqlClient.SqlInfoMessageEventHandler(this.sqlConnection1_InfoMessage);
???//
???// sqlCommand1
???//
???this.sqlCommand1.Connection = this.sqlConnection1;

??}
??#endregion
??//取查詢信息
??public void getquer(string recbuff, string sndbuff)
??{
???string billid=recbuff.Substring(61,16);
???string ordertotal,flag,name;
???this.sqlConnection1.Open();//打開連接
??????
???SqlTransaction myTrans;
???// Start a local transaction
???myTrans =sqlConnection1.BeginTransaction(IsolationLevel.ReadCommitted,"sqlTransaction");
???// Must assign both transaction object and connection
???// to Command object for a pending local transaction
???sqlcommand1.Connection = sqlConnection1;
???sqlCommand1.Transaction = myTrans;
???try
???{
????this.sqlCommand1.CommandText="select name, OrderTotal,DeductResaultSymbol from tblOrderExpenses where Billid='"+billid+"'";
????myreader=this.sqlCommand1.ExecuteReader();
????myTrans.Commit();
???}?
???catch(Exception e)
???{
????try
????{
?????myTrans.Rollback("sqlTransaction");
????}
????catch (SqlException ex)
????{
?????if (myTrans.Connection != null)
?????{
??????Console.WriteLine("An exception of type " + ex.GetType() +
???????" was encountered while attempting to roll back the transaction.");
?????}
????}
???
????Console.WriteLine("An exception of type " + e.GetType() +
?????" was encountered while inserting the data.");
????Console.WriteLine("Neither record was written to database.");
???}
???finally
???{
????sqlConnection1.Close();
???}
??}

//??if(myreader.Read())
//???{
//????name=myreader["name"].ToString();
//????ordertotal=myreader["ordertotal"].ToString();
//????flag=myreader["DeductResaultSymbol"].ToString();
//????sndbuff=recbuff.Substring(0,61)+strpack(name,10)+strpack(ordertotal,7)+flag;
//???}
//???else
//?????? sndbuff="999000";
//????myreader.Close();
//????this.sqlConnection1.Close();
//??
??//democode
??public void RunSqlTransaction(string myConnString)
??{
???SqlConnection myConnection = new SqlConnection(myConnString);
???myConnection.Open();//打開連接

???SqlCommand myCommand = myConnection.CreateCommand();
???SqlTransaction myTrans;

???// Start a local transaction
???myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted,"SampleTransaction");
???// Must assign both transaction object and connection
???// to Command object for a pending local transaction
???myCommand.Connection = myConnection;
???myCommand.Transaction = myTrans;

???try
???{
????myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
????myCommand.ExecuteNonQuery();
????myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
????myCommand.ExecuteNonQuery();
????myTrans.Commit();
????Console.WriteLine("Both records are written to database.");
???}
???catch(Exception e)
???{
????try
????{
?????myTrans.Rollback("SampleTransaction");
????}
????catch (SqlException ex)
????{
?????if (myTrans.Connection != null)
?????{
??????Console.WriteLine("An exception of type " + ex.GetType() +
???????" was encountered while attempting to roll back the transaction.");
?????}
????}
???
????Console.WriteLine("An exception of type " + e.GetType() +
?????" was encountered while inserting the data.");
????Console.WriteLine("Neither record was written to database.");
???}
???finally
???{
????myConnection.Close();
???}
??}

??????? //沖帳
??public void getczpack(string recbuff, string sndbuff)
??{
???string billid=recbuff.Substring(61,16);
???string bankwater=recbuff.Substring(77,16);
???
???this.sqlConnection1.Open();
???this.sqlCommand1.CommandText ="update tblOrderExpenses set DeductResaultSymbol='2' where Billid='"+billid+"'and DeductResaultSymbol='2'"
????+" and bankwater='"+bankwater+"'";
???int k=0;
???k=this.sqlCommand1.ExecuteNonQuery();
???if(k==1)
??
????sndbuff=recbuff.Substring(0,61)+"0";
???
???else
????sndbuff="999000";
???
???this.sqlConnection1.Close();
??
??}
??//交費
??public void getjfpack(string recbuff, string sndbuff)
??{
???string billid=recbuff.Substring(61,16);
???string accno=recbuff.Substring(77,19);
???string amt=recbuff.Substring(96,7);
???string cbflag=recbuff.Substring(103,1);
???string bankid=recbuff.Substring(10,3);
???string sbranchno=recbuff.Substring(13,10);
???string DTdeduct=DateTime.Now.ToString("yyyyMMdd");
???string AreaId="0514";
???string bankwater=recbuff.Substring(45,16);
???int k=0;
???
???this.sqlConnection1.Open();
???this.sqlCommand1.CommandText ="update tblOrderExpenses set BankId='"+bankid+"', ServiceStationId='"
????+sbranchno+"',DTdeduct='"+DTdeduct+"',AreaID='"+AreaId+"',DeductResaultSymbol='1',BargainerSymbol='"
????+cbflag+"',bankwater='"+bankwater+"' where Billid='"+billid+"' and OrderTotal='"+amt+"' and DeductResaultSymbol = '0'";
???k=this.sqlCommand1.ExecuteNonQuery();
???if(k==1)
????? sndbuff=recbuff.Substring(0,61)+"0";
???else
????sndbuff="999000";
???
???this.sqlConnection1.Close();
??
??}
??//字符串補位函數
?
??public string strpack(string tmp,int length)
??{
??????????????????????????????????????????
???string atmp;
???int k=0;
???int t=0;
???for(int i=0;i???{??
????if(tmp[i]>0x255)
????{
?????k++;
?????t++;
????}
????t++;
????if(t>length)
?????break;
???}
???int alen=tmp.Length+k;
???if(alen??????????????????
???{
????atmp=new String(' ',length-alen);
??
????return tmp+atmp;
??
???????
???}
???else
???
????return tmp.Substring(0,length-k);
??????????
?????
??

??}

??private void sqlConnection1_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
??{
??
??}
??


?}
}
(本文是直接從VS.net貼出的,自行刪除前面的??)

發佈了50 篇原創文章 · 獲贊 0 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章