| 发表于:2007-09-06 00:30:33 楼主 |
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.net; using system.net.sockets; using system.windows.forms; namespace easychat { public partial class login_frm : form { /// <summary> /// ip地址 /// </summary> private ipaddress _ipaddr; #region 登录窗体构造函数 /// <summary> /// 构造函数,自动生成 /// </summary> public login_frm() { initializecomponent(); } #endregion #region 登录窗体的私有方法 /// <summary> /// 验证登录信息 /// </summary> /// <returns> 验证结果 </returns> private bool validateinfo() { if (user_tb.text.trim() == string.empty) { messagebox.show( "请填写用户名! ", "提示 ", messageboxbuttons.ok, messageboxicon.information); return false; } if (!ipaddress.tryparse(svrip_tb.text, out _ipaddr)) { messagebox.show( "ip地址不合法! ", "提示 ", messageboxbuttons.ok, messageboxicon.information); return false; } int _port; if (!int.tryparse(svrport_tb.text, out _port)) { messagebox.show( "端口号不合法! ", "提示 ", messageboxbuttons.ok, messageboxicon.information); return false; } else { if (_port < 1024 ¦ ¦ _port > 65535) { messagebox.show( "端口号不合法! ", "提示 ", messageboxbuttons.ok, messageboxicon.information); return false; } } return true; } /// <summary> /// 取消,关闭窗体 /// </summary> /// <param name= "sender "> </param> /// <param name= "e "> </param> private void cancel_btn_click(object sender, eventargs e) { this.close(); } /// <summary> /// 登录 /// </summary> /// <param name= "sender "> </param> /// <param name= "e "> </param> private void login_btn_click(object sender, eventargs e) { //验证数据合法性 if (!validateinfo()) { return; } int port = int.parse(svrport_tb.text); //向服务器发出连接请求 tcpconnection conn = new tcpconnection(_ipaddr, port); tcpclient _tcpc = conn.connect(); if (_tcpc == null) { messagebox.show( "无法连接到服务器,请重试! ", "错误 ", messageboxbuttons.ok, messageboxicon.exclamation); } else { networkstream netstream = _tcpc.getstream(); //向服务器发送用户名以确认身份 netstream.write(encoding.unicode.getbytes(user_tb.text), 0, encoding.unicode.getbytes(user_tb.text).length); //得到登录结果 byte[] buffer = new byte[50]; netstream.read(buffer, 0, buffer.length); string connresult = encoding.unicode.getstring(buffer).trimend( '\0 '); if (connresult.equals( "cmd::failed ")) { messagebox.show( "您的用户名已经被使用,请尝试其他用户名! ", "提示 ", messageboxbuttons.ok, messageboxicon.information); return; } else { string svrskt = svrip_tb.text + ": " + svrport_tb.text; chat_frm chatfrm = new chat_frm(user_tb.text, netstream, svrskt); chatfrm.owner = this; this.hide(); chatfrm.show(); } } } /// <summary> /// 初始化登录信息 /// </summary> /// <param name= "sender "> </param> /// <param name= "e "> </param> private void login_frm_load(object sender, eventargs e) { svrip_tb.text = "127.0.0.1 "; svrport_tb.text = "8888 "; user_tb.focus(); } #endregion } } |
|
|
|
|