No, I don't think there's too much complexity in the connection
itself, but you must divide the objects' responsabilities right to
have good dreams
Creating a connection and sending information is easy, a bit harder is
to test the transmition problems when the application is running.
Think earlier in the maintenance work and you'll have no problems
bye.
--- Sorry for my english, did you understood anything?
I'm pretty sure I understand what you're saying here. I found some
code for a Socket connection...I have modified it slightly from what
I'm pasting, is this a good start, though?
public class Socket
{
//our socket
private Socket sock;
//our buffer to receive bytes
private Byte[] buffer;
//ascii encoding for converting bytes to stings, etc
private ASCIIEncoding ascii;
//AsyncCallBack objects for Async Socker usage
private AsyncCallback ConnectCallBack;
private AsyncCallback ReceiveCallBack;
private AsyncCallback SendCallBack;
public Socket()
{
//create socket
sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
//create a buffer of size 4095
buffer = new Byte[4095];
//create ascii encoding object
ascii = new ASCIIEncoding();
//create async objects to tell which method to go to
when invoked
ConnectCallBack = new
AsyncCallback(sock_ConnectCallBack);
ReceiveCallBack = new
AsyncCallback(sock_ReceiveCallBack);
SendCallBack = new AsyncCallback(sock_SendCallBack);
}
public void Connect(String IP)
{
//parse IP
IPAddress ip = Dns.GetHostEntry(IP).AddressList[0];
//create IPEndPoint from ip and Port
IPEndPoint ep = new IPEndPoint(ip, _Port);
//begin connecting to other user
sock.BeginConnect(ep, ConnectCallBack, null);
}
public void Connect(IPAddress IP)
{
//parse IP
IPAddress ip = Dns.GetHostEntry(IP).AddressList[0];
//create IPEndPoint from ip and Port
IPEndPoint ep = new IPEndPoint(ip, _Port);
//begin connecting to other user
sock.BeginConnect(ep, ConnectCallBack, null);
}
private void sock_ConnectCallBack(IAsyncResult ar)
{
/* finalize the connection and begin receiving
information from
* the user, passing in our AsyncCallBack object
*/
sock.EndConnect(ar);
sock.BeginReceive(buffer, 0, buffer.Length,
SocketFlags.None, ReceiveCallBack, null);
}
private void sock_ReceiveCallBack(IAsyncResult ar)
{
//get the number of bytes received
int numBytes = sock.EndReceive(ar);
/* if numBytes < 1 then other user disconnected from
session,
* so shutdown socket and exit method
*/
if (numBytes < 1)
{
sock.Shutdown(SocketShutdown.Both);
return;
}
//if buffer is null, exit method
if (buffer == null)
return;
/* get string from bytes (can also write the bytes to
a file,
* all bytes received are in the array named buffer)
*/
String str = ascii.GetString(buffer);
/* clear the array so if the array isnt filled next
time,
* we wont have extra info
*/
Array.Clear(buffer, 0, buffer.Length);
//begin received again
sock.BeginReceive(buffer, 0, buffer.Length,
SocketFlags.None, ReceiveCallBack, null);
}
public void Send(Byte[] bt)
{
/* begin sending to connected client, passing our
AsyncDelegate
* telling where to go when the send is complete
*/
sock.BeginSend(bt, 0, bt.Length, SocketFlags.None,
SendCallBack, null);
}
private void sock_SendCallBack(IAsyncResult ar)
{
//handle send finalize here...
sock.EndSend(ar);
}
}
I will be modifying this code to actually send a serialized, encrypted
object to make it a little more secure. Is there any other way to
make the connection itself more secure?
Thank you for your input!
-mac