基本信息
源码名称:<赞>C# 实现 服务器端和客户端Socket通讯,聊天功能实现源码
源码大小:0.05M
文件格式:.zip
开发语言:C#
更新时间:2013-02-17
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
简单的实现了服务器和客户端的聊天功能
服务器端代码:
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } // 定义私有变量。 public bool m_bConnected = false; public IPEndPoint p = null; public Socket s = null; public NetworkStream ns = null; public TextReader r = null; public TextWriter w = null; public Thread t = null; public void ThreadListen() { Socket recv = null; recv = s.Accept(); if(recv != null) m_bConnected = true; ns = new NetworkStream(recv); r = new StreamReader(ns); w = new StreamWriter(ns); while(m_bConnected) { string tmp; try { tmp = r.ReadLine(); if(tmp.Length != 0) { lock(this) { richTextBox1.Text = "他说:" tmp "\n" richTextBox1.Text; } } } catch { MessageBox.Show("连接断开!!"); } } recv.Shutdown(SocketShutdown.Both); recv.Close(); s.Shutdown(SocketShutdown.Both); s.Close(); } private void button1_Click(object sender, System.EventArgs e) { p = new IPEndPoint(IPAddress.Any, 34567); s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.Blocking = true; s.Bind(p); s.Listen(0); t = new Thread(new ThreadStart(this.ThreadListen)); t.Start(); button1.Enabled = false; } private void richTextBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { byte[] data; if(e.KeyChar == (char)13) { if(m_bConnected) { try { lock(this) { richTextBox1.Text = "我说:" richTextBox2.Text richTextBox1.Text; w.WriteLine(richTextBox2.Text); w.Flush(); } } catch { MessageBox.Show("连接断开!!"); } } else { MessageBox.Show("对不起,还没有与对方连接,不能通信!"); } richTextBox2.Text = ""; richTextBox2.Focus(); } }
客户端代码:
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } // 定义私有变量。 public bool m_bConnected = false; public Thread t = null; public IPEndPoint p = null; public Socket s = null; public NetworkStream ns = null; public TextReader r = null; public TextWriter w = null; public void ThreadListen() { string tmp; while(m_bConnected) { try { tmp = r.ReadLine(); if(tmp.Length != 0) { lock(this) { richTextBox1.Text = "他说:" tmp "\n" richTextBox1.Text; } } } catch { MessageBox.Show("连接断开!!"); } } s.Shutdown(SocketShutdown.Both); s.Close(); } private void button1_Click(object sender, System.EventArgs e) { p = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text)); s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.Connect(p); if(s.Connected) { ns = new NetworkStream(s); r = new StreamReader(ns); w = new StreamWriter(ns); t = new Thread(new ThreadStart(this.ThreadListen)); t.Start(); m_bConnected = true; button1.Enabled = false; } } private void richTextBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(e.KeyChar == (char)13) { if(m_bConnected) { lock(this) { try { richTextBox1.Text = "我说:" richTextBox2.Text richTextBox1.Text; w.WriteLine(richTextBox2.Text); w.Flush(); } catch { MessageBox.Show("连接断开!!"); } } } else { MessageBox.Show("对不起,还没有与对方连接,不能通信!"); } richTextBox2.Text = ""; richTextBox2.Focus(); } }