using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Net;
using
System.Net.Sockets;
using
System.Threading;
using
System.Windows.Forms;
using
System.IO;
using
System.Data.SqlClient;
namespace
SERVERsocket
{
class
SERVER
{
private
Thread serverThread;
private
Thread recvThread;
private
TcpListener tcpListener;
private
NetworkStream networkStream;
private
StreamReader streamReader;
private
StreamWriter streamWriter;
private
Socket socketForClient;
private
string
message;
private
int
operatorNO;
private
bool
[] isOperated;
private
void
Listen()
{
try
{
Int32 port = Int32.Parse(
"2020"
);
IPAddress ipAddress = Dns.Resolve(
"localhost"
).AddressList[0];
tcpListener =
new
TcpListener(IPAddress.Any, port);
//开始侦听
tcpListener.Start();
//返回可以用以处理连接的Socket实例
socketForClient = tcpListener.AcceptSocket();
if
(socketForClient.Connected)
{
networkStream =
new
NetworkStream(socketForClient);
streamReader =
new
StreamReader(networkStream);
streamWriter =
new
StreamWriter(networkStream);
recvThread =
new
Thread(
new
ThreadStart(RecvData));
recvThread.Start();
}
MessageBox.Show(
"客户端成功连接上服务器!"
);
}
catch
(Exception exc)
{
MessageBox.Show(exc.Message,
"Server提示"
);
}
}
//接收数据
private
void
RecvData()
{
string
msg = streamReader.ReadLine();
string
[] tokens = msg.Split(
new
char
[] {
'#'
});
//MessageBox.Show(tokens[0]);
//MessageBox.Show(tokens[1]);
//MessageBox.Show(tokens[2]);
while
(!msg.Equals(
"clientExit"
))
{
switch
(tokens[0])
{
case
"c01"
:
{
//MessageBox.Show("开始登陆!");
//下面写SQL语句,查询对应的帐户密码是否正确,并返回信息
SqlConnection thisConnection
=
new
SqlConnection(
"server = localhost;Integrated
Security =true;database = security"
);
SqlCommand comn
=
new
SqlCommand(
"select 密码 from 帐户表 where 账号="
+ tokens[1]
, thisConnection);
thisConnection.Open();
SqlDataAdapter thisAdapt
=
new
SqlDataAdapter(
"select * from 帐户表"
, thisConnection);
DataSet thisDataSet =
new
DataSet();
thisAdapt.Fill(thisDataSet,
"帐户表"
);
string
s = comn.ExecuteScalar().ToString();
// MessageBox.Show(s);
// MessageBox.Show(tokens[2]);
if
(s.CompareTo(tokens[2]) == 0)
{
streamWriter.Write(
"1"
);
streamWriter.Flush();
tokens[0].Remove(0);
// MessageBox.Show("登陆消息已发送");
}
else
{
streamWriter.Write(
"1"
);
streamWriter.Flush();
tokens[0].Remove(0);
// MessageBox.Show("登陆");
}
}
break
;
case
"c02"
:
{
//下面写SQL语句,查询对应账户的余额,并返回信息
SqlConnection thisConnection
=
new
SqlConnection(
"server = localhost;Integrated
Security =true;database = security;"
);
SqlDataAdapter thisAdapt
=
new
SqlDataAdapter(
"select * from 帐户表"
, thisConnection);
SqlCommandBuilder thisbuider
=
new
SqlCommandBuilder(thisAdapt);
SqlCommand thisCommand
= thisConnection.CreateCommand();
thisConnection.Open();
DataSet thisDataSet =
new
DataSet();
thisAdapt.Fill(thisDataSet,
"帐户表"
);
string
s1 =
"select 金额 from 帐户表
where 账号="
+ tokens[1];
setMessage(
"c002#"
+ s1);
//填写账户余额
SendData();
tokens[0].Remove(0);
}
break
;
case
"c03"
:
{
//更改帐户密码的sql语句,完成后返回数据003成功
SqlConnection thisConnection
=
new
SqlConnection(
"server = localhost;Integrated
Security =true;database = security;"
);
SqlDataAdapter thisAdapt
=
new
SqlDataAdapter(
"select * from 帐户表"
, thisConnection);
SqlCommandBuilder thisbuider
=
new
SqlCommandBuilder(thisAdapt);
SqlCommand thisCommand
= thisConnection.CreateCommand();
thisConnection.Open();
DataSet thisDataSet =
new
DataSet();
thisAdapt.Fill(thisDataSet,
"帐户表"
);
foreach
(DataRow rows
in
thisDataSet.Tables[
"帐户表"
].Rows)
{
string
s1 =
"select 密码 from 帐户表
where 账号="
+ tokens[1];
if
(s1.CompareTo(tokens[1]) == 0)
{
thisCommand.CommandText
=
"update 帐户表 set
密码 = "
+ tokens[3] +
"where 账号="
+ tokens[1];
setMessage(
"c003#1"
);
//修改密码成功
SendData();
tokens[0].Remove(0);
}
else
{
setMessage(
"c003#2"
);
//修改密码失败
SendData();
tokens[0].Remove(0);
}
}
}
break
;
case
"c04"
:
{
operatorNO = Int32.Parse(tokens[1]);
if
(isOperated[operatorNO])
//这句要查下,c#中的bool初始值是什么?true还是false
{
SendData();
isOperated[operatorNO] =
true
;
//将“已操作”标记设置为true
}
else
{
//执行转账操作的SQL语句,完成后并返回信息
SqlConnection thisConnection
=
new
SqlConnection(
"server = localhost;Integrated
Security =true;database = security;"
);
SqlDataAdapter thisAdapt
=
new
SqlDataAdapter(
"select * from 帐户表"
, thisConnection);
thisConnection.Open();
SqlCommand thisCommand
= thisConnection.CreateCommand();
DataSet thisDataSet =
new
DataSet();
thisAdapt.Fill(thisDataSet,
"帐户表"
);
string
s2 =
"select 金额 from 帐户表
where 账号="
+ tokens[1];
string
s3 =
"select 金额 from 帐户表
where 账号="
+ tokens[1];
if
(s2.CompareTo(tokens[3]) < 0)
setMessage(
"c004#1"
);
else
if
(s3 ==
null
)
setMessage(
"c004#2"
);
else
{
foreach
(DataRow rows
in
thisDataSet.Tables[
"帐户表"
].Rows)
{
if
(rows[
"账号"
].ToString() == tokens[1])
thisCommand.CommandText =
"update 帐户表 set 金额 = 金额 -"
+ tokens[3] +
"where 账号="
+ tokens[1];
if
(rows[
"账号"
].ToString() == tokens[2])
thisCommand.CommandText
=
"update 帐户表 set 金额 = 金额 +"
+ tokens[3] +
"where 账号="
+ tokens[2];
}
setMessage(
"c004#3"
);
}
SendData();
tokens[0].Remove(0);
isOperated[operatorNO] =
true
;
//将“已操作”标记设置为true
}
}
break
;
case
"c05"
:
{
operatorNO = Int32.Parse(tokens[1]);
if
(isOperated[operatorNO])
//这句要查下,c#中的bool初始值是什么?true还是false
{
SendData();
isOperated[operatorNO] =
true
;
//将“已操作”标记设置为true
}
else
{
//执行转账操作的SQL语句,完成后并返回信息
}
SendData();
tokens[0].Remove(0);
isOperated[operatorNO] =
true
;
//将“已操作”标记设置为true
}
break
;
}
msg = streamReader.ReadLine();
}
MessageBox.Show(
"一名客户退出"
);
ReleaseResource();
startServer();
}
public
void
setMessage(
string
message)
{
this
.message = message;
}
private
void
SendData()
{
streamWriter.Write(message);
streamWriter.Flush();
//streamWriter.Close();
}
private
void
ReleaseResource()
{
if
(networkStream !=
null
)
{
networkStream.Close();
streamReader.Close();
streamWriter.Close();
socketForClient.Shutdown(SocketShutdown.Both);
socketForClient.Close();
tcpListener.Stop();
}
}
public
void
startServer()
{
serverThread =
new
Thread(
new
ThreadStart(Listen));
serverThread.Start();
}
public
void
serverExit()
{
string
exitMsg =
"serverExit"
;
//要退出时,发送exit信息给服务器
setMessage(exitMsg);
SendData();
ReleaseResource();
}
}
public
partial
class
BankClient
{
private
NetworkStream networkStream;
private
StreamReader streamReader;
private
StreamWriter streamWriter;
private
TcpClient myclient;
private
Thread recvThread;
private
Thread sendThread;
private
Thread serverThread;
private
bool
flag =
true
;
private
void
Connection()
{
try
{
Int32 port = 8888;
myclient =
new
TcpClient(
"10.1.1.10"
, port);
}
catch
{
MessageBox.Show(
"没有连接到银行服务器!"
);
}
networkStream = myclient.GetStream();
streamReader =
new
StreamReader(networkStream);
streamWriter =
new
StreamWriter(networkStream);
// recvThread = new Thread(new ThreadStart(RecvData));
// recvThread.Start();
MessageBox.Show(
"连接到银行服务器!"
);
}
private
void
RecvData()
{
string
s = streamReader.ReadLine();
string
[] tokens = s.Split(
new
char
[] {
'#'
});
while
(!s.Equals(
"severExit"
))
{
if
(tokens[0] ==
"1"
)
{
SqlConnection thisConnection
=
new
SqlConnection(
"server = localhost;Integrated
Security = true;database = security;"
);
SqlDataAdapter thisAdapt
=
new
SqlDataAdapter(
"select * from 转账接口表"
, thisConnection);
SqlCommandBuilder thisbuider
=
new
SqlCommandBuilder(thisAdapt);
SqlCommand thisCommand
= thisConnection.CreateCommand();
DataSet thisSet =
new
DataSet();
thisAdapt.Fill(thisSet,
"转账接口表"
);
thisConnection.Open();
// thisCommand.CommandText = "update 转账接口表 set 标志位 = 3" + "where 转账序号=" + tokens[1];
thisCommand.CommandText
=
"delete from 转账接口表 where 转账序号="
+ tokens[1];
thisCommand.ExecuteNonQuery();
flag =
true
;
}
else
flag =
false
;
s = streamReader.ReadLine();
}
ReleaseResouce();
}
private
void
SendData()
{
SqlConnection thisConnection
=
new
SqlConnection(
"server = localhost;Integrated
Security = true;database = security;"
);
SqlDataAdapter thisAdapt
=
new
SqlDataAdapter(
"select * from 转账接口表"
, thisConnection);
SqlCommandBuilder thisbuider
=
new
SqlCommandBuilder(thisAdapt);
SqlCommand thisCommand
= thisConnection.CreateCommand();
DataSet thisSet =
new
DataSet();
thisAdapt.Fill(thisSet,
"转账接口表"
);
thisConnection.Open();
foreach
(DataRow rows
in
thisSet.Tables[
"转账接口表"
].Rows)
{
if
(rows[
"标志位"
].ToString() ==
"1"
)
{
string
s;
s = rows[
"转账序号"
].ToString()
+
'#'
+ rows[
"发起端账号"
].ToString()
+
'#'
+ rows[
"接收端账号"
].ToString()
+
'#'
+ rows[
"发生金额"
] +
'#'
+ rows[
"标志位"
].ToString();
if
(flag)
{
streamWriter =
new
StreamWriter(networkStream);
streamWriter.WriteLine(s);
streamWriter.Flush();
System.Threading.Thread.Sleep(5000);
}
else
{
//将数据读入资金变更表
thisCommand.CommandText
=
"delete from 转账接口表 where 标志位= '3'"
;
}
}
}
}
private
void
ReleaseResouce()
{
networkStream.Close();
streamReader.Close();
streamWriter.Close();
sendThread.Abort();
//serverThread.Abort();
myclient.Close();
}
public
void
startBankClient( )
{
serverThread =
new
Thread(
new
ThreadStart(Connection));
serverThread.Start();
int
row = 0;
SqlConnection thisConnection
=
new
SqlConnection(
"server = localhost;Integrated
Security = true;database = security;"
);
SqlDataAdapter thisAdapt
=
new
SqlDataAdapter(
"select * from 转账接口表"
, thisConnection);
SqlCommandBuilder thisbuider
=
new
SqlCommandBuilder(thisAdapt);
DataSet thisSet =
new
DataSet();
thisAdapt.Fill(thisSet,
"转账接口表"
);
while
(
true
)
{
foreach
(DataRow rows
in
thisSet.Tables[
"转账接口表"
].Rows)
{
row++;
}
if
(row > 0)
{
SendData();
RecvData();
}
System.Threading.Thread.Sleep(5000);
}
}
public
void
exitBankClient( )
{
streamWriter.Flush();
ReleaseResouce();
}
}
}
(福建师范大学软件学院)