在ubuntu上配置dotnet环境再跑个C#程序做套接字通信
参考微软官方知识库
添加存储库
sudo add-apt-repository ppa:dotnet/backports
安装 SDK
sudo apt-get update && sudo apt-get install -y dotnet-sdk-9.0
安装运行时
安装ASP.NET Core 运行时
sudo apt-get update && sudo apt-get install -y aspnetcore-runtime-9.0
或者
安装不包含 ASP.NET Core 支持的 .NET 运行时
sudo apt-get update && sudo apt-get install -y dotnet-runtime-9.0
编个程序测试下
dotnet new console -o SocketServer
Welcome to .NET 9.0!
---------------------
SDK Version: 9.0.107
----------------
Installed an ASP.NET Core HTTPS development certificate.
To trust the certificate, run 'dotnet dev-certs https --trust'
Learn about HTTPS: https://aka.ms/dotnet-https
----------------
Write your first app: https://aka.ms/dotnet-hello-world
Find out what's new: https://aka.ms/dotnet-whats-new
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /home/siit/donetscript/SocketServer/SocketServer.csproj:
Restore succeeded.
反馈信息里给出了一大溜学习资源,这个培养用户的手法是真不错。
继续继续,CD进路径。
cd SocketServer
使用Nano打开Program.cs,并写入以下代码。
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Program
{
    static void Main()
    {
        int port = 11000;
        IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, port);
        using (Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
        {
            listener.Bind(localEndPoint);
            listener.Listen(10);
            Console.WriteLine($"Server is running on port {port}. Accepting connections...");
            while (true)
            {
                Console.WriteLine("Waiting for a client to connect...");
                using (Socket handler = listener.Accept())
                {
                    Console.WriteLine("Client connected. Ready to receive messages:");
                    byte[] buffer = new byte[1024];
                    while (true)
                    {
                        int received = handler.Receive(buffer);
                        if (received == 0)
                        {
                            Console.WriteLine("Client disconnected.");
                            break;
                        }
                        string message = Encoding.ASCII.GetString(buffer, 0, received);
                        Console.WriteLine("Received: " + message);
                        string reply = $"Echo: {message}<EOF>";
                        byte[] replyBytes = Encoding.ASCII.GetBytes(reply);
                        handler.Send(replyBytes);
                    }
                }
            }
        }
    }
}
然后运行一下
dotnet run
看到反馈:
SocketServer$ dotnet run
Server is running on port 11000. Accepting connections...
Waiting for a client to connect...
然后通过另一台电脑,使用NetAssist去连接这台服务器。套接字通信和NetAssist下载看这篇

可以在服务器端看到
Client connected. Ready to receive messages:
客户端发送Hello,可以看到服务器的Echo。

同时在服务器终端可以看到
Received: Hello
下一步就准备把这段套接字通信的内容和视觉部分程序结合起来,就可以实现外部设备比如机器人或者PLC等触发视觉拍照获取坐标信息等内容的交互了。
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号