当前位置: 首页 > news >正文

手机怎么做微电影网站php+做网站

手机怎么做微电影网站,php+做网站,小程序模板指令,影视网站cpa 如何做需要提前了解的Socket知识点#xff1a; Client端输入的IP都是Server所在电脑的IPServer最好设置0.0.0.0这样无论迁移到哪个电脑上#xff0c;都是那台电脑的IPClient和Server必须在同一个局域网之下#xff0c;否则不能通信#xff1b;如果要实现跨局域网通信#xff0c…需要提前了解的Socket知识点 Client端输入的IP都是Server所在电脑的IPServer最好设置0.0.0.0这样无论迁移到哪个电脑上都是那台电脑的IPClient和Server必须在同一个局域网之下否则不能通信如果要实现跨局域网通信需要使用公网IP通过阿里云等 整个聊天室的代码分为两个模块 Server服务器模块只能有一个用来存储监听用户发送的内容Client客户模块可以有多个各个用户直接进行收发信息 以下是Server代码 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks;namespace Server {class Program{static Socket socket null;static void Main(string[] args){socket new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);socket.Bind(new IPEndPoint(IPAddress.Parse(0.0.0.0), 60000));socket.Listen(10);Console.WriteLine(已启动侦听);Task.Run(Connect);Console.ReadLine();}static ListSocket socketPool new ListSocket();static Socket acceptSocket null;static void Connect(){while (true){try{acceptSocket socket.Accept();var receiveSocket acceptSocket;Console.WriteLine($已接受连接{receiveSocket.RemoteEndPoint});socketPool.Add(receiveSocket);Task.Run(Receive);}catch (Exception ex){Console.WriteLine($连接失败{ex.Message});}}}static void Receive(){var receiveSocket acceptSocket;while (true){if (receiveSocket null) continue;if (!receiveSocket.Connected) continue;try{byte[] buffer new byte[1024];var len receiveSocket.Receive(buffer);if (len 1) continue;var msg Encoding.UTF8.GetString(buffer, 0, len);Console.WriteLine($来自 {receiveSocket.RemoteEndPoint} 的消息 {msg});var responseBuffer Encoding.UTF8.GetBytes($来自{receiveSocket.RemoteEndPoint} 的消息{msg});// receiveSocket.Send(responseBuffer);//清理失效的连接for (int i socketPool.Count - 1; i 0; i--){if (socketPool[i] null || !socketPool[i].Connected){socketPool.RemoveAt(i);}}//广播消息除了发送消息方之外的全部广播foreach (var s in socketPool){if (s ! receiveSocket){s.Send(responseBuffer);}}buffer null;}catch (Exception ex){Console.WriteLine(ex.Message \n);}}}} }以下是Client代码 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;/// summary /// 客户端 /// /summary namespace WpfApp1 {/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{Socket socket null;public MainWindow(){InitializeComponent();btnConnect.Click btnConnect_Click;btnSend.Click btnSend_Click;Task.Run(Receive);}private void btnConnect_Click(object sender, RoutedEventArgs e){if (socket ! null socket.Connected){ShowMessage($无需重复连接);return;}try{socket new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);ShowMessage($LocalEndPoint{socket.LocalEndPoint},RemoteEndPoint{socket.RemoteEndPoint});var ipEndPoint new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text));socket.Connect(ipEndPoint);if (socket.Connected){ShowMessage($成功连接到{socket.RemoteEndPoint});}}catch (Exception ex){ShowMessage($连接失败{ex.Message});socket null;}}private void btnSend_Click(object sender, RoutedEventArgs e){if (!socket.Connected){ShowMessage($未连接无法发送);return;}try{if (rtxtSend.Text ! ){var buffer Encoding.UTF8.GetBytes(rtxtSend.Text);socket.Send(buffer);ShowMessage($发送到{socket.RemoteEndPoint},消息{rtxtSend.Text});}rtxtSend.Text null;}catch (Exception ex){ShowMessage($发送失败{socket.RemoteEndPoint},{ex.Message});socket null;}}private void Receive(){while (true){if (socket null) continue;if (!socket.Connected) continue;try{byte[] buffer new byte[1024];if (buffer ! null){int len socket.Receive(buffer);if (len 1) continue;string msg Encoding.UTF8.GetString(buffer, 0, len);ShowMessage(msg);}buffer null;}catch (Exception ex){ShowMessage(ex.Message \n);socket null;}}}private void ShowMessage(string msg){///这是Winform的用法//子线程调用 //if (rtxtLog.InvokeRequired) //c#中禁止跨线程直接访问控件InvokeRequired是为了解决这个问题而产生的//{// rtxtLog.Invoke(new Action(() rtxtLog.Text msg \n));//}//主线程调用//else//{// rtxtLog.Text msg \n;//}if (!CheckAccess()) //WPF使用Dispatcher控制对消息泵的访问而不是让每个控件负责访问UI线程。{Dispatcher.Invoke(new Action(() rtxtLog.Text msg \n));}//主线程调用else{rtxtLog.Text msg \n;}}} }
http://www.zqtcl.cn/news/109029/

相关文章:

  • 站长素材音效下载重庆市建设工程信息网综合网
  • 网络营销专业就业方向wordpress插件dx-seo
  • 义乌有什么企业网站吗大良企业网站建设
  • 外链网站推荐几个html5标准网站建设
  • 博星卓越营销网站设计古镇镇建网站公司
  • 3. 是网站建设的重点网站后台添加关键词
  • 免费手机网站模板下载百度网站建设北京
  • 爱用建站建设网站的报告
  • 网站关键字被百度收录网站开发asp.net
  • 内部卷网站怎么做的h5在线编辑器
  • 搞定设计网站做的图怎么下载不了网站死链接怎么删除
  • 济南企业建站怎么样在pc端预览手机网站
  • c 手机网站开发模板最好的建站网站
  • 防城港市网站建设成功网站建设案例
  • 成都公司网站制作公司实验一 电子商务网站建设与维护
  • 即墨区城乡建设局网站300m空间够用吗 wordpress
  • 成都软件开发培训机构7个湖北seo网站推广策略
  • 嘉定企业网站建设深圳网站制作费用
  • 外贸网站有必要吗wordpress远程保存图片
  • 苏州吴中网站建设wordpress中文版安装教程
  • wordpress 网站静态页面赶集网网站建设分析
  • 伊春网站开发大型网站建设兴田德润专业
  • 温州平阳县营销型网站建设榆林做网站
  • 沽源网站建设娄底网站建设工作室
  • 商场网站 策划医疗网站是否全部需要前置备案
  • 电商网站开发实训心得wordpress网络验证
  • 美图网seo 优化技术难度大吗
  • 知名的传媒行业网站开发天空网站开发者
  • 网站域名年费多少钱二手手表交易平台哪个好
  • 用易语言做抢购网站软件下载自己可以做企业网站吗