mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-12-05 08:09:03 +00:00
添加项目文件。
This commit is contained in:
parent
57cff72909
commit
847d176840
15
FunGameServer.csproj
Normal file
15
FunGameServer.csproj
Normal file
@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\Entity\" />
|
||||
<Folder Include="Models\Config\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
25
FunGameServer.sln
Normal file
25
FunGameServer.sln
Normal file
@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.3.32804.467
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunGameServer", "FunGameServer.csproj", "{CFC4F490-967B-4F12-883E-86C2A6E61461}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{CFC4F490-967B-4F12-883E-86C2A6E61461}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{CFC4F490-967B-4F12-883E-86C2A6E61461}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CFC4F490-967B-4F12-883E-86C2A6E61461}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CFC4F490-967B-4F12-883E-86C2A6E61461}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {B4235C3B-6B99-4B97-B7F1-B586B219B51E}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
120
Main.cs
Normal file
120
Main.cs
Normal file
@ -0,0 +1,120 @@
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System;
|
||||
using FunGameServer.Sockets;
|
||||
using System.Net.WebSockets;
|
||||
|
||||
bool Running = true;
|
||||
Socket? ServerSocket = null;
|
||||
|
||||
string host = "127.0.0.1";
|
||||
int port = 22222;
|
||||
|
||||
try
|
||||
{
|
||||
Task t = Task.Factory.StartNew(() =>
|
||||
{
|
||||
// 创建IP地址终结点对象
|
||||
IPAddress ip = IPAddress.Parse(host);
|
||||
IPEndPoint ipe = new IPEndPoint(ip, port);
|
||||
|
||||
// 创建TCP Socket对象并绑定终结点
|
||||
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
ServerSocket.Bind(ipe);
|
||||
|
||||
// 开始监听连接
|
||||
ServerSocket.Listen(0);
|
||||
Console.WriteLine("服务器启动成功,正在监听 . . .");
|
||||
|
||||
while (Running)
|
||||
{
|
||||
Socket socket;
|
||||
try
|
||||
{
|
||||
socket = ServerSocket.Accept();
|
||||
IPEndPoint? clientIP = (IPEndPoint?)socket.RemoteEndPoint;
|
||||
if (clientIP != null)
|
||||
Console.WriteLine("客户端" + clientIP.ToString() + "连接 . . .");
|
||||
else
|
||||
Console.WriteLine("未知地点客户端连接 . . .");
|
||||
Task.Factory.StartNew(() =>
|
||||
{
|
||||
new ClientSocket(socket, Running).Start();
|
||||
});
|
||||
// 接收客户端消息
|
||||
Receive(socket);
|
||||
// 发送给客户端消息
|
||||
Send(socket);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("ERROR: 客户端断开连接!\n" + e.StackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.StackTrace);
|
||||
if (ServerSocket != null)
|
||||
{
|
||||
ServerSocket.Close();
|
||||
ServerSocket = null;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
while (Running)
|
||||
{
|
||||
string? order = "";
|
||||
order = Console.ReadLine();
|
||||
if (order != null && !order.Equals(""))
|
||||
{
|
||||
switch (order)
|
||||
{
|
||||
case "quit":
|
||||
Running = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("服务器已关闭,按任意键退出程序。");
|
||||
Console.ReadKey();
|
||||
|
||||
|
||||
static void Receive(Socket socket)
|
||||
{
|
||||
byte[] bytes = new byte[1024];
|
||||
//从客户端接收消息
|
||||
int len = socket.Receive(bytes, bytes.Length, 0);
|
||||
//将消息转为字符串
|
||||
string recvStr = Encoding.ASCII.GetString(bytes, 0, len);
|
||||
Console.WriteLine("接收的客户端消息 : {0}", recvStr);
|
||||
}
|
||||
|
||||
void Send(Socket socket)
|
||||
{
|
||||
string sendStr = ">> 服务器" + host + ":" + port + "连接成功";
|
||||
Console.WriteLine("发送给客户端消息 : {0}", sendStr);
|
||||
// 将字符串消息转为数组
|
||||
byte[] bytes = Encoding.ASCII.GetBytes(sendStr);
|
||||
//发送消息给客户端
|
||||
socket.Send(bytes, bytes.Length, 0);
|
||||
}
|
||||
|
||||
bool IsIP(string ip)
|
||||
{
|
||||
//判断是否为IP
|
||||
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
|
||||
}
|
||||
|
||||
bool IsEmail(string ip)
|
||||
{
|
||||
//判断是否为Email
|
||||
return Regex.IsMatch(ip, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$");
|
||||
}
|
||||
81
Sockets/ClientSocket.cs
Normal file
81
Sockets/ClientSocket.cs
Normal file
@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FunGameServer.Sockets
|
||||
{
|
||||
public class ClientSocket
|
||||
{
|
||||
public bool Running { get; set; } = false;
|
||||
public Socket? Socket { get; set; } = null;
|
||||
|
||||
public ClientSocket(Socket socket, bool running)
|
||||
{
|
||||
Socket = socket;
|
||||
Running = running;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Task StringStream = Task.Factory.StartNew(() =>
|
||||
{
|
||||
CreateStringStream();
|
||||
});
|
||||
Task IntStream = Task.Factory.StartNew(() =>
|
||||
{
|
||||
CreateStringStream();
|
||||
});
|
||||
Task DecimalStream = Task.Factory.StartNew(() =>
|
||||
{
|
||||
CreateDecimalStream();
|
||||
});
|
||||
Task ObjectStream = Task.Factory.StartNew(() =>
|
||||
{
|
||||
CreateObjectStream();
|
||||
});
|
||||
}
|
||||
|
||||
private void CreateStringStream()
|
||||
{
|
||||
Thread.Sleep(1000);
|
||||
Console.WriteLine("Creating: StringStream...OK");
|
||||
while (Running)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateIntStream()
|
||||
{
|
||||
Thread.Sleep(1000);
|
||||
Console.WriteLine("Creating: IntStream...OK");
|
||||
while (Running)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateDecimalStream()
|
||||
{
|
||||
Thread.Sleep(1000);
|
||||
Console.WriteLine("Creating: DecimalStream...OK");
|
||||
while (Running)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateObjectStream()
|
||||
{
|
||||
Thread.Sleep(1000);
|
||||
Console.WriteLine("Creating: ObjectStream...OK");
|
||||
while (Running)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user