我正在使用 C# 和单声道为 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();
}
}
}
访问者窗口.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
日志文件中没有错误,抛出任何异常,只是它崩溃了......任何帮助都会非常有用!