init csharp server

This commit is contained in:
SikongJueluo 2025-03-29 19:02:18 +08:00
parent cddf92e432
commit 351aad8300
No known key found for this signature in database
12 changed files with 216 additions and 5 deletions

View File

@ -20,9 +20,13 @@
sqls sqls
sql-studio sql-studio
# Backend # Backend
dotnetCorePackages.sdk_9_0 (dotnetCorePackages.combinePackages [
dotnetCorePackages.aspnetcore_9_0 dotnetCorePackages.sdk_9_0
dotnetCorePackages.aspnetcore_9_0
dotnetCorePackages.sdk_8_0
])
nuget nuget
omnisharp-roslyn
# LSP # LSP
typescript-language-server typescript-language-server

4
server/.csharpierrc.yaml Normal file
View File

@ -0,0 +1,4 @@
printWidth: 100
useTabs: false
tabWidth: 2
endOfLine: auto

3
server/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
obj
bin

43
server/Program.cs Normal file
View File

@ -0,0 +1,43 @@
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "FPGA Web Lab API",
Description = "Use FPGA in the cloud",
Version = "v1"
});
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "FPAG WebLab API V1");
});
}
// Router
app.MapGet("/", () => "Hello World!");
var thrdStartUdpServer = new ThreadStart(UDPServer.Start);
var thrdUdpServer = new Thread(thrdStartUdpServer);
thrdUdpServer.Start();
Console.WriteLine("Start UDP Server");
var thrdStartUdpClient = new ThreadStart(UDPClient.Start);
var thrdUdpClient = new Thread(thrdStartUdpClient);
thrdUdpClient.Start();
Console.WriteLine("Start UDP Client");
thrdUdpServer.Join();
thrdUdpClient.Join();
// app.Run("http://localhost:5000");

View File

@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5188",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7070;http://localhost:5188",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

9
server/appsettings.json Normal file
View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

14
server/server.csproj Normal file
View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.OpenApi" Version="1.6.23" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.0.0" />
</ItemGroup>
</Project>

View File

View File

@ -0,0 +1,41 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
class UDPClientPool
{
private static IPAddress localhost = IPAddress.Parse("127.0.0.1");
public UDPClientPool()
{
}
public static void SendLocalHost(int port, string[] stringArray)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
byte[] sendbuf = Encoding.ASCII.GetBytes(stringArray[0]);
IPEndPoint ep = new IPEndPoint(localhost, port);
socket.SendTo(sendbuf, ep);
}
public static void CycleSendLocalHost(int times, int sleepMilliSeconds, int port, string[] stringArray)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
byte[] sendbuf = Encoding.ASCII.GetBytes(stringArray[0]);
IPEndPoint ep = new IPEndPoint(localhost, port);
while (times-- >= 0)
{
socket.SendTo(sendbuf, ep);
Thread.Sleep(sleepMilliSeconds);
}
}
public void Start()
{
}
}

63
server/src/UdpServer.cs Normal file
View File

@ -0,0 +1,63 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UDPServer
{
private int listenPort;
private UdpClient listener;
private IPEndPoint groupEP;
private Thread thrd;
private bool isRunning;
public UDPServer(int port)
{
// Construction
listenPort = port;
listener = new UdpClient(listenPort);
groupEP = new IPEndPoint(IPAddress.Any, listenPort);
// New Thread
var thrdStart = new ThreadStart(ReceiveHandler);
thrd = new Thread(thrdStart);
}
private void ReceiveHandler()
{
try
{
while (isRunning)
{
byte[] bytes = listener.Receive(ref groupEP);
Console.WriteLine($"Received broadcast from {groupEP} :");
Console.WriteLine($" {Encoding.ASCII.GetString(bytes, 0, bytes.Length)}");
}
}
catch (SocketException e)
{
Console.WriteLine(e);
}
finally
{
listener.Close();
}
}
public void Start()
{
isRunning = true;
thrd.Start();
}
public void Stop()
{
isRunning = false;
thrd.Join();
}
}

View File

@ -1,8 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { useThemeStore } from './stores/theme'; import { useThemeStore } from "./stores/theme";
const theme = useThemeStore()
const theme = useThemeStore();
</script> </script>
<template> <template>