gpt4 book ai didi

c# - 在 C# 中启动应用程序的 linux 服务,该应用程序从 tcp 端口读取并启动 GTK 窗口

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:39:04 25 4
gpt4 key购买 nike

我正在使用 C# 和 mono 为 linux (ubuntu 11.04) 编写应用程序。这个应用程序是一个服务器,它从我制作的客户端发送的 tcp 端口 (9898) 读取消息(一个 url)。服务器收到 URL 并弹出一个窗口(gtk 窗口),我在其中使用 WebKit 从 URL 嵌入 HTML 代码。当我执行程序时一切正常,但我需要这个应用程序在启动时运行,所以我制作了一个在计算机启动时运行的 shell 脚本。该脚本执行这一行“mono NetworkServer.exe &”。当我打开我的计算机时,该进程处于事件状态并且 NetworkServer.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

这是 NetworkServer.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();
}
}
}

这是 Client.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();
}
}
}

VisitorWindow.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

日志文件中没有错误,抛出任何异常,只是它崩溃了......任何帮助都会非常有用!!

最佳答案

可能有帮助的一件事是您说当您尝试连接客户端时程序崩溃。错误信息是什么?

我想补充的另一件事是您的 Client.cs 对我来说不合适。可能只是我太挑剔了,但是 Client 类的 Main 方法创建了它自己的一个实例。如何创建 Client 对象?你说当你的 Linux 机器启动时 NetworkServer.exe 正在运行,它只会在你创建客户端时崩溃。

关于c# - 在 C# 中启动应用程序的 linux 服务,该应用程序从 tcp 端口读取并启动 GTK 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10288656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com