using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
/// <summary>
/// 服务端
/// </summary>
public class TcpServer : MonoBehaviour
{
/// <summary>服务器端;</summary>
private Socket _ServerSocket;
/// <summary>客户端;</summary>
private Socket _ClientSocket;
/// <summary>侦听端口;</summary>
private IPEndPoint _IpEnd;
/// <summary>接收的字符串;</summary>
private string _ReceiveStr;
/// <summary>发送的字符串;</summary>
private string _SendStr;
/// <summary>接收的数据,必须为字节;</summary>
private byte[] _ReceiveData = new byte[1024];
/// <summary>发送的数据;</summary>
private byte[] _SendData = new byte[1024];
/// <summary>接收的数据长度;</summary>
private int _ReceiveLength;
/// <summary>连接线程;</summary>
private Thread _ConnectThread;
private void Start()
{
InitSocket();
}
private void OnApplicationQuit()
{
SocketQuit();
}
/// <summary>初始化服务器;</summary>
public void InitSocket()
{
//定义侦听端口,侦听任何IP;
_IpEnd = new IPEndPoint(IPAddress.Any, ConfigDataReader.Config.ComputerUsePort);
//定义套接字类型,在主线程定义;
_ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//连接;
_ServerSocket.Bind(_IpEnd);
//开始侦听,最大10个连接;
_ServerSocket.Listen(10);
//开启一个线程连接,必须的,否则主线程卡死;
_ConnectThread = new Thread(new ThreadStart(SocketReceive));
_ConnectThread.Start();
}
private void SocketConnet()
{
//if (_ClientSocket != null)
// _ClientSocket.Close();
//控制台输出侦听状态;
Debug.Log("Waiting for a client");
_ClientSocket = _ServerSocket.Accept();
//获取客户端的IP和端口;
IPEndPoint ipEndClient = _ClientSocket.RemoteEndPoint as IPEndPoint;
//输出客户端的IP和端口
Debug.Log("Connect with" + ipEndClient.Address.ToString() + ":" + ipEndClient.Port.ToString());
//连接成功发送数据;
_SendStr = "Connect succeed";
SocketSend(_SendStr);
Thread th = new Thread(ReceiveMsg);
th.IsBackground = true;
th.Start(_ClientSocket);
}
private void SocketSend(string sendStr)
{
//清空发送缓存;
_SendData = new byte[1024];
//数据类型转换;
_SendData = Encoding.UTF8.GetBytes(sendStr);//一般用UTF8,,有中文的时候用Encoding.ASCII.GetBytes(sendStr)
//发送;
_ClientSocket.Send(_SendData, _SendData.Length, SocketFlags.None);
}
private void SocketReceive()
{
//连接
//SocketConnet();
//进入接收循环;
while (true)
{
SocketConnet();
}
}
private void ReceiveMsg(object o)
{
Socket client = o as Socket;
while (true)
{
try
{
byte[] buff = new byte[1024];
int len = client.Receive(buff);
string msg = Encoding.UTF8.GetString(buff, 0, len);
print("收到客户端" + client.RemoteEndPoint.ToString() + "的信息:" + msg);
ConfigDataReader.Instance.ReadMessage(msg);
_SendStr = ConfigDataReader.Instance.WriteMessage(msg);
ConfigDataReader.Instance.WriteFile(msg);
ConfigDataReader.Instance.WriteFile(_SendStr);
ConfigDataReader.Instance.WriteFile("");
SocketSend(_SendStr);
}
catch (System.Exception ex)
{
SocketSend(ex.ToString());
break;
}
}
}
private void SocketQuit()
{
//先关闭客户端;
if (_ClientSocket != null)
_ClientSocket.Close();
//再关闭线程
if (_ConnectThread != null)
{
_ConnectThread.Interrupt();
_ConnectThread.Abort();
}
//最后关闭服务器
_ServerSocket.Close();
Debug.Log("Exit the connection");
}
}