日期:2014-05-17 浏览次数:20441 次
//连接客户端 private void SendSocket() { labmsg.InnerText = "正在建立连接"; //连接 socket = new AsySocket(Cip, port); socket.OnSended += new AsySocketEventHandler(socket_OnSended); socket.OnSendTo += new AsySocketEventHandler(socket_OnSendTo); socket.OnStringDataAccept += new StringDataAcceptHandler(socket_OnStringDataAccept); socket.OnClosed += new AsySocketClosedEventHandler(socket_OnClosed); try { socket.LinkObject.Connect(Cip, port); } catch { labmsg.InnerText = "连接失败"; return; } labmsg.InnerText = "连接成功"; socket.BeginAcceptData(); } void socket_OnSended(string SenderID, string EventMessage) { labcontent.Text += "<br/>我:"+txtcontent.Value.Trim(); } void socket_OnClosed(string SocketID, string ErrorMessage) { //服务器关闭 Page.ClientScript.RegisterClientScriptBlock(GetType(), "key", "<script>alert('服务器关闭')</script>"); } void socket_OnSendTo(string SenderID, string EventMessage) { throw new Exception("The method or operation is not implemented."); } void socket_OnStringDataAccept(string AccepterID, string AcceptData) { //AddMsg(AcceptData); pmsg += AcceptData.ToString() + "<br/>"; labcontent.Text += AcceptData.ToString(); Page.ClientScript.RegisterStartupScript(GetType(), "key", "<script>almsg('"+AcceptData.ToString()+"');</script>"); } private void AddMsg(string msg) { labcontent.Text += Environment.NewLine + msg; } //发送信息 protected void btnsend_Click(object sender, EventArgs e) { socket.ASend("Me:" + txtcontent.Value.Trim() + AsySocket.EndChar); }