实例介绍
【实例简介】
【实例截图】
【核心代码】
using System; using System.Net; using System.Net.Sockets; using System.Speech.Synthesis; using System.Text; using System.Threading; using System.Windows.Forms; namespace CallServer { public partial class Form1 : Form { public Form1() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; } /// <summary> /// 等待客户端的连接 并且创建与之通信的Socket /// </summary> Socket socketSend; void Listen(object o) { try { Socket socketWatch = o as Socket; while (true) { socketSend = socketWatch.Accept();//等待接收客户端连接 ShowMsg(socketSend.RemoteEndPoint.ToString() ":" "连接成功!"); //开启一个新线程,执行接收消息方法 Thread r_thread = new Thread(Received); r_thread.IsBackground = true; r_thread.Start(socketSend); } } catch { } } /// <summary> /// 服务器端不停的接收客户端发来的消息 /// </summary> /// <param name="o"></param> void Received(object o) { try { Socket socketSend = o as Socket; while (true) { //客户端连接服务器成功后,服务器接收客户端发送的消息 byte[] buffer = new byte[1024 * 1024 * 3]; //实际接收到的有效字节数 int len = socketSend.Receive(buffer); if (len == 0) { break; } string str = Encoding.UTF8.GetString(buffer, 0, len); ShowMsg(socketSend.RemoteEndPoint ":" str); //叫号 SpeakStr(str); } } catch { } } /// <summary> /// 服务器向客户端发送消息 /// </summary> /// <param name="str"></param> void Send(string str) { byte[] buffer = Encoding.UTF8.GetBytes(str); socketSend.Send(buffer); } StringBuilder buf = new StringBuilder(); void ShowMsg(string msg) { buf.Append(msg); buf.Append("\n\r"); this.txtContent.Text = buf.ToString(); } private void SpeakStr(string content) { //方式一 //SpeechVoiceSpeakFlags flag = SpeechVoiceSpeakFlags.SVSFlagsAsync; //SpVoice voice = new SpVoice(); //var aa = voice.GetVoices(string.Empty, string.Empty); //voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0); //voice.Rate = 1;//阅读速度 //方式二 SpeechSynthesizer hello = new SpeechSynthesizer(); //string str = "请A001号到第一检查室检查"; hello.Volume = 10; hello.Rate = -5;//-10-10,值越小,语速越慢 hello.Speak(content); //Speak(string),Speak加上字符串类型的参数 //Item(0)单词男声Sam //Item(1)单词男声Mike //Item(2)单词女声Mary //Item(3)中文发音,如果是英文,就依单词字母一个一个发音 //voice.Speak(content, flag); } private void btnSend_Click(object sender, EventArgs e) { Send(this.txtSendMessage.Text.Trim() "\n"); } private void btnStartListener_Click(object sender, EventArgs e) { CreateListener(); } private void CreateListener() { try { //点击开始监听时 在服务端创建一个负责监听IP和端口号的Socket Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Any; //创建对象端口 IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text)); socketWatch.Bind(point);//绑定端口号 ShowMsg("监听成功!"); socketWatch.Listen(10);//设置监听 //创建监听线程 Thread thread = new Thread(Listen); thread.IsBackground = true; thread.Start(socketWatch); } catch { } } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论