当前位置:   article > 正文

Unity UDP协议_unity udp_cmd

unity udp_cmd

不熟悉TCP/UDP协议的同学可以参考这篇文章:http://blog.csdn.net/li_ning_/article/details/52117463

话不多少说,服务器端代码如下:

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. public class Server : MonoBehaviour {
  8. private UdpClient udpClient;
  9. private bool isRunningThread;
  10. private Thread thread_udpRecv;
  11. private Queue<string> queue_udpMessage;
  12. // Use this for initialization
  13. void Start () {
  14. queue_udpMessage = new Queue<string>();
  15. CreateUdpListener();
  16. }
  17. // Update is called once per frame
  18. void Update () {
  19. if(queue_udpMessage.Count > 0)
  20. {
  21. string cmd = queue_udpMessage.Dequeue();
  22. print(cmd);
  23. DoByCommand(cmd);
  24. }
  25. }
  26. void OnDisable()
  27. {
  28. isRunningThread = false;
  29. udpClient.Close();
  30. }
  31. private void CreateUdpListener()
  32. {
  33. isRunningThread = true;
  34. udpClient = new UdpClient(ConfiInfo.UdpPort);
  35. thread_udpRecv = new Thread(ReciveMessages);
  36. thread_udpRecv.Start();
  37. }
  38. private void ReciveMessages()
  39. {
  40. while(isRunningThread)
  41. {
  42. try
  43. {
  44. IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Any, ConfiInfo.UdpPort);
  45. byte[] bytRecv = udpClient.Receive(ref remoteIpep);
  46. string message = System.Text.Encoding.Default.GetString(bytRecv, 0, bytRecv.Length);
  47. queue_udpMessage.Enqueue(message);
  48. }
  49. catch(System.Exception ex)
  50. {
  51. print(ex.Message);
  52. break;
  53. }
  54. }
  55. }
  56. private void DoByCommand(string cmd)
  57. {
  58. //string tmp = cmd.ToLower();
  59. switch(cmd)
  60. {
  61. case "TransPortPhoto": ReceivePhoto(); break; //图片数据流的接收并显示
  62. }
  63. }
  64. private void ReceivePhoto()
  65. {
  66. Debug.Log("显示图片!");
  67. }
  68. }

客户端代码如下:

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.IO;
  7. using System;
  8. public class Client : MonoBehaviour
  9. {
  10. [SerializeField]
  11. private string fileName;
  12. private string pathPhoto;
  13. private Socket socket;
  14. private IPEndPoint serverPoint;
  15. // Use this for initialization
  16. void Start()
  17. {
  18. pathPhoto = Application.dataPath + ConfiInfo.pathPhoto;
  19. CreateCilent();
  20. }
  21. // Update is called once per frame
  22. void Update()
  23. {
  24. }
  25. void OnDestory()
  26. {
  27. socket.Close();
  28. }
  29. public void TransPortPhotoMessage() //发送数据
  30. {
  31. string pathFile = pathPhoto + fileName;
  32. TcpSendFileStream(pathFile);
  33. UdpSendMessage("TransPortPhoto");
  34. }
  35. private void TcpSendFileStream(string pathFile) //发送文件数据流
  36. {
  37. print("EnterUdpSendFileStream");
  38. print("UdpSendFileStreamEnd");
  39. }
  40. private void UdpSendMessage(string message) //发送字符串指令
  41. {
  42. try
  43. {
  44. byte[] bytBuffer = Encoding.UTF8.GetBytes(message);
  45. socket.SendTo(bytBuffer, serverPoint);
  46. }
  47. catch (Exception e)
  48. {
  49. Debug.Log(e.Message);
  50. }
  51. print(message);
  52. }
  53. private void CreateCilent()
  54. {
  55. socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  56. serverPoint = new IPEndPoint(IPAddress.Parse(ConfiInfo.serverIP), ConfiInfo.UdpPort);
  57. socket.Connect(serverPoint);
  58. }
  59. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/114143
推荐阅读
相关标签
  

闽ICP备14008679号