首页 > 其他分享 >unity 局域网内传送照片

unity 局域网内传送照片

时间:2022-12-14 17:36:12浏览次数:39  
标签:传送 socket void System private 局域网 unity new using

发送的电脑

using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System;

public class SendPhoto:MonoBehaviour {

    private Socket socket = null;

    private IPEndPoint endPoint = null;
    void Start() {
        InitSocketEnv();
    }
//绑定在发送按钮上
    public void SendMegEvent() {
        SendPhotoMessage("123.jpg");
    }

    void SendPhotoMessage(string fileName) {

        string path = @"D:\test\";
        print(path + fileName);
        FileStream fs = new FileStream(path + fileName,FileMode.OpenOrCreate,FileAccess.Read);
        BinaryReader strread = new BinaryReader(fs);
        byte[] byt = new byte[fs.Length];
        strread.Read(byt,0,byt.Length - 1);


        socket.Send(byt);
        fs.Close();
        socket.Close();
    }


    void InitSocketEnv() {
        socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
        //接收电脑的IP
        endPoint = new IPEndPoint(IPAddress.Parse("192.168.0.76"),9001);
        socket.Connect(endPoint);
    }

    void OnDestory() {
        socket.Close();
    }
}

//发送的电脑

using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using System;

public class RecivePhoto:MonoBehaviour {

    private Socket m_socket = null;
    private IPEndPoint m_ipEp = null;

    private Thread m_thread = null;
    private bool isRunningThread = false;

    private Queue<byte[]> m_queue;

    void Start() {
        m_queue = new Queue<byte[]>();
        InitSocketEnv();
    }

    // Update is called once per frame
    void Update() {
        if(m_queue.Count > 0) {
            Debug.Log(m_queue.Count);

            byte[] temp = m_queue.Dequeue();

            FileStream fs = File.Create("D:/test/123.png");
            fs.Write(temp,0,temp.Length);
            fs.Close();
        }


    }

    void ReciveMeg() {
        while(isRunningThread) {
            Socket socket = m_socket.Accept();
            //1M的大小
            byte[] buffer = new byte[1024*1024];
            socket.Receive(buffer,buffer.Length,SocketFlags.None);

            m_queue.Enqueue(buffer);

            socket.Close();
        }

        Debug.Log("stop");
    }

    void InitSocketEnv() {
        m_socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
        //接收电脑的IP
        m_ipEp = new IPEndPoint(IPAddress.Parse("192.168.0.166"),9001);
        m_socket.Bind(m_ipEp);
        m_socket.Listen(5);

        isRunningThread = true;
        m_thread = new Thread(ReciveMeg);
        m_thread.Start();
    }

    void OnDistory() {
        isRunningThread = false;
        m_socket.Close();
    }
}

 

标签:传送,socket,void,System,private,局域网,unity,new,using
From: https://www.cnblogs.com/zqiang0803/p/16982724.html

相关文章

  • CommunityToolkit.Mvvm
    publicclassViewModel:ObservableObject{publicViewModel(){ShowCommand=newRelayCommand<string>(Show);}......
  • .NetCore【工作应用】Unity
    UnityUnity是一个IoC容器,用来实现依赖注入(DependencyInjection,DI),减少耦合Unity安装install-packageUnity使用IUnityContainercontainer=newUnity......
  • 局域网 大文件分片上传处理
    ​ HTML部分 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="up6.index" %><!DOCTYPE html PUBLIC "-//W3C//DTDXH......
  • 【Unity Shader】Special Effects(二)BorderFlow 边框流动(UI)
    更新日期:2021年8月23日。Github源码:​​​[点我获取源码]​​索引​​BorderFlow边框流动​​​​思路分析​​​​流光区域​​​​流光区域的中心点​​​​流光区域的......
  • Unity插件 - MeshEditor(五) 网格顶点动画(变形动画)
        网格顶点动画(变形动画)是针对于物体的形状可以随意变换并记录为关键帧的动画,虽然模型的顶点数据还是应该交给GPU绘制才是正道,CPU刷新模型顶点始终是个吃力不讨好的事......
  • 【Unity】 HTFramework框架(四十)Debug的性能监控
    更新日期:2021年4月22日。Github源码:​​​[点我获取源码]​​​Gitee源码:​​[点我获取源码]​​索引​​C#代码性能监控​​​​使用​​​​Debug的性能监控模式​​​......
  • 【Unity】 HTFramework框架(四十二)【进阶篇】使用依赖注入(控制反转模式)
    更新日期:2022年1月4日。Github源码:​​​[点我获取源码]​​​Gitee源码:​​[点我获取源码]​​索引​​依赖注入​​​​使用​​​​InjectPath​​​​InjectUI​​​......
  • Unity UGUI图文混排源码(一)
    我从一开始想到的图文混排的概念都是通过文字间的空隙去粘贴一张图片,这样确定图片前面文字的最后一个位置变成了最主要的参数,接下来就给出两种解决方案首先,先发UGUI源码的一......
  • Unity UGUI基础之Text
    Text作为UGUI最基础的控件以及最常用的控件,它在项目中的应用绝对可以算是最多的,任何一个UI界面可以说都离不开它,它的基本属性如下:一、recttransform组件:recttransform(矩形......
  • Unity UGUI实现图文混排
    目前在unity实现图文混排的好像都是通过自定义字体然后在文本获取字符的位置,用图片替换掉图片标签,这样对于支持英文来说,并没有什么影响。然后对于中文来说就是一个相当麻烦......