我自己做的一段程序
来源:互联网 发布:翻墙方法 知乎 编辑:程序博客网 时间:2024/05/17 11:03
我用多线程的一简单的登录程序,与大家一起分享一下,献丑了
server端的代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Data;
using System.Data.SqlClient;
namespace server
{
class Program
{
static void Main(string[] args)
{
tcpserver info = new tcpserver();
}
}
class tcpserver
{
private Socket server;
public tcpserver()
{
IPAddress local = IPAddress.Parse("127.0.0.1");
IPEndPoint iep = new IPEndPoint(local, 13000);
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(iep);
server.Listen(20);
Console.Write("----------------等待连接---------------");
while (true)
{
Socket client = server.Accept();
clientthred newclient = new clientthred(client);
Thread newthread = new Thread(new ThreadStart(newclient.clientservice));
newthread.Start();
}
}
}
class clientthred
{
public Socket service;
int i;
public clientthred(Socket clientsocket)
{
this.service = clientsocket;
}
public void clientservice()
{
string data;
byte[] bytes = new byte[024];
string back;
Console.WriteLine('/n'+"------------------连接成功---------------");
while ((i = service.Receive(bytes)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine('/n' + "收到数据{0}", data);
Data conn = new Data();
DataTableReader ws = conn.datas(data);
if (ws.HasRows)
{
Console.WriteLine("----------------------------");
while (ws.Read())
{
Console.WriteLine("用户登录的密码是:{0}", ws[0]);
back = ws[0].ToString().Trim();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(back);
service.Send(msg);
Console.WriteLine("发送数据{0}",back);
}
}
else
{
Console.WriteLine("no");
back ="no";
byte[] qw = System.Text.Encoding.ASCII.GetBytes(back);
service.Send(qw);
Console.WriteLine("发送数据{0}", back);
}
}
}
}
}
数据处理的类:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace server
{
class Data
{
public DataTableReader datas(string username)
{
string connstring = "data source=.; initial catalog= jssk;user id=sa";
using (SqlConnection conn = new SqlConnection(connstring))
{
try
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter daor = new SqlDataAdapter("select passwd from userinfo where username='" + username + "'", conn);
daor.Fill(ds, "info");
DataTableReader dtr = ds.CreateDataReader();
return dtr;
}
catch (SqlException e)
{
Console.WriteLine(e.Message);
return null;
}
}
}
}
}
客户端的代码:
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.Threading;
namespace client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int i;
Socket client;
byte[] buf = new byte[1024];
IPAddress local = IPAddress.Parse("127.0.0.1");
IPEndPoint iep = new IPEndPoint(local, 13000);
try
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(iep);
}
catch(SocketException)
{
MessageBox.Show("无法连接到服务器");
return;
}
string name = textBox1.Text.ToString().Trim();
string passwd = textBox2.Text.ToString().Trim();
string message = name;
client.Send(Encoding.ASCII.GetBytes(name));
if ((i = client.Receive(buf)) != 0)
{
string back = System.Text.Encoding.ASCII.GetString(buf, 0, i);
if (back == "no")
{
MessageBox.Show("密码错误");
}
else
{
int w = string.Compare(back, textBox2.Text.ToString().Trim());
if (w == 0)
{
Form2 qw = new Form2();
qw.Show();
}
else if (w != 0)
{
MessageBox.Show("密码错误");
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
- 我自己做的一段程序
- 自己的一段程序
- 一段自己打印自己的c程序
- 一段自己打印自己的c程序
- 一段自己打印自己的c程序
- 一段自己打印自己的c程序
- JSR184,我学完自己做的程序
- 打印自己的一段漂亮C程序
- 《我如何自己做自己的导师》
- 讲述我一段自己打游戏的感受
- LotusScript 一段对字符串前后做修改的程序
- 我为自己代言,今天起做一个幸福的程序猿!
- 哈哈,我为自己代言,做一个幸福的程序猿!
- 、做最好的自己--【我的2014】
- 我的2016:做精彩的自己
- 做自己的主人-我的2016
- 我写的一段"记事本"的程序,请高手指教.
- 我写的一段关于检测时间合法性的程序
- 微软的面试题及答案-经典
- A story happened on an island 孤岛上的故事
- 菜鸟有多菜
- HTML字符实体 “<” “>”的转换
- 徐凡的Blog
- 我自己做的一段程序
- ADD构架设计方法
- TCP连接的状态转换图深度剖析
- 成功之必备9大好习惯(转)
- dos batch file in windows
- 你是最适合创业的程序员吗?
- IT世界里,所有的发明都是用来被推翻的!java也好,.NET也罢,都无法逃脱这样的宿命
- Oracle存储过程
- SQL注入的防止--使用preparedStatement