展示 HN:用 C# 和 .NET 9 编写的 TCP 聊天服务器,在终端中使用。
Show HN: TCP chat server written in C# and .NET 9, used in the terminal

原始链接: https://github.com/Sieep-Coding/simple-chat-csharp

## CSharpStream:一个简单的终端聊天服务器 CSharpStream 是一个基础的、基于终端的聊天服务器和客户端应用程序,使用 C# 和 .NET 9 构建。目前在 Linux PopOS 上测试,它允许多个用户通过 TCP 连接并交换实时消息。 服务器通过 `dotnet run --project CSharpStream server` 启动,监听 8000 端口的传入连接。客户端使用 `dotnet run --project CSharpStream client` 连接,并在连接时输入用户名。然后通过终端发送消息,并广播给所有已连接的客户端。 该项目结构分为服务器、客户端和数据模型(Message & User)等独立组件。`ChatServer.cs` 处理核心服务器逻辑,而 `Program.cs` 管理基于命令行参数的启动。 确保防火墙未阻止 8000 端口。该项目采用 MIT 许可。

一个用C#和.NET 9编写的新型、简单的TCP聊天服务器和客户端在Hacker News上引起了关注。该项目由用户“sieep”创建,包含单文件服务器和客户端应用程序,专为终端使用而设计,旨在实现最简单的两人TCP通信。 讨论很快转向了与IRC的比较,作者承认自己网络知识有限,但确认了相似的底层原理。一些评论员最初猜测代码是由AI生成的,但另一些人指出其风格表明开发者正在学习网络基础知识——这是OP的第一个公开的C#仓库。 批评集中在处理大于4096字节的消息的潜在问题以及缺乏强大的TCP流处理上,这可能导致在现实网络条件下出现故障。作者承认了反馈并表示有兴趣改进。该项目在Hacker News上的快速走红归功于算法更倾向于快速的初始评分,而不是总点数。
相关文章

原文

A terminal-based chat server written in C#.

Warning

Only Tested on Linux PopOS!

License: MIT PopOS Passing wakatime

Listens for connections & messages, logs in real-time.

Steve sends a message to the server.

Nick sends a message to the server.

.NET 9 SDK

Compatible terminal environment (Linux, macOS, Windows)

  • Open a terminal and navigate to the project root directory.
  • Run:
dotnet run --project CSharpStream server
  • The server listens on port 8000 by default.
  • Press Ctrl+C to gracefully stop the server.
  • Open a new terminal window.
  • Run:
dotnet run --project CSharpStream client
  • When prompted, enter your username.
  • Type messages and press Enter to send them to the chat.
  • CSharpStream.Server – TCP server implementation handling client connections and message broadcasting.
  • CSharpStream.Client – TCP client implementation for sending and receiving chat messages.
  • CSharpStream.Models – Contains the Message and User models, used for structured data transfer and managing user identity information.
  • ChatServer.cs - Works as the service layer.
  • Program.cs - Starts client/server based on command line arguments.
  • Ensure no firewall or network restrictions block port 8000.
  • If connection is refused, verify the server is running before starting clients.
  • Use Ctrl+C to exit clients and server cleanly.

MIT License

联系我们 contact @ memedata.com