Linux(ubuntu 11.04)用のアプリをC#とmonoでコーディングしています。このアプリは、私が作成したクライアントから送信されたtcpポート(9898)からメッセージ(URL)を読み取るサーバーです。サーバーはURLを受信し、WebKitを使用してURLからHTMLコードを埋め込むウィンドウ(gtkウィンドウ)をポップアップします。プログラムを実行するとすべて正常に動作しますが、起動時にこのアプリを実行する必要があるため、コンピューターの起動時に実行されるシェルスクリプトを作成しました。このスクリプトは、この行「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
ログファイルにエラーはなく、例外がスローされ、クラッシュするだけです...どんなヘルプも非常に役立ちます!!