Mossy's World | Weird Stuff..

Archive for October 2009

Oct/09

16

C# – Easy Sockets Class

/*
* Sockets Client
* (C)2009 Daniel Moss
*
* Handles all of the Socket functions
*
*/
internal class StateObject
{
public byte[] buffer = new byte[32768];
public int BufferSize = 32767;
public StringBuilder sb = new StringBuilder();
public Socket workSocket;
}
internal class SocketsClient
{
#region Delegates
public delegate void onConnectEventHandler();
public delegate void onDataArrivalEventHandler(byte[] Data, int TotalBytes);
public delegate void onDisconnectEventHandler();
public delegate void onErrorEventHandler(string Description);
public delegate void onSendCompleteEventHandler(int DataSize);
#endregion
private static Socket  client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private static IPHostEntry ipHostInfo = Dns.GetHostEntry(”localhost”);
private static IPAddress ipAddress = ipHostInfo.AddressList[0];
private static int port = 44;
// private static string response;
public event onConnectEventHandler onConnect;
public event onErrorEventHandler onError;
public event onDataArrivalEventHandler onDataArrival;
public event onDisconnectEventHandler onDisconnect;
public event onSendCompleteEventHandler onSendComplete;
public void Connect(string RemoteHostName, int RemotePort)
{
try
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
port = RemotePort;
ipHostInfo = Dns.GetHostEntry(RemoteHostName);
ipAddress = ipHostInfo.AddressList[0];
var remoteEP = new IPEndPoint(ipAddress, port);
client.BeginConnect(remoteEP, sockConnected, client);
}
catch (Exception ex)
{
if (onError != null)
onError(ex.Message);
return;
}
}
public void SendData(byte[] Data)
{
try
{
var byteData = Data;
client.BeginSend(byteData, 0, byteData.Length, 0, sockSendEnd, client);
}
catch (Exception ex)
{
if (onError != null)
onError(ex.Message);
return;
}
}
public void Disconnect()
{
try
{
client.Shutdown(SocketShutdown.Both);
}
catch
{
}
onDisconnect();
client.Close();
}
public byte[] StringToBytes(string Data)
{
return Encoding.ASCII.GetBytes(Data);
}
public string BytestoString(byte[] Data)
{
return Encoding.ASCII.GetString(Data);
}
private void sockConnected(IAsyncResult ar)
{
try
{
if (client.Connected == false)
{
if (onError != null)
onError(”Connection refused.”);
return;
}
var state = new StateObject();
state.workSocket = client;
client.BeginReceive(state.buffer, 0, state.BufferSize, 0, sockDataArrival, state);
if (onConnect != null)
onConnect();
}
catch (Exception ex)
{
if (onError != null)
onError(ex.Message);
return;
}
}
private void sockDataArrival(IAsyncResult ar)
{
var state = (StateObject)ar.AsyncState;
var client = state.workSocket;
var bytesRead = 0;
try
{
bytesRead = client.EndReceive(ar);
}
catch
{
return;
}
try
{
var Data = state.buffer;
if (bytesRead == 0)
{
client.Shutdown(SocketShutdown.Both);
client.Close();
if (onDisconnect != null)
onDisconnect();
return;
}
state.buffer = new byte[32768];
client.BeginReceive(state.buffer, 0, state.BufferSize, 0, sockDataArrival, state);
if (onDataArrival != null)
onDataArrival(Data, bytesRead);
}
catch (Exception ex)
{
if (onError != null)
onError(ex.Message);
return;
}
}
private void sockSendEnd(IAsyncResult ar)
{
try
{
var client = (Socket)ar.AsyncState;
var bytesSent = client.EndSend(ar);
if (onSendComplete != null)
onSendComplete(bytesSent);
}
catch (Exception ex)
{
if (onError != null)
onError(ex.Message);
return;
}
}
public bool Connected()
{
try
{
return client.Connected;
}
catch (Exception ex)
{
if (onError != null)
onError(ex.Message);
return false;
}
}
}
    /*
     * Sockets Class
     * (C)2009 Daniel Moss
     *
     * Handles all of the Socket functions
     * DO NOT REMOVE (C) NOTICE!
     *
     */

    internal class StateObject
    {
        public byte[] buffer = new byte[32768];
        public int BufferSize = 32767;
        public StringBuilder sb = new StringBuilder();
        public Socket workSocket;
    }

    internal class SocketsClass
    {
        #region Delegates

        public delegate void onConnectEventHandler();

        public delegate void onDataArrivalEventHandler(byte[] Data, int TotalBytes);

        public delegate void onDisconnectEventHandler();

        public delegate void onErrorEventHandler(string Description);

        public delegate void onSendCompleteEventHandler(int DataSize);

        #endregion

        private static Socket  client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        private static IPHostEntry ipHostInfo = Dns.GetHostEntry("localhost");
        private static IPAddress ipAddress = ipHostInfo.AddressList[0];
        private static int port = 44;
       // private static string response;
        public event onConnectEventHandler onConnect;

        public event onErrorEventHandler onError;

        public event onDataArrivalEventHandler onDataArrival;

        public event onDisconnectEventHandler onDisconnect;

        public event onSendCompleteEventHandler onSendComplete;

        public void Connect(string RemoteHostName, int RemotePort)
        {
            try
            {
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                port = RemotePort;
                ipHostInfo = Dns.GetHostEntry(RemoteHostName);
                ipAddress = ipHostInfo.AddressList[0];
                var remoteEP = new IPEndPoint(ipAddress, port);
                client.BeginConnect(remoteEP, sockConnected, client);
            }
            catch (Exception ex)
            {
                if (onError != null)

                    onError(ex.Message);
                return;
            }
        }

        public void SendData(byte[] Data)
        {
            try
            {
                var byteData = Data;
                client.BeginSend(byteData, 0, byteData.Length, 0, sockSendEnd, client);
            }
            catch (Exception ex)
            {
                if (onError != null)

                    onError(ex.Message);
                return;
            }
        }

        public void Disconnect()
        {
            try
            {

                client.Shutdown(SocketShutdown.Both);
            }
            catch
            {
            }
            onDisconnect();
            client.Close();
        }

        public byte[] StringToBytes(string Data)
        {
            return Encoding.ASCII.GetBytes(Data);
        }

        public string BytestoString(byte[] Data)
        {
            return Encoding.ASCII.GetString(Data);
        }

        private void sockConnected(IAsyncResult ar)
        {
            try
            {
                if (client.Connected == false)
                {
                    if (onError != null)
                        onError("Connection refused.");
                    return;
                }
                var state = new StateObject();
                state.workSocket = client;
                client.BeginReceive(state.buffer, 0, state.BufferSize, 0, sockDataArrival, state);
                if (onConnect != null)
                    onConnect();
            }
            catch (Exception ex)
            {
                if (onError != null)

                    onError(ex.Message);
                return;
            }
        }

        private void sockDataArrival(IAsyncResult ar)
        {
            var state = (StateObject)ar.AsyncState;
            var client = state.workSocket;
            var bytesRead = 0;

            try
            {
                bytesRead = client.EndReceive(ar);
            }
            catch
            {
                return;
            }

            try
            {
                var Data = state.buffer;
                if (bytesRead == 0)
                {
                    client.Shutdown(SocketShutdown.Both);
                    client.Close();
                    if (onDisconnect != null)
                        onDisconnect();
                    return;
                }
                state.buffer = new byte[32768];

                client.BeginReceive(state.buffer, 0, state.BufferSize, 0, sockDataArrival, state);
                if (onDataArrival != null)
                    onDataArrival(Data, bytesRead);
            }
            catch (Exception ex)
            {
                if (onError != null)

                    onError(ex.Message);
                return;
            }
        }

        private void sockSendEnd(IAsyncResult ar)
        {
            try
            {
                var client = (Socket)ar.AsyncState;
                var bytesSent = client.EndSend(ar);
                if (onSendComplete != null)
                    onSendComplete(bytesSent);
            }
            catch (Exception ex)
            {
                if (onError != null)

                    onError(ex.Message);
                return;
            }
        }

        public bool Connected()
        {
            try
            {
                return client.Connected;
            }
            catch (Exception ex)
            {
                if (onError != null)

                    onError(ex.Message);
                return false;
            }
        }
    }

