delivery-boy-cvs Mailing List for DeliveryBoy (Page 2)
Status: Planning
Brought to you by:
mkloubert
You can subscribe to this list here.
2005 |
Jan
|
Feb
(19) |
Mar
(6) |
Apr
(4) |
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
---|
From: Marcel K. <gen...@us...> - 2005-02-03 21:10:10
|
Update of /cvsroot/delivery-boy/client/DeliveryBoyC/classes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30983/DeliveryBoyC/classes Modified Files: clsEasySocket.cs Log Message: * added GarbageCollector code * added socket events to clsEasySocket * added AutoAccept property Index: clsEasySocket.cs =================================================================== RCS file: /cvsroot/delivery-boy/client/DeliveryBoyC/classes/clsEasySocket.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** clsEasySocket.cs 2 Feb 2005 20:50:19 -0000 1.5 --- clsEasySocket.cs 3 Feb 2005 21:10:00 -0000 1.6 *************** *** 45,48 **** --- 45,54 ---- public class clsEasySocket { + /// Default buffer size in byte + private const Int32 DEFAULT_BUFFER_SIZE = 1024; + + /// Byte buffer + private byte[] b_Buffer; + /// Last occurred exception private Exception m_oLastException = null; *************** *** 53,64 **** /// Socket private Socket m_oSocket = null; - /// Local address - private string m_strLocalAddr = ""; /// Local port private Int32 m_i32LocalPort = 0; - /// Local end point - private IPEndPoint m_LocalEndPoint = null; /// Remote address ! private string m_strRemoteAddr = ""; /// Remote port private Int32 m_i32RemotePort = 0; --- 59,66 ---- /// Socket private Socket m_oSocket = null; /// Local port private Int32 m_i32LocalPort = 0; /// Remote address ! private string m_strRemoteAddr = "0.0.0.0"; /// Remote port private Int32 m_i32RemotePort = 0; *************** *** 67,70 **** --- 69,76 ---- /// Listen queue private Int32 m_i32ListenQueue = -1; + /// Listen queue + private Int32 m_i32BufferSize = 0; + /// Auto accept? + private bool m_boolAutoAccept = true; /// Use this address type *************** *** 75,80 **** private const ProtocolType c_ProtocolType = ProtocolType.Tcp; ! /*! \fn Constructor ! * \brief Init all necessary stuff * * \param (none) --- 81,103 ---- private const ProtocolType c_ProtocolType = ProtocolType.Tcp; ! /// Accept-Event-Handler-Struct ! public delegate void AcceptHandler(System.Object sender, System.Net.Sockets.Socket socket); ! /// Accept-Event-Handler ! public event AcceptHandler OnAccept; ! /// Connect-Event-Handler-Struct ! public delegate void ConnectHandler(System.Object sender, System.Net.Sockets.Socket socket); ! /// Connect-Event-Handler ! public event ConnectHandler OnConnect; ! /// Error-Event-Handler-Struct ! public delegate void ErrorHandler(System.Object sender, System.Exception e); ! /// Error-Event-Handler ! public event ErrorHandler OnError; ! /// Receive-Event-Handler-Struct ! public delegate void ReceiveHandler(System.Object sender, System.Byte[] buffer, System.Int32 bytes_read); ! /// Receive-Event-Handler ! public event ReceiveHandler OnReceive; ! ! /*! \fn clsEasySocket ! * \brief Inits all necessary stuff * * \param (none) *************** *** 84,105 **** public clsEasySocket() { ! try ! { ! // initialize all necessary vars ! m_oASCII = new ASCIIEncoding(); ! ! SetLastException(0, null); ! m_oSocket = new Socket(c_AddressFamily, c_SocketType, c_ProtocolType); ! LocalAddress = "0.0.0.0"; ! LocalPort = 0; ! RemoteAddress = "0.0.0.0"; ! RemotePort = 0; ! ListenQueue = 1; ! } ! catch (Exception e) ! { ! SetLastException(1, e); ! } } --- 107,124 ---- public clsEasySocket() { ! // initialize all necessary vars ! m_oASCII = new ASCIIEncoding(); ! m_oSocket = new Socket(c_AddressFamily, c_SocketType, c_ProtocolType); ! ! RemoteAddress = "0.0.0.0"; ! RemotePort = 0; ! ListenQueue = 1; ! BufferSize = DEFAULT_BUFFER_SIZE; ! AutoAccept = true; ! ! b_Buffer = new byte[BufferSize]; ! ! SetLastException(0, null); } *************** *** 135,138 **** --- 154,158 ---- } + /*! \property public Int32 ListenQueue * \brief Returns the size of the listen queue or sets it. *************** *** 155,158 **** --- 175,198 ---- } + /*! \property public Int32 BufferSize + * \brief Returns the socket buffer size in bytes or sets it. + * + * \param value New size + * + * \return Size of buffer, in byte + */ + public Int32 BufferSize + { + get + { + return m_i32BufferSize; + } + + set + { + m_i32BufferSize = value; + } + } + /*! \property public Int32 RemotePort * \brief Returns the remote port or sets it. *************** *** 185,216 **** } ! /*! \property public string LocalAddress ! * \brief Returns the local address or sets it. * ! * \param value New local address * ! * \return Local address */ ! public string LocalAddress { get { ! return m_strRemoteAddr; ! } ! ! set ! { ! try ! { ! m_LocalEndPoint = GetEndPoint(value.Trim(), LocalPort); ! m_oSocket.Bind(m_LocalEndPoint); ! ! m_strLocalAddr = value.Trim(); ! SetLastException(0, null); ! } ! catch (Exception e) ! { ! SetLastException(1, e); ! } } } --- 225,240 ---- } ! /*! \property public Socket SocketObject ! * \brief Returns the socket object. * ! * \param (none) * ! * \return Socket object */ ! public Socket SocketObject { get { ! return m_oSocket; } } *************** *** 227,247 **** get { ! return m_i32RemotePort; } set { ! try ! { ! m_LocalEndPoint = GetEndPoint(LocalAddress, value); ! m_oSocket.Bind(m_LocalEndPoint); ! ! m_i32LocalPort = value; ! SetLastException(0, null); ! } ! catch (Exception e) ! { ! SetLastException(1, e); ! } } } --- 251,260 ---- get { ! return m_i32LocalPort; } set { ! m_i32LocalPort = value; } } *************** *** 258,262 **** get { ! return m_oSocket.Connected; } } --- 271,275 ---- get { ! return SocketObject.Connected; } } *************** *** 308,312 **** if (!Connected) { ! m_oSocket.Connect(m_RemoteEndPoint); return SetLastException(0, null); --- 321,325 ---- if (!Connected) { ! SocketObject.Connect(m_RemoteEndPoint); return SetLastException(0, null); *************** *** 321,326 **** } ! /*! \fn public UInt32 Disconnect() ! * \brief Disconnects. * * \param (none) --- 334,339 ---- } ! /*! \fn public UInt32 SocketObject() ! * \brief Disconnects or close socket. * * \param (none) *************** *** 331,335 **** * 2 => not connected */ ! public UInt32 Disconnect() { try --- 344,348 ---- * 2 => not connected */ ! public UInt32 Close() { try *************** *** 339,343 **** else { ! m_oSocket.Close(); return SetLastException(0, null); --- 352,356 ---- else { ! SocketObject.Close(); return SetLastException(0, null); *************** *** 350,357 **** } /*! \fn private IPEndPoint GetEndPoint (string str_Address, Int32 i32_Port) * \brief Creates an end point by an ip address and a port * ! * \param str_Address Address * \param i32_Port Port * --- 363,394 ---- } + /*! \fn public bool AutoAccept + * \brief Accept each connection request or first run OnAccept-event. + * + * \param value New state:<BR> + * TRUE => Accept each connection request<BR> + * FALSE => First run OnAccept event + * + * \return State:<BR> + * TRUE => Accept each connection request<BR> + * FALSE => First run OnAccept event + */ + public bool AutoAccept + { + get + { + return m_boolAutoAccept; + } + + set + { + m_boolAutoAccept = value; + } + } + /*! \fn private IPEndPoint GetEndPoint (string str_Address, Int32 i32_Port) * \brief Creates an end point by an ip address and a port * ! * \param string str_Address * \param i32_Port Port * *************** *** 362,366 **** try { ! // create end point IPAddress oIPAddress = Dns.Resolve(str_Address.Trim()).AddressList[0]; IPEndPoint oIPEndPoint = new IPEndPoint(oIPAddress, i32_Port); --- 399,403 ---- try { ! // create end point IPAddress oIPAddress = Dns.Resolve(str_Address.Trim()).AddressList[0]; IPEndPoint oIPEndPoint = new IPEndPoint(oIPAddress, i32_Port); *************** *** 418,422 **** if (Connected) { ! m_oSocket.Send(b_Bytes); return SetLastException(0, null); --- 455,459 ---- if (Connected) { ! SocketObject.Send(b_Bytes); return SetLastException(0, null); *************** *** 448,452 **** if (!Connected) { ! m_oSocket.Listen(ListenQueue); return SetLastException(0, null); --- 485,491 ---- if (!Connected) { ! SocketObject.Bind( new IPEndPoint(IPAddress.Any, LocalPort)); ! SocketObject.Listen(ListenQueue); ! SocketObject.BeginAccept( new AsyncCallback(this.pOnConnectRequest), SocketObject); return SetLastException(0, null); *************** *** 460,463 **** --- 499,578 ---- } } + + /*! \fn private void pOnConnectRequest ( IAsyncResult ar ) + * \brief Is invoked if a remote socket wants to connect + * + * \param ar Async result + * + * \return (none) + */ + private void pOnConnectRequest ( IAsyncResult ar ) + { + try + { + if (AutoAccept) + { + m_oSocket = SocketObject.EndAccept(ar); + + if (null != OnConnect) + this.OnConnect(this, m_oSocket); + + m_oSocket.BeginReceive(b_Buffer, 0, BufferSize, SocketFlags.None, new AsyncCallback(this.pOnReceive), m_oSocket); + } + else + { + if (null != OnAccept) + this.OnAccept(this, m_oSocket); + } + + } + catch (Exception e) + { + pOnError(e); + } + + return; + } + + /*! \fn private void pOnReceive ( IAsyncResult ar ) + * \brief Is invoked if a remote socket has sended data to this socket + * + * \param ar Async result + * + * \return (none) + */ + private void pOnReceive ( IAsyncResult ar ) + { + try + { + int iBytesRead = SocketObject.EndReceive(ar); + + if (null != OnReceive) + this.OnReceive(this, b_Buffer, iBytesRead); + + // b_Buffer = new byte[BufferSize]; + m_oSocket.BeginReceive(b_Buffer, 0, BufferSize, SocketFlags.None, new AsyncCallback(this.pOnReceive), m_oSocket); + } + catch (Exception e) + { + pOnError(e); + } + return; + } + + /*! \fn private void pOnReceive ( IAsyncResult ar ) + * \brief Is invoked if an error occurred + * + * \param ar Async result + * + * \return (none) + */ + private void pOnError ( Exception e ) + { + if (null != OnError) + this.OnError(this, e); + + return; + } } |
From: Marcel K. <gen...@us...> - 2005-02-03 21:10:09
|
Update of /cvsroot/delivery-boy/client/DeliveryBoyC In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30983/DeliveryBoyC Modified Files: DeliveryBoyC.prjx Main.cs Log Message: * added GarbageCollector code * added socket events to clsEasySocket * added AutoAccept property Index: DeliveryBoyC.prjx =================================================================== RCS file: /cvsroot/delivery-boy/client/DeliveryBoyC/DeliveryBoyC.prjx,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** DeliveryBoyC.prjx 30 Jan 2005 23:32:20 -0000 1.2 --- DeliveryBoyC.prjx 3 Feb 2005 21:09:59 -0000 1.3 *************** *** 9,13 **** <DeploymentInformation target="" script="" strategy="File" /> <Configuration runwithwarnings="True" name="Debug"> ! <CodeGeneration runtime="MsNet" compiler="Csc" compilerversion="" warninglevel="4" nowarn="" includedebuginformation="True" optimize="False" unsafecodeallowed="False" generateoverflowchecks="True" mainclass="" target="Exe" definesymbols="" generatexmldocumentation="False" win32Icon="" noconfig="False" nostdlib="False" /> <Execution commandlineparameters="" consolepause="True" /> <Output directory="..\..\bin\Debug" assembly="DeliveryBoyC" executeScript="" executeBeforeBuild="" executeAfterBuild="" executeBeforeBuildArguments="" executeAfterBuildArguments="" /> --- 9,13 ---- <DeploymentInformation target="" script="" strategy="File" /> <Configuration runwithwarnings="True" name="Debug"> ! <CodeGeneration runtime="MsNet" compiler="Csc" compilerversion="Standard" warninglevel="4" nowarn="" includedebuginformation="True" optimize="False" unsafecodeallowed="False" generateoverflowchecks="True" mainclass="" target="Exe" definesymbols="" generatexmldocumentation="False" win32Icon="" noconfig="False" nostdlib="False" /> <Execution commandlineparameters="" consolepause="True" /> <Output directory="..\..\bin\Debug" assembly="DeliveryBoyC" executeScript="" executeBeforeBuild="" executeAfterBuild="" executeBeforeBuildArguments="" executeAfterBuildArguments="" /> *************** *** 15,19 **** <Configurations active="Debug"> <Configuration runwithwarnings="True" name="Debug"> ! <CodeGeneration runtime="MsNet" compiler="Csc" compilerversion="" warninglevel="4" nowarn="" includedebuginformation="True" optimize="False" unsafecodeallowed="False" generateoverflowchecks="True" mainclass="" target="Exe" definesymbols="" generatexmldocumentation="False" win32Icon="" noconfig="False" nostdlib="False" /> <Execution commandlineparameters="" consolepause="True" /> <Output directory="..\..\bin\Debug" assembly="DeliveryBoyC" executeScript="" executeBeforeBuild="" executeAfterBuild="" executeBeforeBuildArguments="" executeAfterBuildArguments="" /> --- 15,19 ---- <Configurations active="Debug"> <Configuration runwithwarnings="True" name="Debug"> ! <CodeGeneration runtime="MsNet" compiler="Csc" compilerversion="Standard" warninglevel="4" nowarn="" includedebuginformation="True" optimize="False" unsafecodeallowed="False" generateoverflowchecks="True" mainclass="" target="Exe" definesymbols="" generatexmldocumentation="False" win32Icon="" noconfig="False" nostdlib="False" /> <Execution commandlineparameters="" consolepause="True" /> <Output directory="..\..\bin\Debug" assembly="DeliveryBoyC" executeScript="" executeBeforeBuild="" executeAfterBuild="" executeBeforeBuildArguments="" executeAfterBuildArguments="" /> Index: Main.cs =================================================================== RCS file: /cvsroot/delivery-boy/client/DeliveryBoyC/Main.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Main.cs 30 Jan 2005 23:45:36 -0000 1.3 --- Main.cs 3 Feb 2005 21:09:59 -0000 1.4 *************** *** 21,24 **** --- 21,26 ---- using System; + using System.Net; + using System.Net.Sockets; namespace DeliveryBoyC *************** *** 49,56 **** * \param args List of command line parameters. * ! * \return (none) ! */ public static void Main(string[] args) { bool bool_ExitDoWhile = false; string str_ConsoleInput = ""; --- 51,60 ---- * \param args List of command line parameters. * ! * \return (none) ! */ public static void Main(string[] args) { + /* sample socket test: clsEasySocket a = new clsEasySocket(); */ + bool bool_ExitDoWhile = false; string str_ConsoleInput = ""; *************** *** 58,61 **** --- 62,79 ---- PrintHeader(); + /* sample socket test: + + a.OnConnect += new clsEasySocket.ConnectHandler(oConnected); + a.LocalPort = 23; + a.ListenQueue = 10; + + + if (0 == a.Listen()) + { + Console.WriteLine("OK!"); + } + else + Console.WriteLine("ERROR!"); */ + do { *************** *** 78,81 **** --- 96,102 ---- while (!bool_ExitDoWhile); + // Clear all + GC.Collect(); + GC.WaitForPendingFinalizers(); return; } *************** *** 112,115 **** return; } ! } } --- 133,142 ---- return; } ! ! /* sample event ! public static void SocketConnected (System.Object sender, System.Net.Sockets.Socket socket) ! { ! return; ! } */ ! } } |
From: Marcel K. <gen...@us...> - 2005-02-02 20:50:30
|
Update of /cvsroot/delivery-boy/client/DeliveryBoyC/classes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1875/DeliveryBoyC/classes Modified Files: clsEasySocket.cs Log Message: * added Listen()-Method in clsEasySocket Index: clsEasySocket.cs =================================================================== RCS file: /cvsroot/delivery-boy/client/DeliveryBoyC/classes/clsEasySocket.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** clsEasySocket.cs 2 Feb 2005 20:37:10 -0000 1.4 --- clsEasySocket.cs 2 Feb 2005 20:50:19 -0000 1.5 *************** *** 39,43 **** * \remarks Last changed by: $Author$ in revision $Revision$ on $Date$ * ! * \todo Adding methods for connecting and listening, etc. * */ --- 39,43 ---- * \remarks Last changed by: $Author$ in revision $Revision$ on $Date$ * ! * \todo Adding methods for handling data transfer between sockets. * */ *************** *** 65,68 **** --- 65,70 ---- /// Remote end point private IPEndPoint m_RemoteEndPoint = null; + /// Listen queue + private Int32 m_i32ListenQueue = -1; /// Use this address type *************** *** 94,97 **** --- 96,100 ---- RemoteAddress = "0.0.0.0"; RemotePort = 0; + ListenQueue = 1; } catch (Exception e) *************** *** 132,135 **** --- 135,158 ---- } + /*! \property public Int32 ListenQueue + * \brief Returns the size of the listen queue or sets it. + * + * \param value New size + * + * \return Size of listen queue + */ + public Int32 ListenQueue + { + get + { + return m_i32ListenQueue; + } + + set + { + m_i32ListenQueue = value; + } + } + /*! \property public Int32 RemotePort * \brief Returns the remote port or sets it. *************** *** 270,274 **** /*! \fn public UInt32 Connect() ! * \brief Connects to RemoteIP:RemotePort. * * \param (none) --- 293,297 ---- /*! \fn public UInt32 Connect() ! * \brief Connects to RemoteAddress:RemotePort. * * \param (none) *************** *** 408,410 **** --- 431,463 ---- } } + + /*! \fn public UInt32 Listen() + * \brief Listen from for connections from LocalAddress:LocalPort. + * + * \param (none) + * + * \return Error code:<BR> + * 0 => no error<BR> + * 1 => general exception<br> + * 2 => already connected + */ + public UInt32 Listen() + { + try + { + if (!Connected) + { + m_oSocket.Listen(ListenQueue); + + return SetLastException(0, null); + } + else + return SetLastException(2, null); + } + catch (Exception e) + { + return SetLastException(1, e); + } + } + } |
From: Marcel K. <gen...@us...> - 2005-02-02 20:37:24
|
Update of /cvsroot/delivery-boy/client/DeliveryBoyC/classes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30346/DeliveryBoyC/classes Modified Files: clsEasySocket.cs Log Message: * removed namespace in clsEasySocket * added method Disconnect() to EasySocket-Class * some code optimizations Index: clsEasySocket.cs =================================================================== RCS file: /cvsroot/delivery-boy/client/DeliveryBoyC/classes/clsEasySocket.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** clsEasySocket.cs 31 Jan 2005 04:11:54 -0000 1.3 --- clsEasySocket.cs 2 Feb 2005 20:37:10 -0000 1.4 *************** *** 23,315 **** using System.Net; using System.Net.Sockets; ! namespace DeliveryBoyC.classes { ! /*! \class clsEasySocket clsEasySocket.cs "clsEasySocket.cs" ! * ! * \brief Easy-to-use socket class. ! * ! * This class is a simple version of the System.Net.Socket.Socket-Class ! * ! * \author generalpd ! * ! * \date 2005-01-30 ! * ! * \bug (none) ! * ! * \remarks Last changed by: $Author$ in Revision $Revision$ on $Date$ ! * ! * \todo Adding methods for connecting and listening, etc. ! * ! */ ! public class clsEasySocket ! { ! /// Last occurred exception ! private Exception m_oLastException = null; ! /// Socket ! private Socket m_oSocket = null; ! /// Local address ! private string m_strLocalAddr = ""; ! /// Local port ! private Int32 m_i32LocalPort = 0; ! /// Local end point ! private IPEndPoint m_LocalEndPoint = null; ! /// Remote address ! private string m_strRemoteAddr = ""; ! /// Remote port ! private Int32 m_i32RemotePort = 0; ! /// Remote end point ! private IPEndPoint m_RemoteEndPoint = null; ! /// Use this address type ! private const AddressFamily c_AddressFamily = AddressFamily.InterNetwork; ! private const SocketType c_SocketType = SocketType.Stream; ! private const ProtocolType c_ProtocolType = ProtocolType.Tcp; ! /*! \fn Constructor ! * \brief Init all necessary stuff ! * ! * \param (none) ! * ! * \return (none) ! */ ! public clsEasySocket() ! { ! try ! { ! // initialize all necessary vars ! m_oLastException = null; ! m_oSocket = new Socket(c_AddressFamily, c_SocketType, c_ProtocolType); ! LocalAddress = "0.0.0.0"; ! LocalPort = 0; ! RemoteAddress = "0.0.0.0"; ! RemotePort = 0; ! } ! catch (Exception e) ! { ! SetLastException(1, e); ! } ! } ! /*! \property public string RemoteAddress ! * \brief Returns the remote address or sets it. ! * ! * \param value New remote address ! * ! * \return Remote address ! */ ! public string RemoteAddress { ! get ! { ! return m_strRemoteAddr; ! } ! set { ! try ! { ! m_RemoteEndPoint = GetEndPoint(value.Trim(), RemotePort); ! m_strRemoteAddr = value.Trim(); ! SetLastException(0, null); ! } ! catch (Exception e) ! { ! SetLastException(1, e); ! } } } ! /*! \property public Int32 RemotePort ! * \brief Returns the remote port or sets it. ! * ! * \param value New remote port ! * ! * \return Remote port ! */ ! public Int32 RemotePort { ! get ! { ! return m_i32RemotePort; ! } ! set { ! try ! { ! m_RemoteEndPoint = GetEndPoint(RemoteAddress, value); ! m_i32RemotePort = value; ! SetLastException(0, null); ! } ! catch (Exception e) ! { ! SetLastException(1, e); ! } } } ! /*! \property public string LocalAddress ! * \brief Returns the local address or sets it. ! * ! * \param value New local address ! * ! * \return Local address ! */ ! public string LocalAddress { ! get ! { ! return m_strRemoteAddr; ! } ! set { ! try ! { ! m_LocalEndPoint = GetEndPoint(value.Trim(), LocalPort); ! m_oSocket.Bind(m_LocalEndPoint); ! m_strLocalAddr = value.Trim(); ! SetLastException(0, null); ! } ! catch (Exception e) ! { ! SetLastException(1, e); ! } } } ! /*! \property public Int32 LocalPort ! * \brief Returns the local port or sets it. ! * ! * \param value New local port ! * ! * \return Local port ! */ ! public Int32 LocalPort { ! get ! { ! return m_i32RemotePort; ! } ! set { ! try ! { ! m_LocalEndPoint = GetEndPoint(LocalAddress, value); ! m_oSocket.Bind(m_LocalEndPoint); ! m_i32LocalPort = value; ! SetLastException(0, null); ! } ! catch (Exception e) ! { ! SetLastException(1, e); ! } } } ! /*! \property public bool Connected ! * \brief Returns if this socket is connected or not. ! * ! * \param (none) ! * ! * \return Is connected (TRUE) or not (FALSE) ! */ ! public bool Connected { ! get ! { ! return m_oSocket.Connected; ! } } ! /*! \property public Exception LastException ! * \brief Returns the last occurred exception. ! * ! * \param (none) ! * ! * \return Exception object (null => no error) ! */ ! public Exception LastException { ! get ! { ! return m_oLastException; ! } } ! /*! \fn private UInt32 SetLastException (UInt32 ui32_ErrorCode, Exception oNewException) ! * \brief Sets the last occurred exception. ! * ! * \param ui32_ErrorCode Error code. ! * \param oNewException The new exception object. ! * ! * \return Error code submitted by ui32_ErrorCode ! */ ! private UInt32 SetLastException (UInt32 ui32_ErrorCode, Exception oNewException) ! { ! m_oLastException = oNewException; ! return ui32_ErrorCode; ! } ! /*! \fn public UInt32 Connect() ! * \brief Connects to RemoteIP:RemotePort. ! * ! * \param (none) ! * ! * \return Error code:<BR> ! * 0 => no error<BR> ! * 1 => general exception ! */ ! public UInt32 Connect() ! { ! try ! { ! m_oSocket.Connect(m_RemoteEndPoint); ! ! return SetLastException(0, null); ! } ! catch (Exception e) ! { ! return SetLastException(1, e); ! } ! } ! /*! \fn private IPEndPoint GetEndPoint (string str_Address, Int32 i32_Port) ! * \brief Creates an end point by an ip address and a port ! * ! * \param str_Address Address ! * \param i32_Port Port ! * ! * \return End point ... or null if an exception occurred ! */ ! private IPEndPoint GetEndPoint (string str_Address, Int32 i32_Port) ! { ! try { ! // create end point ! IPAddress oIPAddress = Dns.Resolve(str_Address).AddressList[0]; ! IPEndPoint oIPEndPoint = new IPEndPoint(oIPAddress, i32_Port); ! SetLastException(0, null); ! return oIPEndPoint; ! } ! catch (Exception e) ! { ! SetLastException(1, e); ! return null; ! } ! } } } --- 23,410 ---- using System.Net; using System.Net.Sockets; + using System.Text; ! /*! \class clsEasySocket clsEasySocket.cs "clsEasySocket.cs" ! * ! * \brief Easy-to-use socket class. ! * ! * This class is a simple version of the System.Net.Socket.Socket-Class ! * ! * \author generalpd ! * ! * \date 2005-01-30 ! * ! * \bug (none) ! * ! * \remarks Last changed by: $Author$ in revision $Revision$ on $Date$ ! * ! * \todo Adding methods for connecting and listening, etc. ! * ! */ ! ! public class clsEasySocket { ! /// Last occurred exception ! private Exception m_oLastException = null; ! /// For ASCII conversion ! private ASCIIEncoding m_oASCII = null; ! /// Socket ! private Socket m_oSocket = null; ! /// Local address ! private string m_strLocalAddr = ""; ! /// Local port ! private Int32 m_i32LocalPort = 0; ! /// Local end point ! private IPEndPoint m_LocalEndPoint = null; ! /// Remote address ! private string m_strRemoteAddr = ""; ! /// Remote port ! private Int32 m_i32RemotePort = 0; ! /// Remote end point ! private IPEndPoint m_RemoteEndPoint = null; ! /// Use this address type ! private const AddressFamily c_AddressFamily = AddressFamily.InterNetwork; ! /// Use this socket type ! private const SocketType c_SocketType = SocketType.Stream; ! /// Use this protocol type ! private const ProtocolType c_ProtocolType = ProtocolType.Tcp; ! /*! \fn Constructor ! * \brief Init all necessary stuff ! * ! * \param (none) ! * ! * \return (none) ! */ ! public clsEasySocket() ! { ! try ! { ! // initialize all necessary vars ! m_oASCII = new ASCIIEncoding(); ! SetLastException(0, null); ! ! m_oSocket = new Socket(c_AddressFamily, c_SocketType, c_ProtocolType); ! LocalAddress = "0.0.0.0"; ! LocalPort = 0; ! RemoteAddress = "0.0.0.0"; ! RemotePort = 0; ! } ! catch (Exception e) ! { ! SetLastException(1, e); ! } ! } ! /*! \property public string RemoteAddress ! * ! * \brief Returns the remote address or sets it. ! * ! * \param value New remote address ! * ! * \return Remote address ! */ ! public string RemoteAddress ! { ! get { ! return m_strRemoteAddr; ! } ! set ! { ! try { ! m_RemoteEndPoint = GetEndPoint(value.Trim(), RemotePort); ! m_strRemoteAddr = value.Trim(); ! SetLastException(0, null); ! } ! catch (Exception e) ! { ! SetLastException(1, e); } } + } ! /*! \property public Int32 RemotePort ! * \brief Returns the remote port or sets it. ! * ! * \param value New remote port ! * ! * \return Remote port ! */ ! public Int32 RemotePort ! { ! get { ! return m_i32RemotePort; ! } ! set ! { ! try { ! m_RemoteEndPoint = GetEndPoint(RemoteAddress, value); ! m_i32RemotePort = value; ! SetLastException(0, null); ! } ! catch (Exception e) ! { ! SetLastException(1, e); } } + } ! /*! \property public string LocalAddress ! * \brief Returns the local address or sets it. ! * ! * \param value New local address ! * ! * \return Local address ! */ ! public string LocalAddress ! { ! get { ! return m_strRemoteAddr; ! } ! set ! { ! try { ! m_LocalEndPoint = GetEndPoint(value.Trim(), LocalPort); ! m_oSocket.Bind(m_LocalEndPoint); ! m_strLocalAddr = value.Trim(); ! SetLastException(0, null); ! } ! catch (Exception e) ! { ! SetLastException(1, e); } } + } ! /*! \property public Int32 LocalPort ! * \brief Returns the local port or sets it. ! * ! * \param value New local port ! * ! * \return Local port ! */ ! public Int32 LocalPort ! { ! get { ! return m_i32RemotePort; ! } ! set ! { ! try { ! m_LocalEndPoint = GetEndPoint(LocalAddress, value); ! m_oSocket.Bind(m_LocalEndPoint); ! m_i32LocalPort = value; ! SetLastException(0, null); ! } ! catch (Exception e) ! { ! SetLastException(1, e); } } + } ! /*! \property public bool Connected ! * \brief Returns if this socket is connected or not. ! * ! * \param (none) ! * ! * \return Is connected (TRUE) or not (FALSE) ! */ ! public bool Connected ! { ! get { ! return m_oSocket.Connected; } + } ! /*! \property public Exception LastException ! * \brief Returns the last occurred exception. ! * ! * \param (none) ! * ! * \return Exception object (null => no error) ! */ ! public Exception LastException ! { ! get { ! return m_oLastException; } + } ! /*! \fn private UInt32 SetLastException (UInt32 ui32_ErrorCode, Exception oNewException) ! * \brief Sets the last occurred exception. ! * ! * \param ui32_ErrorCode Error code. ! * \param oNewException The new exception object. ! * ! * \return Error code submitted by ui32_ErrorCode ! */ ! private UInt32 SetLastException (UInt32 ui32_ErrorCode, Exception oNewException) ! { ! m_oLastException = oNewException; ! return ui32_ErrorCode; ! } ! /*! \fn public UInt32 Connect() ! * \brief Connects to RemoteIP:RemotePort. ! * ! * \param (none) ! * ! * \return Error code:<BR> ! * 0 => no error<BR> ! * 1 => general exception<br> ! * 2 => already connected ! */ ! public UInt32 Connect() ! { ! try ! { ! if (!Connected) ! { ! m_oSocket.Connect(m_RemoteEndPoint); ! ! return SetLastException(0, null); ! } ! else ! return SetLastException(2, null); ! } ! catch (Exception e) ! { ! return SetLastException(1, e); ! } ! } ! /*! \fn public UInt32 Disconnect() ! * \brief Disconnects. ! * ! * \param (none) ! * ! * \return Error code:<BR> ! * 0 => no error<BR> ! * 1 => general exception<br> ! * 2 => not connected ! */ ! public UInt32 Disconnect() ! { ! try ! { ! if (!Connected) ! return SetLastException(2, null); ! else { ! m_oSocket.Close(); ! ! return SetLastException(0, null); ! } ! } ! catch (Exception e) ! { ! return SetLastException(1, e); ! } ! } ! ! /*! \fn private IPEndPoint GetEndPoint (string str_Address, Int32 i32_Port) ! * \brief Creates an end point by an ip address and a port ! * ! * \param str_Address Address ! * \param i32_Port Port ! * ! * \return End point ... or null if an exception occurred ! */ ! private IPEndPoint GetEndPoint (string str_Address, Int32 i32_Port) ! { ! try ! { ! // create end point ! IPAddress oIPAddress = Dns.Resolve(str_Address.Trim()).AddressList[0]; ! IPEndPoint oIPEndPoint = new IPEndPoint(oIPAddress, i32_Port); ! SetLastException(0, null); ! return oIPEndPoint; ! } ! catch (Exception e) ! { ! SetLastException(1, e); ! return null; ! } } + + /*! \fn public UInt32 SendString(string str_Expression) + * \brief Sends a string to the remote host. + * + * \param str_Expression The string that should be send + * + * \return Error code:<BR> + * 0 => no error<BR> + * 1 => general exception<BR> + * 2 => not connected + */ + public UInt32 SendString(string str_Expression) + { + try + { + byte[] bBuffer = m_oASCII.GetBytes(str_Expression); + + return SetLastException(Send(bBuffer), LastException); + } + catch (Exception e) + { + return SetLastException(1, e); + } + } + + /*! \fn public UInt32 Send(byte[] b_Bytes) + * \brief Sends a couple of bytes to the remote host. + * + * \param b_Bytes Array of bytes + * + * \return Error code:<BR> + * 0 => no error<BR> + * 1 => general exception<BR> + * 2 => not connected + */ + public UInt32 Send(byte[] b_Bytes) + { + try + { + if (Connected) + { + m_oSocket.Send(b_Bytes); + + return SetLastException(0, null); + } + else + // Not connected + return SetLastException(2, null); + } + catch (Exception e) + { + return SetLastException(1, e); + } + } } |