概述
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Main : MonoBehaviour {
private void Start()
{
NetConnectManager.GetInstance().ConnectNetWork();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NetConnectManager : MonoBehaviour {
private static NetConnectManager instance;
public static NetConnectManager GetInstance()
{
return instance;
}
public string ip = "192.168.18.113";
public int port = 8081;
public NetWork NetWork;
private void Awake()
{
instance = this;
}
public void ConnectNetWork()
{
NetWork.RequestNetConnect();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NetWork : MonoBehaviour, ISocketStateListener
{
private bool mIsRequestNetConnect;
private SocketConnect mSocketConnect;
public void RequestNetConnect()
{
mSocketConnect = new SocketConnect();
mSocketConnect.InitSocketConnect();
mIsRequestNetConnect = true;
}
private void Update()
{
if (mIsRequestNetConnect && mSocketConnect != null)
{
mIsRequestNetConnect = false;
string ip = NetConnectManager.GetInstance().ip;
int port = NetConnectManager.GetInstance().port;
mSocketConnect.ConnectServer(ip, port,6,this);
}
}
public void CloseConnect(object obj, string desc, int socketID)
{
}
public void CloseReceiveThread(object obj, string desc, int socketID)
{
}
public void CloseSendThread(object obj, string desc, int socketID)
{
}
public void Connected(object obj, string desc, int socketID)
{
Debug.Log("NetWork.Connected desc " + desc + " socketID " + socketID);
}
public void ConnectError(object obj, string desc, int socketID)
{
}
public void Connecting(object obj, string desc, int socketID)
{
Debug.Log("NetWork.Connecting desc " + desc + " socketID " + socketID);
}
public void CreateConnect(object obj, string desc, int socketID)
{
Debug.Log("NetWork.CreateConnect desc " + desc + " socketID " + socketID);
}
public void Ready(object obj, string desc, int socketID)
{
}
public void Receive(object obj, string desc, int socketID)
{
}
public void Send(object obj, string desc, int socketID)
{
}
public void StartReceiveThread(object obj, string desc, int socketID)
{
}
public void StartSendThread(object obj, string desc, int socketID)
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net.Sockets;
public class SocketConnect
{
public int SocketID { get; set; }
public int IDCounter { get; set; }
public TcpClient TcpNetClient { get; set; }
public ISocketStateListener SocketStateListener { get; set; }
public static UnityEngine.Object SyncLock = new UnityEngine.Object();
public ConnectorThread SocketConnectThread { get; set; }
public void InitSocketConnect()
{
}
public void ConnectServer(string ip, int port,int timeOut, ISocketStateListener socketStateListener)
{
SocketID = ++IDCounter;
SocketStateListener = socketStateListener;
SocketConnectThread = new ConnectorThread(ip,port, socketStateListener,this,null);
SocketConnectThread.TagID = SocketID;
SocketConnectThread.TagName = "Connector_Thread[SocketID:" + SocketID + "]";
SocketConnectThread.StartThread();
}
public bool IsConnected()
{
lock (SyncLock)
{
if (TcpNetClient != null)
{
return TcpNetClient.Connected;
}
return false;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Threading;
public class ProxyThread
{
public int ThreadID { get; set; }
public int TagID { get; set; }
public string TagName { get; set; }
public bool IsActive { get; set; }
protected Thread mThread { get; set; }
protected int mThreadPriority = -1;
public Action RunAction;
public virtual void StartThread()
{
if (mThread == null)
{
IsActive = true;
mThread = new Thread(ThreadProc);
if (mThreadPriority > 0)
{
mThread.Priority = (System.Threading.ThreadPriority)mThreadPriority;
}
ThreadID = mThread.ManagedThreadId;
mThread.Start(this);
}
}
protected static void ThreadProc(object obj)
{
ProxyThread thread = (ProxyThread)obj;
thread.RunThread();
if (thread.RunAction != null)
{
thread.RunAction();
}
thread.StopThread();
}
public virtual void RunThread()
{
}
public virtual void StopThread()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net.Sockets;
using System.Net;
using System;
public class ConnectorThread:ProxyThread
{
public string Ip { get; set; }
public int Port { get; set; }
public TcpClient TcpNetClient { get; set; }
public IPAddress IpAddress { get; set; }
public object Connect { get; set; }
public Action ErrorHandle { get; set; }
public bool IsCloseConnect { get; set; }
public ISocketStateListener SocketStateListener { get; set; }
public bool IsStopConnect { get; set; }
public ConnectorThread(string ip, int port, ISocketStateListener socketStateListener,object connect, Action erroHandle)
{
Ip = ip;
Port = port;
SocketStateListener = socketStateListener;
Connect = connect;
ErrorHandle = erroHandle;
}
public override void RunThread()
{
ConnectServer();
}
private void ConnectServer()
{
IPAddress ipAddress = IPAddress.None;
AddressFamily addressFamily = AddressFamily.InterNetwork;
IPAddress[] ipAddressArr = Dns.GetHostAddresses(Ip);
if (ipAddressArr.Length > 0)
{
ipAddress = ipAddressArr[0];
addressFamily = ipAddress.AddressFamily;
}
lock (SocketConnect.SyncLock)
{
if (!IsStopConnect)
{
IpAddress = ipAddress;
TcpNetClient = new TcpClient(addressFamily);
if (SocketStateListener != null)
{
SocketStateListener.CreateConnect(TcpNetClient,"create TcpClient",TagID);
}
(Connect as SocketConnect).TcpNetClient = TcpNetClient;
TcpNetClient.BeginConnect(IpAddress,Port, new AsyncCallback(ConnectedCallBack) , Connect);
if (SocketStateListener != null)
{
SocketStateListener.Connecting(TcpNetClient, "begin Connect", TagID);
}
}
}
}
private void ConnectedCallBack(IAsyncResult asyncResult)
{
lock (SocketConnect.SyncLock)
{
if (!IsStopConnect)
{
SocketConnect socketConnect = asyncResult.AsyncState as SocketConnect;
if (socketConnect != null && socketConnect.TcpNetClient != null)
{
int socketID = socketConnect.SocketID;
TcpClient tcpNetClient = socketConnect.TcpNetClient;
if (tcpNetClient != null)
{
tcpNetClient.NoDelay = true;
tcpNetClient.EndConnect(asyncResult);
if (socketConnect.IsConnected())
{
ISocketStateListener socketStateListener = socketConnect.SocketStateListener;
if (socketStateListener != null)
{
socketStateListener.Connected(tcpNetClient,"EndConnect", socketID);
Debug.LogError("网络连接成功");
}
}
}
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using System;
public interface INetStateListener
{
void CreateConnect(Object obj,string desc,int socketID);
void Connecting(Object obj, string desc, int socketID);
void Connected(Object obj, string desc, int socketID);
void CloseConnect(Object obj, string desc, int socketID);
void ConnectError(Object obj, string desc, int socketID);
void Ready(Object obj, string desc, int socketID);
void Send(Object obj, string desc, int socketID);
void Receive(Object obj, string desc, int socketID);
}
using System.Collections;
using System.Collections.Generic;
using System;
public interface ISocketStateListener : INetStateListener
{
void StartSendThread(Object obj, string desc, int socketID);
void CloseSendThread(Object obj, string desc, int socketID);
void StartReceiveThread(Object obj, string desc, int socketID);
void CloseReceiveThread(Object obj, string desc, int socketID);
}
最后
以上就是狂野跳跳糖为你收集整理的unity 客户端socket网络连接的全部内容,希望文章能够帮你解决unity 客户端socket网络连接所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复