我想要的是一个用C#开发的win窗体c/s模式的程序,并不是asp.net的网页程序。最好是用vs.net2005编辑 数据库是sql sever 2000系统可以为:学生成绩管理系统 某公司人员管理系统 药房药品管理系统 等等类似的模式基本是:客户端---(互联网络路径)---服务器---数据库服务器端:接受用户发送过来的信息,并进行与数据库相关的处理。如当用户发送登录、注册或进行别的请求时,服务器或做出相应的处理,并把处理后的信息返回给客户端。客户端:与服务器建立连接,用户登录后能进行一系列的操作。如查询、添加、删除等基本操作。
本人是初学者,只做过小的聊天系统,对多个数据的同时发送很不了解。现在想开发个c/s模式下的程序,但由于对客户端与服务器间的多个数据的交互很不了解,所以希望大家能发个代码让我学习。我一直认为每个人都是从新手过来的,每个人都需要帮助,每个热爱学习的人都应该得到大家的支持,对每个支持我的人,我都会非常感谢.....
本人是初学者,只做过小的聊天系统,对多个数据的同时发送很不了解。现在想开发个c/s模式下的程序,但由于对客户端与服务器间的多个数据的交互很不了解,所以希望大家能发个代码让我学习。我一直认为每个人都是从新手过来的,每个人都需要帮助,每个热爱学习的人都应该得到大家的支持,对每个支持我的人,我都会非常感谢.....
解决方案 »
- wpf 关闭定时器
- 关于OnMouseDown事件中获取的值
- 如何解决Access to the registry key is denied.
- ListView闪烁的问题,急!
- 怎么给button设键盘的默认键?及时给分!
- vs2008文件打包,为什么有时候会把.net3.5打进去
- textbox不支持透明色吗?
- 我注册MSDART.DLL时 提示 已加载E:\BESOFT\BE-SDH\MSDART.DLL但没有找到DLLREGISTSERVER输入点,无法注册这个文件,请问是怎么回事~~~急
- HttpWebRequest 获取的页面为什么是空白的
- 一个很不幸的问题发生在我身上了。。。。。。。。。。。。。
- 活动目录编程:如何把一个用户添加到一个指定的用户组呢?
- vs2005安装包问题:如何在卸载WEB项目的时候,卸载WEB对应的数据库
客户端+数据库的啊?
那样就不需要SOCKET编程了,比较方便。
错误行: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()
{ }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Text;
using System.Windows.Forms;namespace openjjkh
{
public partial class enter : Form
{
public enter()
{
InitializeComponent();
} private StateObject state;
private IPAddress myip = IPAddress.Parse("127.0.0.1");
private string getData = "";
private string getCommand = "";
private string[] spliComm;
private IPEndPoint myserver;
private Socket mySocket;
private static ManualResetEvent connectReset = new ManualResetEvent(false);
private static ManualResetEvent sendReset = new ManualResetEvent(false); private void enter_Load(object sender, EventArgs e)
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
try
{
myserver = new IPEndPoint(myip, 5555);
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
mySocket.BeginConnect(myserver, new AsyncCallback(ConnectCallback), mySocket);
connectReset.WaitOne();
}
catch (Exception ee) { MessageBox.Show(ee.Message); }
}
private void button1_Click(object sender, EventArgs e)
{
string sendSele = null;
if (textBox1.Text != "" && textBox2.Text != "")
{
sendSele = "1000" + " " + textBox1.Text + " " + textBox2.Text + "\r\n";
}
else
MessageBox.Show("用户名或密码不能为空!");
}
private void ConnectCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
client.EndConnect(ar);
getData = "";
Thread thread = new Thread(new ThreadStart(target));
thread.Start();
connectReset.Set();
}
catch { }
} private void spliCommand(string aimCommand)
{
char[] a = new char[] { };
spliComm = new string[5];
spliComm = aimCommand.Split(a);
} public void ReceiveCallback(IAsyncResult ar)
{
try
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
int bytesRead = client.EndReceive(ar);
state.sb.Append(System.Text.Encoding.UTF8.GetString(state.buffer, 0, bytesRead));
string backString = state.sb.ToString();
state.sb.Remove(0, backString.Length);
string feedback = chenEnd(backString);
getCommand = "";
if (feedback != "")
{
if (spliComm[0] == "1000")
{
if (spliComm[1] == "1")
{
khdwindow khwin = new khdwindow();
khwin.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名或密码输入错误!!!");
textBox1.Text = "";
textBox2.Text = "";
}
}
getData = "";
}
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
catch { }
}
private string chenEnd(string aimString)
{
int x = aimString.IndexOf("<EOF>");
if (x != -1)
{
string subcomm = aimString.Substring(0, x);
getData = getData + subcomm;
return getData;
}
else
{
getData = getData + aimString;
return "";
}
} private void SendCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
sendReset.Set();
}
catch (Exception ee)
{
Console.WriteLine(ee.ToString());
}
} private void target()
{
try
{
StateObject state = new StateObject();
state.workSocket = mySocket;
mySocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
} }
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()
{ }
}
}肯定有很多问题的地方,请大家帮我指出....非常感谢了....
---------------------
做成客户端+数据库应该容易很多
主 和 神 都会保佑你的.....呵呵.... 我也明白,但现在偶很想用Socket去实现他...------------------------------------ 呵呵,感谢 云中看海 支持....
佛 和 党 都会保佑你的.....呵呵....我也想加分啊...可我不够加的啊......
一是Socket编程复杂,一般是在局域网环境下做,安全设置比较高,因为要开端口
二是需要考虑防火墙的支持,就是没有防火墙,也要考虑服务商的支持,比如在辽宁,SQL SERVER的1433就是封的
三是要考虑代理服务器,如果客户端是公司,需公司是通过代理服务器上网呢,还要考虑代理服务器的支持
四是要有自己的传输协议所以综上,如果要实现外网通信,最好还是用webServices,安全,简单