主题:[求助] 求一个c/s模式并采用异步Socket的 某管理系统代码
我想要的是一个用C#开发的win窗体c/s模式的程序,并不是asp.net的网页程序。
最好是用vs.net2005编辑 数据库是sql sever 2000
系统可以为:学生成绩管理系统 某公司人员管理系统 药房药品管理系统 等等类似的
模式基本是:客户端---(互联网络路径)---服务器---数据库
服务器端:接受用户发送过来的信息,并进行与数据库相关的处理。如当用户发送登录、注册或进行别的请求时,服务器或做出相应的处理,并把处理后的信息返回给客户端。
客户端:与服务器建立连接,用户登录后能进行一系列的操作。如查询、添加、删除等基本操作。
呵呵,经过昨天下午到现在的努力...写了个关于客户端登录服务器的小程序,但貌似错误多多...
大家帮忙看看:
1:运行服务器,点击button1开始监听,然后并不运行客户端,直接点button2关闭服务器。
错误行:handler = listener.EndAccept(ar);
报错为:无法访问已释放的对象。\r\n对象名:“System.Net.Sockets.Socket”。
2:运行服务器,点击button1开始监听,运行客户端,但马上报错:
错误行:int bytesRead = handler.EndReceive(ar);
报错为:由于线程退出或应用程序请求,已放弃 I/O 操作。
我第一次写c/s模式下的代码,罗嗦,幼稚,不简练,不优美的地方请大家指出:非常感谢!!!
服务器代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Data.SqlClient;
using System.Threading;
namespace fwq
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
StateObject state;
private IPAddress myip = IPAddress.Parse("127.0.0.1");
private IPEndPoint myserver;
private Socket mySocket;
private Socket handler;
private string getCommand = "";
private string[] spliComm;
private bool Exists = false;
private static AutoResetEvent myReset = new AutoResetEvent(false);
private SqlCommand myselectcmd1;
private SqlCommand myinsertcmd1;
private SqlCommand mydeletecmd1;
private SqlCommand myupdatecmd1;
private SqlDataAdapter myda;
private SqlConnection mycn;
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
try
{
myserver = new IPEndPoint(myip, 5555);
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
mySocket.Bind(myserver);
mySocket.Listen(50);
Thread thread = new Thread(new ThreadStart(target));
thread.Start();
}
catch (Exception ee) { MessageBox.Show(ee.Message + "\r\n"); }
}
private void target()
{
while (true)
{
myReset.Set();
mySocket.BeginAccept(new AsyncCallback(AcceptCallback), mySocket);
myReset.WaitOne();
}
}
private void AcceptCallback(IAsyncResult ar)
{
myReset.Set();
Socket listener = (Socket)ar.AsyncState;
handler = listener.EndAccept(ar); //报错:无法访问已释放的对象。\r\n对象名:“System.Net.Sockets.Socket”。
getCommand = "";
StateObject state = new StateObject();
state.workSocket = handler;
Thread thread = new Thread(new ThreadStart(begReceive));
thread.Start();
}
private void begReceive()
{
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
private void sendMessage(string message)
{
byte[] byteDate = System.Text.Encoding.UTF8.GetBytes(message + "\r\n");
handler.BeginSend(byteDate, 0, byteDate.Length, 0, new AsyncCallback(SendCallback), handler);
}
private void SendCallback(IAsyncResult ar)
{
try
{
handler = (Socket)ar.AsyncState;
int bytesSent = handler.EndSend(ar);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private string chenEnd(string aimString)
{
int x = aimString.IndexOf("\r\n");
if (x != 1)
{
string subComm = aimString.Substring(0, x);
getCommand = getCommand + subComm;
return getCommand;
}
else
{
getCommand = getCommand + aimString;
return "";
}
}
private void spliCommand(string aimCommand)
{
char[] a = new char[] { };
spliComm = new string[5];
spliComm = aimCommand.Split(a);
}
private void ReadCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
Socket tt = state.workSocket;
int bytesRead = handler.EndReceive(ar); //报错:由于线程退出或应用程序请求,已放弃 I/O 操作。
state.sb.Append(System.Text.Encoding.UTF8.GetString(state.buffer, 0, bytesRead));
string content = state.sb.ToString();
state.sb.Remove(0,content.Length);
string command = chenEnd(content);
getCommand = "";
if (command != "")
{
spliCommand(command);
string strSql1 = "select * from newuser";
mycn = new SqlConnection();
mycn = new SqlConnection("server=localhost;uid=sa;pwd=;database=openjjsys;");
mycn.Open();
myselectcmd1 = new SqlCommand(strSql1, mycn);
SqlDataReader reader1 = myselectcmd1.ExecuteReader();
Exists=false;
while (reader1.Read())
{
if (spliComm[0] == "1000")
{
int x = reader1.GetString(1).ToString().IndexOf(spliComm[1]);
int y = reader1.GetString(2).ToString().IndexOf(spliComm[2]);
if ((x != -1) && (y != -1))
{
sendMessage("1000" + "\r\n" + "1" + "\r\n" + "<EOF>");
Exists = true;
}
else
{
sendMessage("1000" + "\r\n" + "2" + "\r\n" + "<EOF>");
}
}
}
reader1.Close();
mycn.Close();
}
tt.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = true;
try
{
mySocket.Close();
}
catch { MessageBox.Show("监听尚未开始,关闭无效!"); }
}
}
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 1024;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();
public StateObject()
{ }
}
}
本人是初学者,只做过小的聊天系统,对多个数据的同时发送很不了解。现在想开发个c/s模式下的程序,但由于对客户端与服务器间的多个数据的交互很不了解,所以希望大家能发个代码让我学习。
我一直认为每个人都是从新手过来的,每个人都需要帮助,每个热爱学习的人都应该得到大家的支持,对每个支持我的人,我都会非常感谢.....
我的邮箱是:304566465@qq.com
最好是用vs.net2005编辑 数据库是sql sever 2000
系统可以为:学生成绩管理系统 某公司人员管理系统 药房药品管理系统 等等类似的
模式基本是:客户端---(互联网络路径)---服务器---数据库
服务器端:接受用户发送过来的信息,并进行与数据库相关的处理。如当用户发送登录、注册或进行别的请求时,服务器或做出相应的处理,并把处理后的信息返回给客户端。
客户端:与服务器建立连接,用户登录后能进行一系列的操作。如查询、添加、删除等基本操作。
呵呵,经过昨天下午到现在的努力...写了个关于客户端登录服务器的小程序,但貌似错误多多...
大家帮忙看看:
1:运行服务器,点击button1开始监听,然后并不运行客户端,直接点button2关闭服务器。
错误行:handler = listener.EndAccept(ar);
报错为:无法访问已释放的对象。\r\n对象名:“System.Net.Sockets.Socket”。
2:运行服务器,点击button1开始监听,运行客户端,但马上报错:
错误行:int bytesRead = handler.EndReceive(ar);
报错为:由于线程退出或应用程序请求,已放弃 I/O 操作。
我第一次写c/s模式下的代码,罗嗦,幼稚,不简练,不优美的地方请大家指出:非常感谢!!!
服务器代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Data.SqlClient;
using System.Threading;
namespace fwq
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
StateObject state;
private IPAddress myip = IPAddress.Parse("127.0.0.1");
private IPEndPoint myserver;
private Socket mySocket;
private Socket handler;
private string getCommand = "";
private string[] spliComm;
private bool Exists = false;
private static AutoResetEvent myReset = new AutoResetEvent(false);
private SqlCommand myselectcmd1;
private SqlCommand myinsertcmd1;
private SqlCommand mydeletecmd1;
private SqlCommand myupdatecmd1;
private SqlDataAdapter myda;
private SqlConnection mycn;
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
try
{
myserver = new IPEndPoint(myip, 5555);
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
mySocket.Bind(myserver);
mySocket.Listen(50);
Thread thread = new Thread(new ThreadStart(target));
thread.Start();
}
catch (Exception ee) { MessageBox.Show(ee.Message + "\r\n"); }
}
private void target()
{
while (true)
{
myReset.Set();
mySocket.BeginAccept(new AsyncCallback(AcceptCallback), mySocket);
myReset.WaitOne();
}
}
private void AcceptCallback(IAsyncResult ar)
{
myReset.Set();
Socket listener = (Socket)ar.AsyncState;
handler = listener.EndAccept(ar); //报错:无法访问已释放的对象。\r\n对象名:“System.Net.Sockets.Socket”。
getCommand = "";
StateObject state = new StateObject();
state.workSocket = handler;
Thread thread = new Thread(new ThreadStart(begReceive));
thread.Start();
}
private void begReceive()
{
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
private void sendMessage(string message)
{
byte[] byteDate = System.Text.Encoding.UTF8.GetBytes(message + "\r\n");
handler.BeginSend(byteDate, 0, byteDate.Length, 0, new AsyncCallback(SendCallback), handler);
}
private void SendCallback(IAsyncResult ar)
{
try
{
handler = (Socket)ar.AsyncState;
int bytesSent = handler.EndSend(ar);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private string chenEnd(string aimString)
{
int x = aimString.IndexOf("\r\n");
if (x != 1)
{
string subComm = aimString.Substring(0, x);
getCommand = getCommand + subComm;
return getCommand;
}
else
{
getCommand = getCommand + aimString;
return "";
}
}
private void spliCommand(string aimCommand)
{
char[] a = new char[] { };
spliComm = new string[5];
spliComm = aimCommand.Split(a);
}
private void ReadCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
Socket tt = state.workSocket;
int bytesRead = handler.EndReceive(ar); //报错:由于线程退出或应用程序请求,已放弃 I/O 操作。
state.sb.Append(System.Text.Encoding.UTF8.GetString(state.buffer, 0, bytesRead));
string content = state.sb.ToString();
state.sb.Remove(0,content.Length);
string command = chenEnd(content);
getCommand = "";
if (command != "")
{
spliCommand(command);
string strSql1 = "select * from newuser";
mycn = new SqlConnection();
mycn = new SqlConnection("server=localhost;uid=sa;pwd=;database=openjjsys;");
mycn.Open();
myselectcmd1 = new SqlCommand(strSql1, mycn);
SqlDataReader reader1 = myselectcmd1.ExecuteReader();
Exists=false;
while (reader1.Read())
{
if (spliComm[0] == "1000")
{
int x = reader1.GetString(1).ToString().IndexOf(spliComm[1]);
int y = reader1.GetString(2).ToString().IndexOf(spliComm[2]);
if ((x != -1) && (y != -1))
{
sendMessage("1000" + "\r\n" + "1" + "\r\n" + "<EOF>");
Exists = true;
}
else
{
sendMessage("1000" + "\r\n" + "2" + "\r\n" + "<EOF>");
}
}
}
reader1.Close();
mycn.Close();
}
tt.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = true;
try
{
mySocket.Close();
}
catch { MessageBox.Show("监听尚未开始,关闭无效!"); }
}
}
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 1024;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();
public StateObject()
{ }
}
}
本人是初学者,只做过小的聊天系统,对多个数据的同时发送很不了解。现在想开发个c/s模式下的程序,但由于对客户端与服务器间的多个数据的交互很不了解,所以希望大家能发个代码让我学习。
我一直认为每个人都是从新手过来的,每个人都需要帮助,每个热爱学习的人都应该得到大家的支持,对每个支持我的人,我都会非常感谢.....
我的邮箱是:304566465@qq.com