private SocketsClass pClient = new SocketsClass ();

You can then use event handlers such as:

pClient.onConnect += this.Client_onConnect;

pClient.onDataArrival += this.Client_onDataArrival;

pClient.onError += this.Client_onError;

pClient.onDisconnect += this.Client_onDisconnect;

pClient.onSendComplete +=this.Client_onSendComplete;

Hope this is useful to people ;)

Took me ages to work this out

No tags Hide

Snippet of code to configure a web proxy

private static readonly IWebProxy sDefaultProxy = WebRequest.DefaultWebProxy;

public string ProxyUser = “”;
public string ProxyPassword = “”;
public bool ProxyUse = false;
public bool ProxyDefault = true;
public string ProxyAddress = “”;
public int ProxyPort = 80;

public static void ProxyConfig()
{
if (Controller.Config.Default.ProxyUse)
{
NetworkCredential credential = null;
if ((Controller.Config.Default.ProxyUser.Length > 0) ||
(Controller.Config.Default.ProxyPassword.Length > 0))
{
credential = new NetworkCredential();
credential.UserName = Controller.Config.Default.ProxyUser;
credential.Password = Controller.Config.Default.ProxyPassword;
}
if (!Controller.Config.Default.ProxyDefault)
{
var proxy = new WebProxy(Controller.Config.Default.ProxyAddress, Controller.Config.Default.ProxyPort);
if (credential != null)
{
proxy.Credentials = credential;
}
WebRequest.DefaultWebProxy = proxy;
}
else if (credential != null)
{
WebRequest.DefaultWebProxy.Credentials = credential;
}
}
else
{
WebRequest.DefaultWebProxy = sDefaultProxy;
}
}

