CAT | Programming
/*
* 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
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 (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;
}
}
