Linux 服务,用于启动 C# 中的应用程序,该应用程序从 TCP 端口读取并启动 GTK 窗口
本文关键字:启动 应用程序 读取 窗口 GTK TCP 服务 Linux 用于 | 更新日期: 2023-09-27 18:31:58
我正在使用C#和mono为linux(ubuntu 11.04)编写一个应用程序。此应用程序是从tcp端口(9898)读取消息(URL)的服务器,该消息由我制作的客户端发送。服务器收到 URL 并弹出一个窗口(gtk 窗口),我在其中使用 WebKit 嵌入来自 URL 的 HTML 代码。当我执行程序时一切正常,但我需要在启动时运行这个应用程序,所以我制作了一个在计算机启动时运行的 shell 脚本。此脚本执行此行"mono NetworkServer.exe &"。当我打开计算机时,进程处于活动状态并且网络服务器正在运行.exe但是如果我尝试连接客户端,程序在尝试创建窗口时会崩溃。如果我手动启动|重新启动脚本,一切正常,所以问题是在系统启动时启动应用程序。
这是脚本(我删除了一些功能以使其更简单):
#! /bin/sh
### BEGIN INIT INFO
# Provides: visitord
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Create a deamon that initializes the NetworkServer.exe
### END INIT INFO
#GLOBAL VARIABLES
ROOT_ID=0 # Root ID
USER_ID=$(id -u) # User ID of the user who is running the script
PATH=/sbin:/usr/sbin:/bin:/usr/bin #
APP_PATH=/usr/bin/NetworkServer.exe # The path where the Visitor APP is located
RETURN_VALUE=0 # The return value
APP_PID=`ps -ef | grep $APP_PATH | grep -v grep | awk '{print$2}'` # The Visitor APP PID
. /lib/lsb/init-functions
do_start (){
# if it is running do nothing
if [ -n "$APP_PID" ]; then
echo "The visitord service is already running (pid $APP_PID)"
RETURN_VALUE=1
else
if [ -f $APP_PATH ]; then
mono $APP_PATH &
APP_PID=$!
echo "The visitord service is running (pid $APP_PID)"
RETURN_VALUE=0
else
echo "The VisitorApp doesn't exist, or its location has been changed"
RETURN_VALUE=1
fi
fi
}
case "$1" in
start)
do_start
exit $RETURN_VALUE
;;
*)
echo "Usage: visitord start|stop|restart|status" >&2
exit 3
;;
esac
这是网络服务器.cs代码:
using System;
using System.IO;
using System.Net.Sockets;
using System.Threading;
namespace VisitorApp{
public class NetworkServer{
private int port;
private TcpListener tcpListener;
public NetworkServer(int port){
this.port = port;
}
public void Start(){
TextWriter tw = File.AppendText("/var/www/Visitor/log.log");
try{
this.tcpListener = new TcpListener(this.port);
this.tcpListener.Start();
tw.WriteLine(DateTime.Now+" >> Server started");
}catch{
tw.WriteLine(DateTime.Now+" >> The port "+this.port+" is already in use");
return;
}finally{
tw.Close();
}
int clientNo=0;
while(true){
System.Net.Sockets.Socket socketForClient = tcpListener.AcceptSocket();
clientNo++;
if (socketForClient.Connected){
tw = File.AppendText("/var/www/Visitor/log.log");
tw.WriteLine(DateTime.Now+" >> Client["+clientNo+"] is connected");
NetworkStream networkStream = new NetworkStream(socketForClient);
StreamReader streamReader = new StreamReader(networkStream);
string url;
try{
url = streamReader.ReadLine();
tw.WriteLine(DateTime.Now+" >> URL from the Client: "+url);
//The execution stops here!!!!!!
new VisitorWindow("Visitor App",url);
tw.WriteLine(DateTime.Now+" >> An open window has been closed");
}catch{
tw.WriteLine(DateTime.Now+" The Client didn't send a URL");
}finally{
tw.Close();
socketForClient.Close();
networkStream.Close();
streamReader.Close();
}
}
}
}
public static void Main(){
new Thread(new ThreadStart(
delegate {
new NetworkServer(9898).Start();
})).Start();
}
}
}
这是客户端.cs代码:
using System;
using System.IO;
using System.Net.Sockets;
namespace Client{
public class Client{
private TcpClient clientSocket;
private string server;
private int port;
private string url;
public Client(string server, int port, string url){
this.clientSocket = new TcpClient();
this.server = server;
this.port = port;
this.url = url;
}
public void Connect(){
try{
this.clientSocket.Connect(this.server,this.port);
}catch{
Console.WriteLine("Unable to connect to server: "+this.server+" port: "+this.port);
return;
}
NetworkStream serverStream = this.clientSocket.GetStream();
StreamWriter streamWriter = new System.IO.StreamWriter(serverStream);
try{
streamWriter.WriteLine(url);
streamWriter.Flush();
}catch{
Console.WriteLine("Exception writing to the Server");
}finally{
this.clientSocket.Close();
serverStream.Close();
streamWriter.Close();
}
}
public static void Main(){
Client c = new Client("localhost",9898,"http://www.stackoverflow.com");
c.Connect();
}
}
}
访客窗口.cs代码:
using System;
using System.IO;
using Gtk;
using WebKit;
namespace VisitorApp{
public class VisitorWindow {
private string windowName;
private string url;
private Gtk.Window window;
public TextWriter tw;
public VisitorWindow(string windowName, string url){
this.windowName = windowName;
this.url = url;
try{
this.CreateWindow();
}catch(Exception exc){
this.tw.WriteLine(DateTime.Now+" >> "+exc.ToString());
}finally{
this.tw.Close();
}
}
public void CreateWindow(){
this.tw = File.AppendText("/var/www/Visitor/log.log");
this.tw.WriteLine(DateTime.Now+" >> Before Application.Init");
Application.Init ();
this.tw.WriteLine(DateTime.Now+" >> After Application.Init");
//Set the graphical properties of the window
this.window = new Gtk.Window (this.windowName);
this.tw.WriteLine(DateTime.Now+" >> After Gtk.Window (this.windowName)");
window.SetDefaultSize(1000,800);
this.tw.WriteLine(DateTime.Now+" >> After window.SetDefaultSize(1000,800)");
//window.FullScreen();
//Adding events for actions
window.DeleteEvent += new DeleteEventHandler (OnWindowDelete);
this.tw.WriteLine(DateTime.Now+" >> After DeleteEventHandler (OnWindowDelete)");
//Create the embed content
ScrolledWindow scroll = new ScrolledWindow ();
this.tw.WriteLine(DateTime.Now+" >> After new scroll");
WebView webView = new WebView();
this.tw.WriteLine(DateTime.Now+" >> After new WebView();");
webView.Open (this.url);
this.tw.WriteLine(DateTime.Now+" >> webView.Open (this.url);");
scroll.Add (webView);
this.tw.WriteLine(DateTime.Now+" >> scroll.Add (webView);");
window.Add (scroll);
this.tw.WriteLine(DateTime.Now+" >> window.Add (scroll);");
window.ShowAll();
this.tw.WriteLine(DateTime.Now+" >> window.ShowAll()");
Application.Run ();
this.tw.WriteLine(DateTime.Now+" >> Application.Run ");
}
void OnWindowDelete (object obj, DeleteEventArgs args){
Application.Quit();
}
}
}
日志文件:
04/24/2012 08:23:27 >> Server started
04/24/2012 08:24:56 >> Client[1] is connected
04/24/2012 08:24:56 >> URL from the Client: http://www.stackoverflow.com
日志文件中没有错误,抛出任何异常,只是崩溃...任何帮助都将非常有用!!
可能会有所帮助的一件事是,当您尝试连接客户端时,程序会崩溃。错误消息是什么?
我想补充的另一件事是,你的客户.cs对我来说不对劲。可能只是我挑剔,但 Client 类的 Main 方法创建了自身的一个实例。如何创建客户端对象?您说 Linux机器启动时网络服务器.exe正在运行,只有在您创建客户端时才会崩溃。
您无法从服务器打开 X 窗口 - 您如何知道要连接到哪个显示器?
您将需要另一个在 X 中运行的客户端程序,它连接到您的服务器并接收事件,并且该客户端 GUI 程序可以在 Linux 桌面上执行操作。
考虑像这样的机器 - 六用户多头系统 - 您的程序如何知道要连接到哪个显示器?没有由 init 启动的守护进程 - 来自 SysV 目录或暴发户,可以知道要使用哪个显示器。因此,您需要在计算机用户的一个桌面上启动程序,正如您可能建议的那样 xinit
.这可以是你的整个程序,也可以只是你的守护程序的客户端,两者都应该是可能的。