Пример #1
0
 void Start()
 {
     Socket sock = Sockets.CreateTCPSocket("www.myserver.com", 2345);
     client = new ClientInfo(sock, false); // Don't start receiving yet
     client.OnReadBytes += new ConnectionReadBytes(ReadData);
     client.BeginReceive();
 }
Пример #2
0
 public void Start()
 {
     Socket sock = Sockets.CreateTCPSocket("167.205.66.53", 2345);
     client = new ClientInfo(sock, false); // Don't start receiving yet
     client.MessageType = MessageType.CodeAndLength;
     client.OnReadMessage += new ConnectionReadMessage(ReadMessage);
     client.BeginReceive();
     mcConnect = true;
 }
Пример #3
0
        // ASYNC CALLBACK CODE
        void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                Socket server = (Socket)ar.AsyncState;
                Socket cs = server.EndAccept(ar);

                // Start the thing listening again
                server.BeginAccept(new AsyncCallback(AcceptCallback), server);

                ClientInfo c = new ClientInfo(cs, null, null, ClientDirection.Both, false);
                c.server = this;
                // Allow the new client to be rejected by the application
                if (Connect != null)
                {
                    if (!Connect(this, c))
                    {
                        // Rejected
                        cs.Close();
                        return;
                    }
                }
                // Initiate key exchange
                c.EncryptionType = encType;
                switch (encType)
                {
                    case EncryptionType.None: KeyExchangeComplete(c); break;
                    case EncryptionType.ServerKey:
                        c.encKey = GetSymmetricKey();
                        byte[] key = ClientInfo.GetLengthEncodedVector(c.encKey);
                        cs.Send(key);
#if DEBUG
                        Console.Write(c.ID + " Sent key: "); ClientInfo.LogBytes(key, key.Length);
#endif
                        c.MakeEncoders();
                        KeyExchangeComplete(c);
                        break;
                    case EncryptionType.ServerRSAClientKey:
                        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                        RSAParameters p = rsa.ExportParameters(true);
                        cs.Send(ClientInfo.GetLengthEncodedVector(p.Modulus));
                        cs.Send(ClientInfo.GetLengthEncodedVector(p.Exponent));
                        c.encParams = p;
                        break;
                    default: throw new ArgumentException("Unknown or unsupported encryption type " + encType);
                }
                lock (SyncRoot) clients[c.ID] = c;
                c.BeginReceive();
            }
            catch (ObjectDisposedException) { }
            catch (SocketException) { }
            catch (Exception e) { Console.WriteLine(e); }
        }
Пример #4
0
 public void Start()
 {
     Socket sock = Sockets.CreateTCPSocket("localhost", 2345);
     client = new ClientInfo(sock, false); // Don't start receiving yet
     client.MessageType = MessageType.Length;
     client.OnReadMessage += new ConnectionReadMessage(ReadData);
     client.BeginReceive();
 }
Пример #5
0
 void Start()
 {
     Socket sock = Sockets.CreateTCPSocket("localhost", 2345);
     client = new ClientInfo(sock, false); // Don't start receiving yet
     client.OnRead += new ConnectionRead(ReadData);
     client.Delimiter = "\n";  // this is the default, shown for illustration
     client.BeginReceive();
 }