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

兰州网站建设怎么选珠海做网站公司

兰州网站建设怎么选,珠海做网站公司,网络科技工作室起名,举报网站平台因为需要用到unity和其他的程序交互,其他程序可以提供Udp消息,因此找了合适的相互连接方法。这里直接上代码。 工具类: using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Net; u…

因为需要用到unity和其他的程序交互,其他程序可以提供Udp消息,因此找了合适的相互连接方法。这里直接上代码。

工具类:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;
using UnityEngine.Video;public class UdpManager
{public static string m_receivedMessage;static IPEndPoint m_IPEndPoint;static UdpClient m_udpClient;public static void InitializeUdpClient(){m_IPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);m_udpClient = new UdpClient(m_IPEndPoint);UdpModel s = new UdpModel(m_udpClient, m_IPEndPoint);m_udpClient.BeginReceive(EndReceive, s);Debug.Log("服务器启动");}//结束得到的信息private static void EndReceive(IAsyncResult ar){try{UdpModel m_UdpModel = ar.AsyncState as UdpModel;if (m_UdpModel != null){UdpClient udpClient = m_UdpModel.m_udpclient;IPEndPoint ip = m_UdpModel.m_ip;Byte[] receiveBytes = udpClient.EndReceive(ar, ref ip);string msg = Encoding.UTF8.GetString(receiveBytes);m_receivedMessage = msg;udpClient.BeginReceive(EndReceive, m_UdpModel); //开始获取}}catch (Exception ex){//处理异常}}//udp模型private class UdpModel{public UdpClient m_udpclient = null;public IPEndPoint m_ip;public UdpModel(UdpClient udpclient, IPEndPoint ip){this.m_udpclient = udpclient;this.m_ip = ip;}}//关闭public static void Close(){if (m_udpClient != null){m_udpClient.Close();m_udpClient = null;}}/// <summary>/// 发送数据/// </summary>/// <param name="obj"></param>public static void SendMessage(string message){UdpClient myUdpClient = new UdpClient();IPEndPoint endpoint;//当前服务器ip和端口号myUdpClient = new UdpClient(new IPEndPoint(IPAddress.Any, 8800));//要发送给的地址和端口号,255.255.255.255表示在这个局域网的所有ipendpoint = new IPEndPoint(IPAddress.Parse("192.168.31.174"), 1180);byte[] bytes = Encoding.UTF8.GetBytes(message);try{myUdpClient.Send(bytes, bytes.Length, endpoint);myUdpClient.Close();}catch (Exception err){Console.Write(err.Message, "发送失败");}finally{myUdpClient.Close();}}
}

需要挂载运行的脚本:

using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.Video;/// <summary>
/// 服务接收生成
/// </summary>
public class ServerControl : MonoBehaviour
{void Start(){UdpManager.InitializeUdpClient();//part1Root.SetActive(true);//part2Root.SetActive(false);}void Update(){if (UdpManager.m_receivedMessage != null){string[] array = UdpManager.m_receivedMessage.Split(',');Debug.Log(UdpManager.m_receivedMessage);UdpManager.m_receivedMessage = null;}}private void OnDestroy(){UdpManager.Close();}
}

使用方法很简单,把ServerControl脚本挂载在一个物体上,直接运行即可,接受信息的方法和发送的方法都在两个脚本里。

http://www.yayakq.cn/news/768448/

相关文章:

  • 线上p2p网站建设手机网站横向切换
  • 网站需求清单动态的网站大概多少钱
  • 网站流量100g网站建设 应酷
  • 建设门户网站的意见和建议网易企业邮箱怎么找回密码
  • 番禺区pc端网站建设公众号链接的手机网站怎么做的
  • 百度一下你就知道官方网站网站app封装怎么做
  • 怎么做专题网站注册网站后邮箱收到邮件
  • 陕西省建设监理协会网站证件查询品牌网站建设新闻
  • 苏州建设网站首页wordpress用ip访问
  • 郑州定制网站推广工具产品知名网站开发公司
  • 网站开发的优势wordpress 糗百
  • 万网是做网站的吗黄冈网站建设哪家专业
  • 名片在哪个网站做改图网在线制作图片
  • 商城类网站如何众筹网上营销推广
  • 企业高端网站建设美工闵行区网站设计
  • 网站界面风格游戏网站wordpress
  • 广州网站建设 乐云seo效果好网站建设哪家便宜
  • 卖建材的网站有哪些设计网站教程
  • 网站的宣传方法有哪些wordpress原图片删除
  • 企业网站建设宣贯网站开发属于IT行业
  • 茂名手机网站建设公司名录益阳网站建设益阳
  • 网站速度慢如何做优化手游网站开发
  • nodejs可以做网站么外包网易
  • 瑞华特散热器网站谁给做的wordpress文章更多
  • 网站宽度 超宽html网页制作成品
  • 蚌埠建设学校网站教育学校江西建设信息港网站
  • 许昌住房建设局网站丰都网站建设
  • wordpress可以建什么站工行gcms系统
  • 博客网站主页代码html网站发产品ps怎么做产品图
  • 卧龙区网站建设哪家好建一个国外的网站