C#通讯编程(整理)|大家可以进来学习
koobee学生在校写的《银证转帐系统》的通信代码
C# code
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;
http://topic.csdn.net/u/20080623/08/4bbd2475-45f1-42e3-a613-16b094759ade.html
他们设置了哪些标签:
.net 1 2 3 C# C#_C#通讯编程(整理) C#通讯 C#通讯编程 C#通讯编程(代码) C#通讯编程(整理 C#通讯编程(整理) C#通讯编程(整理)| C#通讯编程(整理)|大家可以进来学习 C#通讯编程资料 C#网络 c#网络编程 C#学习 http://topic.csdn.net/u/20080623/08/4bbd2475-45f1-42e3-a613-16b094759ade.html SOCKET socket通讯 Stocket编程 TCP tongxun tongxunbiancheng UDP 多线程 技术相关 论坛C# 通信 通讯 通讯编程 网络 学习 异步模式
谁收藏了这个网址:
时间:2008-6-23 8:38:24 | 相关网摘
时间:2008-6-23 8:54:49 | 相关网摘
时间:2008-6-23 9:14:03 | 相关网摘
时间:2008-6-23 10:16:17 | 相关网摘
时间:2008-6-23 10:29:05 | 相关网摘
时间:2008-6-23 10:53:38 | 相关网摘
时间:2008-6-23 11:02:05 | 相关网摘
时间:2008-6-23 11:32:06 | 相关网摘
时间:2008-6-23 11:40:58 | 相关网摘
时间:2008-6-23 11:47:11 | 相关网摘
时间:2008-6-23 12:38:40 | 相关网摘
时间:2008-6-23 13:47:03 | 相关网摘
时间:2008-6-23 13:59:11 | 相关网摘
时间:2008-6-23 14:38:43 | 相关网摘
Thread signal.线程 用一个指示是否将初始状态设置为终止的布尔值初始化 ManualResetEvent 类的新实例。
public static ManualResetEvent allDone = new ManualResetEvent(false);
//static void Main(string[] args)
时间:2008-6-23 15:03:48 | 相关网摘
时间:2008-6-23 15:49:18 | 相关网摘
时间:2008-6-23 16:00:44 | 相关网摘
时间:2008-6-23 16:06:46 | 相关网摘
时间:2008-6-23 16:31:05 | 相关网摘
时间:2008-6-23 16:47:12 | 相关网摘
时间:2008-6-23 16:58:56 | 相关网摘
时间:2008-6-23 17:19:42 | 相关网摘
时间:2008-6-23 18:16:12 | 相关网摘
时间:2008-6-23 18:33:16 | 相关网摘
sock
时间:2008-6-23 20:24:39 | 相关网摘
时间:2008-6-23 21:22:11 | 相关网摘
时间:2008-6-23 22:22:38 | 相关网摘
时间:2008-6-23 22:57:48 | 相关网摘
时间:2008-6-24 0:03:36 | 相关网摘
使用标签:通讯,时间:2008-6-24 8:17:46 | 相关网摘
时间:2008-6-24 8:32:52 | 相关网摘
时间:2008-6-24 8:35:22 | 相关网摘
时间:2008-6-24 8:52:56 | 相关网摘
时间:2008-6-24 8:56:25 | 相关网摘
时间:2008-6-24 9:04:20 | 相关网摘
C#通讯编程(整理)|大家可以进来学习
时间:2008-6-24 9:16:20 | 相关网摘
时间:2008-6-24 9:23:19 | 相关网摘
时间:2008-6-24 9:44:58 | 相关网摘
时间:2008-6-24 10:00:54 | 相关网摘
时间:2008-6-24 11:17:37 | 相关网摘
时间:2008-6-24 11:34:57 | 相关网摘
时间:2008-6-24 11:56:39 | 相关网摘
时间:2008-6-24 11:58:27 | 相关网摘
C#通讯编程
时间:2008-6-24 12:59:00 | 相关网摘
时间:2008-6-24 13:15:16 | 相关网摘
时间:2008-6-24 15:41:05 | 相关网摘
时间:2008-6-24 16:17:03 | 相关网摘
时间:2008-6-24 16:24:12 | 相关网摘
时间:2008-6-24 16:30:13 | 相关网摘
时间:2008-6-24 17:04:39 | 相关网摘
时间:2008-6-24 17:21:33 | 相关网摘
时间:2008-6-24 17:22:08 | 相关网摘
时间:2008-6-24 17:27:25 | 相关网摘
时间:2008-6-24 18:15:15 | 相关网摘
时间:2008-6-24 19:12:55 | 相关网摘
时间:2008-6-24 19:21:14 | 相关网摘
时间:2008-6-24 21:02:16 | 相关网摘
时间:2008-6-24 21:11:21 | 相关网摘
时间:2008-6-25 8:49:53 | 相关网摘
时间:2008-6-25 9:16:18 | 相关网摘
时间:2008-6-25 10:05:16 | 相关网摘
时间:2008-6-25 10:13:08 | 相关网摘
C#通讯编程
时间:2008-6-25 10:14:33 | 相关网摘
时间:2008-6-25 10:37:06 | 相关网摘
时间:2008-6-25 10:42:13 | 相关网摘
时间:2008-6-25 11:01:52 | 相关网摘
时间:2008-6-25 11:30:37 | 相关网摘
时间:2008-6-25 11:34:00 | 相关网摘
时间:2008-6-25 11:42:07 | 相关网摘
C#通讯编程
时间:2008-6-25 11:53:46 | 相关网摘
时间:2008-6-25 12:25:30 | 相关网摘
时间:2008-6-25 12:28:57 | 相关网摘
时间:2008-6-25 12:43:26 | 相关网摘
ASCII
时间:2008-6-25 12:44:53 | 相关网摘
时间:2008-6-25 14:52:13 | 相关网摘
时间:2008-6-25 15:42:17 | 相关网摘
时间:2008-6-25 15:54:56 | 相关网摘
使用标签:3,时间:2008-6-25 16:30:50 | 相关网摘
时间:2008-6-25 17:17:32 | 相关网摘
时间:2008-6-25 17:31:41 | 相关网摘
时间:2008-6-25 17:33:24 | 相关网摘
时间:2008-6-25 17:52:30 | 相关网摘
时间:2008-6-25 19:36:07 | 相关网摘
时间:2008-6-25 21:18:05 | 相关网摘
时间:2008-6-25 21:28:15 | 相关网摘
时间:2008-6-25 22:08:00 | 相关网摘
时间:2008-6-26 2:10:38 | 相关网摘
时间:2008-6-26 8:37:15 | 相关网摘
时间:2008-6-26 9:25:23 | 相关网摘
时间:2008-6-26 9:29:49 | 相关网摘
时间:2008-6-26 9:30:32 | 相关网摘