关于我的PP0.1聊天软件(客户端)

时间:2022-06-25 22:12:44

登陆界面:

关于我的PP0.1聊天软件(客户端)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml; namespace PP0._1
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
public static String ip;
public static int port;
bool formMove = false;//窗体是否移动
Point formPoint;//记录窗体的位置
private void Login_Load(object sender, EventArgs e)
{
initServiseInfo();
}
public void initServiseInfo()
{
XmlDocument doc = new XmlDocument();
doc.Load("ServiseInfo.xml");
XmlNode root = doc.DocumentElement;
foreach(XmlNode item in root.ChildNodes){
switch (item.Name)
{
case "ip":
ip = item.InnerText;
break;
case "port":
port = Convert.ToInt32(item.InnerText);
break;
}
}
} private void btnLogin_Click(object sender, EventArgs e)
{
LoginNow();
}
public void LoginNow()
{
if (txtName.Text!="" && txtPwd.Text!="")
{
byte[] byteOrder = new byte[1024];
String userInfo = "name:" + txtName.Text + "pwd:" + txtPwd.Text + "type:1";//1为登陆
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
socket.Send(Encoding.ASCII.GetBytes(userInfo));
String order = Encoding.ASCII.GetString(byteOrder, 0, socket.Receive(byteOrder));
if (order == "loginSucceed")
{
this.Hide();
PPMian main = new PPMian();
main.Show();
}
else if (order == "noUser")
{
DialogResult result = MessageBox.Show("未存在此用户,是否跳转,注册页面?", "", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
this.Hide();
Register register = new Register();
register.Show();
}
}
else if (order=="pwdErro")
{
MessageBox.Show("密码错误");
}
}
else if (txtName.Text=="")
{
MessageBox.Show("用户名不能为空");
}
else if (txtPwd.Text=="")
{
MessageBox.Show("密码不能为空");
} } private void linkLabRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Register register = new Register();
register.Show();
this.Hide();
} private void labLogin_Click(object sender, EventArgs e)
{ } private void pictureBox1_Click(object sender, EventArgs e)
{ } private void Login_MouseDown(object sender, MouseEventArgs e)
{
formPoint = new Point();
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
formPoint = new Point(xOffset, yOffset);
formMove = true;//开始移动
}
} private void Login_MouseMove(object sender, MouseEventArgs e)
{
if (formMove == true)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(formPoint.X, formPoint.Y);
Location = mousePos;
}
} private void Login_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//按下的是鼠标左键
{
formMove = false;//停止移动
}
} private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//按下的是鼠标左键
{
formMove = false;//停止移动
}
} private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (formMove == true)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(formPoint.X, formPoint.Y);
Location = mousePos;
}
} private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
formPoint = new Point();
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
formPoint = new Point(xOffset, yOffset);
formMove = true;//开始移动
}
} private void btnExit_Click(object sender, EventArgs e)
{
DialogResult result= MessageBox.Show("是否确定退出?","",MessageBoxButtons.YesNo);
if (result==DialogResult.Yes)
{
System.Environment.Exit(0);
}
}
}
}

读取xml文件,获取ip和端口

public void initServiseInfo()
{
XmlDocument doc = new XmlDocument();
doc.Load("ServiseInfo.xml");
XmlNode root = doc.DocumentElement;
foreach(XmlNode item in root.ChildNodes){
switch (item.Name)
{
case "ip":
ip = item.InnerText;
break;
case "port":
port = Convert.ToInt32(item.InnerText);
break;
}
}
}

登陆的判断操作

public void LoginNow()
{
if (txtName.Text!="" && txtPwd.Text!="")
{
byte[] byteOrder = new byte[1024];
String userInfo = "name:" + txtName.Text + "pwd:" + txtPwd.Text + "type:1";//1为登陆
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
socket.Send(Encoding.ASCII.GetBytes(userInfo));
String order = Encoding.ASCII.GetString(byteOrder, 0, socket.Receive(byteOrder));
if (order == "loginSucceed")
{
this.Hide();
PPMian main = new PPMian();
main.Show();
}
else if (order == "noUser")
{
DialogResult result = MessageBox.Show("未存在此用户,是否跳转,注册页面?", "", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
this.Hide();
Register register = new Register();
register.Show();
}
}
else if (order=="pwdErro")
{
MessageBox.Show("密码错误");
}
}
else if (txtName.Text=="")
{
MessageBox.Show("用户名不能为空");
}
else if (txtPwd.Text=="")
{
MessageBox.Show("密码不能为空");
}

}

注册界面:

关于我的PP0.1聊天软件(客户端)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; namespace PP0._1
{
public partial class Register : Form
{
public Register()
{
InitializeComponent();
}
bool formMove = false;//窗体是否移动
Point formPoint;//记录窗体的位置
private void Register_MouseDown(object sender, MouseEventArgs e)
{
formPoint = new Point();
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
formPoint = new Point(xOffset, yOffset);
formMove = true;//开始移动
}
} private void Register_MouseMove(object sender, MouseEventArgs e)
{
if (formMove == true)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(formPoint.X, formPoint.Y);
Location = mousePos;
}
} private void Register_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//按下的是鼠标左键
{
formMove = false;//停止移动
}
} private void btnCancle_Click(object sender, EventArgs e)
{
this.Hide();
Login login = new Login();
login.Show();
} private void btnRegister_Click(object sender, EventArgs e)
{
RegisterNow();
}
public void RegisterNow()
{
if (txtName.Text!="" && txtPwd.Text!="")
{
byte[] byteOrder=new byte[1024];
String userInfo = "name:" + txtName.Text + "pwd" + txtPwd.Text+"type:2";//2为注册
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(new IPEndPoint(IPAddress.Parse(Login.ip), Login.port));
socket.Send(Encoding.ASCII.GetBytes(userInfo));
String order;
order = Encoding.ASCII.GetString(byteOrder,0,socket.Receive(byteOrder));
if (order == "registerSucceed")
{
MessageBox.Show("注册成功");
Login login = new Login();
this.Hide();
login.Show();
}
else if (order=="userLess")
{
MessageBox.Show("用户名重名,请重新注册");
}
}else if(txtName.Text==""){
MessageBox.Show("用户名不能为空");
}
else if (txtPwd.Text=="")
{
MessageBox.Show("密码不能为空");
} }
private void Register_Load(object sender, EventArgs e)
{ }
}
}