赞
踩
不熟悉TCP/UDP协议的同学可以参考这篇文章:http://blog.csdn.net/li_ning_/article/details/52117463
话不多少说,服务器端代码如下:
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
-
- public class Server : MonoBehaviour {
-
- private UdpClient udpClient;
-
- private bool isRunningThread;
-
- private Thread thread_udpRecv;
-
- private Queue<string> queue_udpMessage;
-
- // Use this for initialization
- void Start () {
- queue_udpMessage = new Queue<string>();
- CreateUdpListener();
- }
-
- // Update is called once per frame
- void Update () {
- if(queue_udpMessage.Count > 0)
- {
- string cmd = queue_udpMessage.Dequeue();
- print(cmd);
-
- DoByCommand(cmd);
- }
- }
-
- void OnDisable()
- {
- isRunningThread = false;
- udpClient.Close();
- }
-
- private void CreateUdpListener()
- {
- isRunningThread = true;
-
- udpClient = new UdpClient(ConfiInfo.UdpPort);
- thread_udpRecv = new Thread(ReciveMessages);
- thread_udpRecv.Start();
- }
-
- private void ReciveMessages()
- {
- while(isRunningThread)
- {
- try
- {
- IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Any, ConfiInfo.UdpPort);
- byte[] bytRecv = udpClient.Receive(ref remoteIpep);
- string message = System.Text.Encoding.Default.GetString(bytRecv, 0, bytRecv.Length);
-
- queue_udpMessage.Enqueue(message);
- }
- catch(System.Exception ex)
- {
- print(ex.Message);
- break;
- }
- }
- }
-
- private void DoByCommand(string cmd)
- {
- //string tmp = cmd.ToLower();
-
- switch(cmd)
- {
- case "TransPortPhoto": ReceivePhoto(); break; //图片数据流的接收并显示
- }
- }
-
- private void ReceivePhoto()
- {
- Debug.Log("显示图片!");
- }
- }
客户端代码如下:
- using UnityEngine;
- using System.Collections;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.IO;
- using System;
-
- public class Client : MonoBehaviour
- {
- [SerializeField]
- private string fileName;
-
- private string pathPhoto;
-
- private Socket socket;
- private IPEndPoint serverPoint;
-
- // Use this for initialization
- void Start()
- {
- pathPhoto = Application.dataPath + ConfiInfo.pathPhoto;
-
- CreateCilent();
- }
-
- // Update is called once per frame
- void Update()
- {
-
- }
-
- void OnDestory()
- {
- socket.Close();
- }
-
- public void TransPortPhotoMessage() //发送数据
- {
- string pathFile = pathPhoto + fileName;
- TcpSendFileStream(pathFile);
-
- UdpSendMessage("TransPortPhoto");
- }
-
- private void TcpSendFileStream(string pathFile) //发送文件数据流
- {
- print("EnterUdpSendFileStream");
-
-
-
- print("UdpSendFileStreamEnd");
- }
-
- private void UdpSendMessage(string message) //发送字符串指令
- {
- try
- {
- byte[] bytBuffer = Encoding.UTF8.GetBytes(message);
- socket.SendTo(bytBuffer, serverPoint);
- }
- catch (Exception e)
- {
- Debug.Log(e.Message);
- }
-
- print(message);
- }
-
- private void CreateCilent()
- {
- socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- serverPoint = new IPEndPoint(IPAddress.Parse(ConfiInfo.serverIP), ConfiInfo.UdpPort);
- socket.Connect(serverPoint);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。