Unity C# TCP通信与大文件传输

本文详细介绍了如何在Unity中使用C#进行TCP通信,并探讨了实现大文件传输的策略和技巧,包括服务器和客户端的实现细节。
摘要由CSDN通过智能技术生成

Unity C# TCP通信与大文件传输
Sever:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Windows.Forms;

namespace SocketServer
{
    class Program
    {
        public static string serverIp = "127.0.0.1";//设置服务端IP
        public static int serverPort = 8888;//服务端端口

        [STAThread]
        static void Main(string[] args)
        {
            //string path= @"..\ipconfig.txt";
            //string[] strs = File.ReadAllLines(path);//将filename路径的txt读取为string数组
           // Console.WriteLine(strs[0]);
            //serverIp = strs[0];//设置服务端IP
            //serverPort =int .Parse ( strs[1]);//服务端端口
            SocketServer();
        }
        public static Socket socketServer;//定义socket
        public static Thread threadWatch;//定义线程
        public static byte[] result = new byte[1024 * 1024 * 2];//定义缓存
        //public static string fileName;//获取文件名
        public static string filePath = "";//存储保存文件的路径
        public static void SocketServer()
        {
            socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个socket的对象
            IPAddress ip = IPAddress.Parse(serverIp);//获取服务器IP地址
            IPEndPoint point = new IPEndPoint(ip, serverPort);//获取端口
            try
            {
                socketServer.Bind(point);//绑定IP地址及端口
            }
            catch (Exception ex)
            {
                Console.WriteLine("绑定IP时出现异常:" + ex.Message);
                return;
            }
            socketServer.Listen(100);//开启监听并设定最多10个排队连接请求
            threadWatch = new Thread(WatchConnect);//创建一个监听进程
            threadWatch.IsBackground = true;//后台启动
            threadWatch.Start();//运行
            Console.WriteLine("服务器{0}监听启动成功!", socketServer.LocalEndPoint.ToString());
            Console.ReadLine();
        }
        public static void WatchConnect()
        {
            while (true)
            {
                Socket watchConnect = socketServer.Accept();//接收连接并返回一个新的Socket
                watchConnect.Send(Encoding.UTF8.GetBytes("服务器连接成功"));//在客户端显示"服务器连接成功"提示
                Thread threadwhat = new Thread(ReceiveMsg);//创建一个接受信息的进程
                threadwhat.IsBackground = true;//后台启动
                threadwhat.Start(watchConnect);//有传入参数的线程
            }
        }
        public static DateTime GetTime()
        {
            DateTime now = new DateTime();
            now = DateTime.Now;
            return now;
        }
        public static  void showproc(string headstr, float proc)
        {
            string a = headstr;
            for (int i = 1; i <= (10 - proc.ToString().Length); i+
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值