您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 广告经营 > 7简单的C#聊天程序 课程设计
简单的c#聊天程序--课程设计参照课本:通过面向套接字,在两个设备之间建立连接源文件见360云盘分享链接访问密码9e93使用说明开发软件visualstudio2015(其他版本可能打不开)运行环境.NET4.6.1直接打开dns开发2\dns开发2\bin\Debug\xxx.exedns2为服务端运行程序dns3微客户端运行程序ip设置为本机ip才能运行端口设置为不常用端口例如ip192.168.1.111端口5000使用截图:1.打开程序(两个窗体)2.建立连接输入ip192.168.1.111端口5000昵称服务器,客户机点允许访问3.完成测试通信代码设计服务器端usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;//添加引用usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Threading;namespacedns开发2{publicpartialclassForm1:Form{privateIPAddressmyIP=IPAddress.Parse(127.0.0.1);privateIPEndPointMyserver;privateSocketseSock;privateboolflag=true;privateSocketreSock;publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){try{myIP=IPAddress.Parse(textBox1.Text);}catch{MessageBox.Show(你输入的IP地址格式不正确,请重新输入!);}try{Myserver=newIPEndPoint(myIP,Int32.Parse(this.textBox2.Text));seSock=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);seSock.Bind(Myserver);seSock.Listen(50);this.toolStripStatusLabel1.Text=主机+this.textBox1.Text+端口+this.textBox2.Text+开始监听...;reSock=seSock.Accept();Threadthread=newThread(newThreadStart(targett));thread.Start();}catch(Exceptionex){MessageBox.Show(ex.Message);}}privatevoidtargett(){Control.CheckForIllegalCrossThreadCalls=false;if(reSock.Connected){this.toolStripStatusLabel1.Text=与客户端建立连接。;while(flag){Byte[]r_data=newByte[64];reSock.Receive(r_data,r_data.Length,0);stringstr=System.Text.Encoding.BigEndianUnicode.GetString(r_data);this.richTextBox1.AppendText(str+\r\n);}}}privatevoidbutton2_Click(objectsender,EventArgse){try{Byte[]s_data=newByte[64];stringsend=this.textBox3.Text+---+this.richTextBox2.Text+\r\n;s_data=System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());reSock.Send(s_data,s_data.Length,0);}catch{MessageBox.Show(链接未建立,无法发送!);}this.textBox2.Clear();}privatevoidbutton3_Click(objectsender,EventArgse){try{seSock.Close();this.toolStripStatusLabel1.Text=主机+this.textBox1.Text+端口+this.textBox2.Text+停止监听;}catch{MessageBox.Show(监听尚未开始,关闭无效);}}privatevoidbutton4_Click(objectsender,EventArgse){this.richTextBox1.Clear();}}}客户机usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;//添加引用usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Threading;namespacedns开发3{publicpartialclassForm1:Form{//添加私有成员privateIPAddressmyIP=IPAddress.Parse(127.0.0.1);privateIPEndPointMyServer;privateSocketsock;privateboolflag=true;publicForm1(){InitializeComponent();}privatevoidlabel3_Click(objectsender,EventArgse){}privatevoidbutton1_Click(objectsender,EventArgse){try{myIP=IPAddress.Parse(this.textBox1.Text);}catch{MessageBox.Show(你输入的IP地址格式不正确,请重新输入!);}try{MyServer=newIPEndPoint(myIP,Int32.Parse(this.textBox2.Text));sock=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);sock.Connect(MyServer);this.toolStripStatusLabel1.Text=与主机+this.textBox1.Text+端口+this.textBox2.Text+连接成功;Threadthread=newThread(newThreadStart(targett));thread.Start();}catch(Exceptioneee){MessageBox.Show(eee.Message);}}privatevoidtargett(){Control.CheckForIllegalCrossThreadCalls=false;while(flag){Byte[]r_date=newByte[64];sock.Receive(r_date,r_date.Length,0);stringstr=System.Text.Encoding.BigEndianUnicode.GetString(r_date);this.richTextBox1.AppendText(str);}}privatevoidbutton2_Click(objectsender,EventArgse){try{Byte[]s_data=newByte[64];stringsend=this.textBox3.Text+---+this.richTextBox2.Text+\r\n;s_data=System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());sock.Send(s_data,s_data.Length,0);}catch{MessageBox.Show(链接尚未建立,无法发送!);}}privatevoidbutton3_Click(objectsender,EventArgse){try{sock.Close();this.toolStripStatusLabel1.Text=与主机+this.textBox1.Text+端口+this.textBox2.Text+断开连接!;}catch{MessageBox.Show(链接尚未建立!断开无效!);}}privatevoidbutton4_Click(objectsender,EventArgse){this.richTextBox1.Clear();}}}
本文标题:7简单的C#聊天程序 课程设计
链接地址:https://www.777doc.com/doc-3403425 .html