C#控制台程序如何发布到服务器Linux上运行
作者:QQ128619 时间:2022-07-17 05:57:52
1、创建控制台程序
如上图所示,选择linux开发平台,我用的VS2019,.Net5.0,一直点下一步,创建。
2、创建TCP服务端程序
using LinuxTcpApp.TcpServer;
using System;
using System.Net;
namespace LinuxTcpApp
{
class Program
{
private static AsyncTcpServer tcpServer;
static void Main(string[] args)
{
Console.WriteLine("程序开始运行");
IPAddress ipAddress;
IPAddress.TryParse("0.0.0.0", out ipAddress);
tcpServer = new AsyncTcpServer(ipAddress, 9888);
tcpServer.ClientConnected += TcpServer_ClientConnected;
tcpServer.ClientDisconnected += TcpServer_ClientDisconnected;
tcpServer.PlaintextReceived += TcpServer_PlaintextReceived;
tcpServer.Start();
while (true)
{
Thread.Sleep(1);
}
}
private static void TcpServer_PlaintextReceived(object sender, TcpDatagramReceivedEventArgs<string> e)
{
Console.WriteLine(e.Datagram);
tcpServer.SendAll(e.Datagram);
}
private static void TcpServer_ClientDisconnected(object sender, TcpClientDisconnectedEventArgs e)
{
Console.WriteLine("连接断开"+ e.TcpClient.Client.LocalEndPoint.ToString());
}
private static void TcpServer_ClientConnected(object sender, TcpClientConnectedEventArgs e)
{
Console.WriteLine("连接成功"+ e.TcpClient.Client.LocalEndPoint.ToString());
}
}
}
3、利用vs发布程序
我这里手动发布至文件夹,然后压缩上传到服务器
把文件放到了桌面,打开cmd利用SCP命令上传至服务器,有上传工具可以用工具上传,可以百度下载。
4、Ubuntu服务器安装.Net环境
安装命令如下:
sudo apt-get install dotnet-sdk-5.0
如果apt下载不了,请更新
wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update
5、解压运行tcp服务端程序
连接服务器工具我用的是Git Bash,使用ssh命令,登录解压命令用rar x linux-x64
进入解压后的文件夹查看tcp使用情况,使用命令netstat -apt
由上图所知,没有9888这个端口的服务启动过。
之后用dotnet 命令运行LinuxTcpApp.dll,即可启动程序
可以用调试助手,进行通讯测试程序是否有问题
6、服务器后台运行程序
使用nohup命令,让程序在后台运行。完整命令如下,参数可以百度了解。
再次查看,tcp使用情况,如下图所示:
至此利用C# 语言跨平台Tcp服务器程序开发部署完毕,当有大型程序任务开发时,此方法不推荐使用,最好使用容器,保证程序的运行的稳定性。
来源:https://blog.csdn.net/QQ128619/article/details/121518268
标签:c#,控制台,发布
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
maven profile动态选择配置文件详解
2023-11-05 22:05:20
![](https://img.aspxhome.com/file/2023/4/64514_0s.png)
Android 开发使用PopupWindow实现加载等待界面功能示例
2023-03-26 07:51:36
![](https://img.aspxhome.com/file/2023/6/100646_0s.gif)
C#通过html调用WinForm的方法
2022-02-26 03:53:16
Android View与Compose互相调用实例探究
2021-06-11 09:07:29
![](https://img.aspxhome.com/file/2023/3/96763_0s.png)
C#实现缩放和剪裁图片的方法示例
2023-06-03 11:09:31
Java的Socket网络编程基础知识入门教程
2021-11-19 10:10:37
Java调用linux shell脚本的方法
2022-03-25 23:09:46
java8 统计字符串字母个数的几种方法总结(推荐)
2023-09-25 17:37:48
C# 获取打印机当前状态的方法
2021-09-29 19:54:43
c#动态加载卸载DLL的方法
2023-06-08 08:18:08
java面试常见模式问题---代理模式
2022-02-14 13:07:31
jdk-logging log4j logback日志系统实现机制原理介绍
2022-03-22 11:45:28
![](https://img.aspxhome.com/file/2023/7/82037_0s.png)
springboot表单提交之validator校验
2023-05-16 23:19:17
![](https://img.aspxhome.com/file/2023/4/81324_0s.png)
Android仿知乎客户端关注和取消关注的按钮点击特效实现思路详解
2022-08-05 19:49:02
![](https://img.aspxhome.com/file/2023/5/94975_0s.png)
Android编程实现仿优酷旋转菜单效果(附demo源码)
2022-03-06 11:25:32
![](https://img.aspxhome.com/file/2023/7/111667_0s.jpg)
Java毕业设计实战之仿小米电子产品售卖商城系统的实现
2022-09-29 10:19:47
![](https://img.aspxhome.com/file/2023/0/72520_0s.jpg)
C#图像处理之头发检测的方法
2023-11-08 22:10:28
![](https://img.aspxhome.com/file/2023/7/96357_0s.jpg)
java求数组元素重复次数和java字符串比较大小示例
2023-08-04 22:20:02
java如何让带T的时间格式化
2022-12-22 10:44:28
![](https://img.aspxhome.com/file/2023/8/125238_0s.png)
简述IDEA集成Git在实际项目中的运用
2022-06-30 20:43:05
![](https://img.aspxhome.com/file/2023/3/113253_0s.jpg)