public static void ProxyConfig()

{

if (ProxyUse)

{

NetworkCredential credential = null;

if ((ProxyUser.Length > 0) ||

(ProxyPassword.Length > 0))

{

credential = new NetworkCredential();

credential.UserName = ProxyUser;

credential.Password = ProxyPassword;

}

if (!ProxyDefault)

{

var proxy = new WebProxy(ProxyAddress, .ProxyPort);

if (credential != null)

{

proxy.Credentials = credential;

}

WebRequest.DefaultWebProxy = proxy;

}

else if (credential != null)

{

WebRequest.DefaultWebProxy.Credentials = credential;

}

}

else

{

WebRequest.DefaultWebProxy = sDefaultProxy;

}

}

No tags Hide

Oct/09

13

Ford KA – Stupid Engines!

Well, who has a Ford KA? The stupid engine in them is ridicules.

Well i finally fixed the annoying Misfire, 4 leads, 4 plugs and a coil pack! Well just after fixing this, exhaust is blowing….. then some twat borrows the KA, and takes the wing mirror off! MORE EXPENSE!

I JUST WANT TO SELL THE CAR!

http://tinyurl.com/fordka13

Buy it please ! :(

No tags Hide

Oct/09

11

About this blog

I will use this blog for posting about my Audi A4 – The mods and upgrades and pictures of it in progress, before and after etc.

Also any programming hints and tips and general Development news.

Then there is my personal bloggage….well thats easy!

No tags Hide

Oct/09

11

Reconditioned Engine!

Well, the car is finally in the garage having a reconditioned engine fitted, this includes new internals and reconditioned Turbo.

Once the car’s back, its Dump valve time, and start on the Bodywork!

Price for Reconditioned: £1,500!

Hopefully worth it, and remove the bottom end rattle!

No tags Hide

Find it!

Theme Design by devolux.org

Tag Cloud