You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.5 KiB
58 lines
1.5 KiB
2 years ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
2 years ago
|
using System;
|
||
|
using System.Text;
|
||
|
using System.Net.Sockets;
|
||
|
using System.Threading;
|
||
|
using System.Net.NetworkInformation;
|
||
|
using System.Text.RegularExpressions;
|
||
|
using System.Net;
|
||
2 years ago
|
|
||
|
namespace JXSoft {
|
||
2 years ago
|
public class TCPServerModel:DataEventModel
|
||
2 years ago
|
{
|
||
|
|
||
|
public override void setLinkState(int linkState)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public override int getLinkState()
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
protected override void OnInit()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
public class TCPServerUtility {
|
||
|
public Socket tcpServer;
|
||
|
|
||
|
/// <summary>
|
||
|
/// 开启服务
|
||
|
/// </summary>
|
||
|
/// <param name="port">端口号</param>
|
||
|
/// <param name="maxLink">最大连接数</param>
|
||
|
public void startServer(int port,int maxLink) {
|
||
|
try
|
||
|
{
|
||
|
//点击开始监听时 在服务端创建一个负责监听IP和端口号的Socket
|
||
|
tcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||
|
IPAddress ip = IPAddress.Parse("127.0.0.1");
|
||
|
//创建对象端口
|
||
|
IPEndPoint point = new IPEndPoint(ip, port);
|
||
|
|
||
|
tcpServer.Bind(point);//绑定端口号
|
||
|
Debug.Log("监听成功!");
|
||
|
tcpServer.Listen(maxLink);//设置监听,最大同时连接10台
|
||
|
}
|
||
|
catch { }
|
||
|
}
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
}
|
||
|
|