[Mono-dev] Web Services Under Linux/OSX

Gavin Landon Gavin.Landon at ignitetech.com
Wed Dec 26 11:21:14 EST 2007


Using:
	Mono 1.2.6
	SharpDev 2.2.1 2648
	Mono References Only

I've created a very simple web server demo below, which works under
Windows, but will not launch under Linux/OSX.   This is a console app,
to make things simple, running as a web server.   I added a writelog to
the very first line of Main() which is never written as if the EXE will
not even attempt to start on Linux/OSX.  Anyone have any ideas why?   I
have other applications in Mono 1.2.6 running on the same Linux/OSX
machines without any issues.

/*
 * Created by SharpDevelop.
 * User: gavin.landon
 * Date: 12/26/2007
 * Time: 9:43 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard
Headers.
 */
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace WebTest
{
	class Program
	{
		static private TcpListener myListener;

        static bool WriteLog(string szBuffer, string szFileName, bool
bAppend)
        {
            try
            {
                szBuffer = DateTime.Now.ToString() + " - " + szBuffer;
                szBuffer = szBuffer + "\n";

                if (!File.Exists(szFileName))
                {
                    // Create the file.
                    using (FileStream fs = File.Create(szFileName))
                    {
                        Byte[] info = new
UTF8Encoding(true).GetBytes(szBuffer);
                        fs.Write(info, 0, info.Length);

                        return true;
                    }
                }
                else
                {
                    if (bAppend)
                        File.AppendAllText(szFileName, szBuffer);
                    else
                        File.WriteAllText(szFileName, szBuffer);

                    return true;
                }
            }
            catch (Exception e)
            {
            	Console.WriteLine("An Exception Occurred while Listening
: {0}", e);
                return false;
            }
        } //end WriteLog
        
		static public void Main(string[] args)
		{
			WriteLog("Entered Main()", "info.log", true);
			try
			{
				//start listing on the given port
				myListener = new
TcpListener(IPAddress.Any, 5000);
				myListener.Start();
				Console.WriteLine("Web Server Running...
Press ^C to Stop...");
				//start the thread which calls the
method 'StartListen'
				Thread th = new Thread(new
ThreadStart(StartListen));
				th.Start();

			}
			catch (Exception e)
			{
				WriteLog(e.ToString(), "error.log",
true);
				Console.WriteLine("An Exception Occurred
while Listening : {0}", e);
			}
		}
		
		static void StartListen()
		{
			while (true)
            {
				//Accept a new connection
				Socket mySocket =
myListener.AcceptSocket();
				Console.WriteLine("Socket Type " +
mySocket.SocketType);
				if (mySocket.Connected)
				{
					Console.WriteLine("\nAt {1}
Client IP {0} Connected.\n",
	
mySocket.RemoteEndPoint, System.DateTime.Now);
					//make a byte array and receive
data from the client
					Byte[] bReceive = new
Byte[1024];
					int i =
mySocket.Receive(bReceive, bReceive.Length, 0);
	
					//Convert Byte to String
					string sBuffer =
Encoding.ASCII.GetString(bReceive);
	
					Console.WriteLine("\nBuffer =
{0}\n", sBuffer);
					
					
					string sResponse = "Data
Received";
					int iTotBytes =
sResponse.Length;
					byte[] writebytes = new
byte[iTotBytes];
					writebytes =
Encoding.ASCII.GetBytes(sResponse);
					
					SendHeader("HTTP/1.1",
"text/html", iTotBytes, " 200 OK", ref mySocket);
					SendToBrowser(writebytes, ref
mySocket);
					mySocket.Close();
				}
			}
		}
		
		static void SendHeader(string sHttpVersion, string
sMIMEHeader, int iTotBytes, string sStatusCode, ref Socket mySocket)
		{

			string sBuffer = "";

			// if Mime type is not provided set default to
text/html
			if (sMIMEHeader.Length == 0)
				sMIMEHeader = "text/html";  // Default
Mime Type is text/html

			sBuffer = sBuffer + sHttpVersion + sStatusCode +
"\r\n";
			sBuffer = sBuffer + "Server: " +
Environment.MachineName + "\r\n";
			sBuffer = sBuffer + "Content-Type: " +
sMIMEHeader + "\r\n";
			sBuffer = sBuffer + "Accept-Ranges: bytes\r\n";
			sBuffer = sBuffer + "Content-Length: " +
iTotBytes + "\r\n\r\n";

			Byte[] bSendData =
Encoding.ASCII.GetBytes(sBuffer);
			SendToBrowser(bSendData, ref mySocket);
		}
		
		static void SendToBrowser(string sData, ref Socket
mySocket)
		{
			SendToBrowser(Encoding.ASCII.GetBytes(sData),
ref mySocket);
		}

		static void SendToBrowser(Byte[] bSendData, ref Socket
mySocket)
		{
			int numBytes = 0;

			try
			{
				if (mySocket.Connected)
				{
					if ((numBytes =
mySocket.Send(bSendData, bSendData.Length, 0)) == -1)
	
Console.WriteLine("Socket Error cannot Send Packet");
					else
						Console.WriteLine("No.
of bytes send {0}", numBytes);
				}
				else
					Console.WriteLine("Connection
Dropped....");
			}
			catch (Exception e)
			{
				Console.WriteLine("Error Occurred : {0}
", e);

			}
		}
	}
}



More information about the Mono-devel-list mailing list