using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Data.SqlClient;
namespace 局域网聊天软件
{
public partial class ChatRoomClient : Form
{
public ChatRoomClient()
{
InitializeComponent();
}
用户登录 form;
public ChatRoomClient(用户登录 form,string text1)
{
InitializeComponent();
this.form = form;
tBName.Text=text1;
}
TcpClient clientSocket = new TcpClient();
NetworkStream clientStream;
IPEndPoint severIPEndPoint;
Thread threadReceive;
byte[] receiveByte = new byte[1024];
private void bSend_Click(object sender, EventArgs e)
{
byte[] writeByte = Encoding.Default.GetBytes(tBName.Text + ":" + tBSend.Text);
clientStream.Write(writeByte, 0, writeByte.Length);
}
protected override void OnLoad(EventArgs e)
{
//实例化IP地址和端口
severIPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2000);
clientSocket.Connect(severIPEndPoint);
clientStream = clientSocket.GetStream();
threadReceive = new Thread(new ThreadStart(ReceiveThread));
threadReceive.Start();
base.OnLoad(e);
//FriendGridView
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM View_name", "Data Source=.\\SQLExpress;Database=student;Trusted_Connection=true;");
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
FriendGridView.DataSource = dataSet.Tables[0];
}
添加线程函数
void ReceiveThread()
{
while (true)
{
//等待接收服务器消息
{
int len = clientStream.Read(receiveByte, 0, receiveByte.Length);
string message = Encoding.Default.GetString(receiveByte, 0, len);
String[] NameAndMessage = message.Split(':');//分割消息字符串
Invoke(new AddClientCallback(AddClient), new object[] { NameAndMessage[0] });
Invoke(new AddContextDelegate(AddContext), new object[] { message });
}
}
}
delegate void AddContextDelegate(string text);
public void AddContext(string text)
{
rTBChat.AppendText(text + "\n");
}
//为窗体添加AddClientCallback和AddClient的委托和函数,用于显示登录到聊天室的用户
delegate void AddClientCallback(string name);
public void AddClient(string name)
{
if (!lBClient1.Items.Contains(name)) lBClient1.Items.Add(name);//添加用户名称
}
private void bTReturn_Click(object sender, EventArgs e)
{
form.Visible = true;
try
{
threadReceive.Abort();
clientSocket.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
throw;
}
this.Close();
}
private void ChatRoomClient_FormClosed(object sender, FormClosedEventArgs e)
{
form.Visible = true;
}
private void ChatRoomClient_Load(object sender, EventArgs e)
{
}
private void Name_Click(object sender, EventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 局域网聊天软件
{
public partial class Change : Form
{
public Change()
{
InitializeComponent();
}
用户登录 form;
public Change(用户登录 form)
{
InitializeComponent();
this.form = form;
}
private void Change_Load(object sender, EventArgs e)
{
}
private void btSure_Click(object sender, EventArgs e)
{
if (tBNewPassword.Text.Length!=0)
{
string connectionString = //"Data Source=DESKTOP-A3INDTQ.\\SQLEXPRESS;";
"Data Source=.\\SQLExpress;";//数据库服务器
connectionString += "Database=student;";//数据库名称
connectionString += "Trusted_Connection=true;";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandText = "UPDATE student_info SET password = '" + this.tBNewPassword.Text + " ' where name = '" + this.tBUser.Text + "' ";
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("修改成功!");
form.Visible = true;
this.Close();
}
else
{
MessageBox.Show("修改失败!请检查用户名是否正确!");
}
}
else
{
MessageBox.Show("输入格式有误!");
}
}
private void btConcle_Click(object sender, EventArgs e)
{
form.Visible = true;
this.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 局域网聊天软件
{
public partial class FormWelcome : Form
{
public FormWelcome()
{
InitializeComponent();
}
private void FormWelcome_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
this.timer1.Start();
this.timer1.Interval = 100;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Opacity > 0)
{
//每单位时间内增加透明度
this.Opacity -= 0.05;
}
else
{
this.timer1.Stop();
this.Close();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 局域网聊天软件
{
public partial class 注册 : Form
{
public 注册()
{
InitializeComponent();
}
用户登录 form;
public 注册(用户登录 form)
{
InitializeComponent();
this.form = form;
}
private void 注册_Load(object sender, EventArgs e)
{
}
private void btSure_Click(object sender, EventArgs e)
{
//注册前先开启数据库
string connectionString = //"Data Source=DESKTOP-A3INDTQ.\\SQLEXPRESS;";
"Data Source=.\\SQLExpress;";//数据库服务器
connectionString += "Database=student;";//数据库名称
connectionString += "Trusted_Connection=true;";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand cmd = new SqlCommand();//创建数据库查询实例
cmd.Connection = connection;
//插入学生信息sql语句
cmd.CommandText = @"INSERT INTO student_info
(password,name)
VALUES(@password,@name)";
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = tBName.Text;
cmd.Parameters.Add("@password", SqlDbType.Int).Value = tBPassword.Text;
if (tBName.Text !=null && tBPassword.Text != null)
{
try
{
cmd.ExecuteNonQuery();
}
catch (Exception)
{
}
MessageBox.Show("注册成功!!!");
cmd.Dispose();
connection.Close();
form.Visible = true;
this.Close();
}
else
{
MessageBox.Show("注册格式有误,请重新输入!");
}
}
private void btConcle_Click(object sender, EventArgs e)
{
this.Close();
form.Visible = true;
}
private void btconnect_Click(object sender, EventArgs e)
{
}
}
}