Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Vb.Net Problema Metodo TCPListner.AcceptSocket()
Forum - C# / VB.NET - Vb.Net Problema Metodo TCPListner.AcceptSocket()

Pagine: [ 1 2 ] Precedente | Prossimo
Avatar
klez91 (Normal User)
Pro


Messaggi: 117
Iscritto: 04/05/2008

Segnala al moderatore
Postato alle 14:58
Martedì, 04/08/2009
Ciao a tutti, ho scritto una nuova classe MySocket ereditandola da System.Net.Sockets.Socket aggiungendo alcuni metodi e proprietà, solo che adesso quando provo ad accettare una connessione attraverso la funzione AcceptSocket() della classe TCPListener in questo modo:
Codice sorgente - presumibilmente VB.NET

  1. Private Listener As New System.Net.Sockets.TcpListener(256)
  2.     Private Client As MySocket
  3.  
  4.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  5.         If Listener.Pending Then
  6.             Client = Listener.AcceptSocket 'Qui ricevo l'eccezione...
  7.         End If
  8.     End Sub


...ricevo la seguente eccezione InvalidCastException - Impossibile eseguire il cast di oggetti di tipo 'System.Net.Sockets.Socket' sul tipo 'WindowsApplication1.MySocket', come del resto avrei dovuto aspettarmi a causa della conversione implicita tra System.Net.Sockets.Socket e MySocket. Come protrei risolvere questo problema? Grazie anticipatamente :k:

Ultima modifica effettuata da klez91 il 04/08/2009 alle 15:00
PM Quote
Avatar
manvb.net (Member)
Guru


Messaggi: 663
Iscritto: 28/01/2008

Segnala al moderatore
Postato alle 15:05
Martedì, 04/08/2009
Prova con un CType(Listener.AcceptSocket, MySocket).

Ultima modifica effettuata da manvb.net il 04/08/2009 alle 15:05
PM Quote
Avatar
klez91 (Normal User)
Pro


Messaggi: 117
Iscritto: 04/05/2008

Segnala al moderatore
Postato alle 15:46
Martedì, 04/08/2009
Si avevo provato già ma continua a darmi lo stesso errore...

PM Quote
Avatar
manvb.net (Member)
Guru


Messaggi: 663
Iscritto: 28/01/2008

Segnala al moderatore
Postato alle 17:04
Martedì, 04/08/2009
Ops, hai ragione, non ho considerato che MySocket è una classe fatta da te, quindi devi scrivere una funzione che, dato il risultato di AcceptSocket, lo converta in MySocket. Non posso dirti come implementarla perchè non so come è fatta la classe MySocket.

Ultima modifica effettuata da manvb.net il 04/08/2009 alle 17:05
PM Quote
Avatar
klez91 (Normal User)
Pro


Messaggi: 117
Iscritto: 04/05/2008

Segnala al moderatore
Postato alle 20:31
Martedì, 04/08/2009
Si scusa, non so se sia il caso di postarla perchè è abbastanza lunga ed inoltre contiene ancora molte imperfezioni, in ogni caso la classe è strutturata così:
Codice sorgente - presumibilmente VB.NET

  1. Imports System.Net.Sockets
  2. Public Class MySocket
  3.     Inherits System.Net.Sockets.Socket
  4.  
  5. Sub New(ByVal addressFamily As System.Net.Sockets.AddressFamily, ByVal socketType As System.Net.Sockets.SocketType, ByVal protocolType As System.Net.Sockets.ProtocolType)
  6.         MyBase.New(addressFamily, socketType, protocolType)
  7.     End Sub
  8.  
  9. 'Qui sono presenti diverse proprietà...
  10. 'diverse funzioni dichiarate come private...
  11. 'diverse sub dichiarate come public...
  12. 'il tutto si va ad aggiungere ai metodi e alle funzioni già presenti, non vine sostituito alcun metodo...
  13. End Class


Ti ringrazio per l'aiuto, se sarà necessario non esiterò a postare la classe interamente nel prossimo post. Granzie ancora ciao:k:

PM Quote
Avatar
ruggy94 (Member)
Guru


Messaggi: 890
Iscritto: 21/04/2008

Segnala al moderatore
Postato alle 20:53
Martedì, 04/08/2009
A quanto ho capito, non hai eseguito nessun override, quindi per convertire il tuo MySocket in Socket, basta che crei un nuovo socket e alle sue proprietà assegni il valore delle corrispondenti proprietà del tuo MySocket. (Correggetemi se sbaglio)

PM Quote
Avatar
klez91 (Normal User)
Pro


Messaggi: 117
Iscritto: 04/05/2008

Segnala al moderatore
Postato alle 22:07
Martedì, 04/08/2009
Si, infatti, non è stato eseguito nessun override, però non ho capito bene cosa intendi...

PM Quote
Avatar
ruggy94 (Member)
Guru


Messaggi: 890
Iscritto: 21/04/2008

Segnala al moderatore
Postato alle 22:12
Martedì, 04/08/2009
Testo quotato

Postato originariamente da klez91:

Si, infatti, non è stato eseguito nessun override, però non ho capito bene cosa intendi...


Forse la mia spiegazione è stata un po' intricata.
Sempre se ho ragione, quello che intendo è questo.
Codice sorgente - presumibilmente Delphi

  1. Public Class A
  2. public var1 as int16
  3. public var2 as string
  4. End Class



Codice sorgente - presumibilmente VB.NET

  1. Public Class B
  2. Inherits A
  3. public var3 as int32
  4. End Class



Codice sorgente - presumibilmente VB.NET

  1. Dim Prova as new B
  2. B.var3=5
  3. B.var2="ciao"
  4.  
  5. Function ConvertBtoA(X as B) as A
  6. Dim Test as new A
  7. Test.var1=X.var1
  8. Test.var2=X.Var2
  9. return Test
  10. End Function


PM Quote
Avatar
klez91 (Normal User)
Pro


Messaggi: 117
Iscritto: 04/05/2008

Segnala al moderatore
Postato alle 10:37
Mercoledì, 05/08/2009
Ti ringrazio per la risposta, ma comunque non sono riuscito a convertirla. Posto il sorgente della classe che ho scritto magari se gentilmente mi puoi dare una mano...
In pratica ho solamente aggiunto delle funzioni per crittografare la connessione...
Codice sorgente - presumibilmente VB.NET

  1. Imports System.Net.Sockets
  2.  
  3. Public Class MySocket
  4.     Inherits System.Net.Sockets.Socket
  5.  
  6.     Private _BytesPassword() As Byte = Nothing
  7.     Private _Authenticated As Boolean = False
  8.     Private Rsa As New System.Security.Cryptography.RSACryptoServiceProvider
  9.  
  10.     Public ReadOnly Property BytesPassword() As Byte()
  11.         Get
  12.             Return _BytesPassword
  13.         End Get
  14.     End Property
  15.  
  16.     Public ReadOnly Property Authenticated() As Boolean
  17.         Get
  18.             Return _Authenticated
  19.         End Get
  20.     End Property
  21.  
  22.     Sub New(ByVal addressFamily As System.Net.Sockets.AddressFamily, ByVal socketType As System.Net.Sockets.SocketType, ByVal protocolType As System.Net.Sockets.ProtocolType)
  23.         MyBase.New(addressFamily, socketType, protocolType)
  24.     End Sub
  25.  
  26.     Private IV32 As Byte() = New Byte() {133, 206, 56, 64, 110, 158, 132, 22, _
  27. 99, 190, 35, 129, 101, 49, 204, 248, 251, 243, 13, 194, 160, 195, 89, 152, 149, 227, 245, 5, 218, 86, 161, 124}
  28.  
  29.     Private Function Encrypt(ByVal Input() As Byte, ByVal Length As Integer) As Byte()
  30.         Dim Provider As New System.Security.Cryptography.RijndaelManaged
  31.         Dim BytePassword As Byte()
  32.         Dim Encryptor As System.Security.Cryptography.ICryptoTransform
  33.         Dim Output As Byte()
  34.  
  35.         Provider.KeySize = 256
  36.         Provider.BlockSize = 256
  37.         BytePassword = _BytesPassword
  38.         Encryptor = Provider.CreateEncryptor(BytePassword, IV32)
  39.         Output = Encryptor.TransformFinalBlock(Input, 0, Length)
  40.  
  41.         Provider.Clear()
  42.         Encryptor.Dispose()
  43.         Return Output
  44.     End Function
  45.  
  46.     Private Function Decrypt(ByVal Input() As Byte, ByVal Length As Integer) As Byte()
  47.         Dim Provider As New System.Security.Cryptography.RijndaelManaged
  48.         Dim BytePassword As Byte()
  49.         Dim Decryptor As System.Security.Cryptography.ICryptoTransform
  50.         Dim Output As Byte()
  51.  
  52.         Provider.KeySize = 256
  53.         Provider.BlockSize = 256
  54.         BytePassword = _BytesPassword
  55.         Decryptor = Provider.CreateDecryptor(BytePassword, IV32)
  56.  
  57.         Output = Decryptor.TransformFinalBlock(Input, 0, Length)
  58.         Provider.Clear()
  59.         Decryptor.Dispose()
  60.         Return Output
  61.     End Function
  62.  
  63.     Private Function GeneratePassword() As Byte()
  64.         Dim Key As String = ""
  65.         Dim Rnd As New Random
  66.  
  67.         For i As Byte = 0 To 11
  68.             Key &= Convert.ToChar(Rnd.Next(33, 126))
  69.         Next
  70.         Dim SaltBytes() As Byte = {162, 21, 92, 34, 27, 239, 64, 30, 136, 102, 223}
  71.         Dim Derive As New System.Security.Cryptography.Rfc2898DeriveBytes(Key, SaltBytes, 5)
  72.         Dim DerivedBytes() As Byte
  73.         DerivedBytes = Derive.GetBytes(32)
  74.         Return DerivedBytes
  75.  
  76.     End Function
  77.  
  78.     Public Sub AuthenticateAsClient()
  79.         If _Authenticated = False Then
  80.             Dim Bytes() As Byte = System.Text.UTF8Encoding.UTF8.GetBytes(Rsa.ToXmlString(False))
  81.             Send(Bytes, Bytes.Length, SocketFlags.None)
  82.             Dim Read As Integer = 0
  83.             Dim BytesPassword(ReceiveBufferSize) As Byte
  84.             Read = Receive(BytesPassword, BytesPassword.Length, SocketFlags.None)
  85.             Dim TempBytes(Read - 1) As Byte
  86.             For i As Integer = 0 To UBound(TempBytes)
  87.                 TempBytes(i) = Bytes(i)
  88.             Next
  89.             Dim Data As String = System.Text.UTF8Encoding.UTF8.GetString(TempBytes)
  90.             _BytesPassword = Convert.FromBase64String(Data)
  91.             _Authenticated = True
  92.         Else
  93.             Throw New Exception
  94.         End If
  95.     End Sub
  96.  
  97.     Public Sub AuthenticateAsServer()
  98.         If _Authenticated = False Then
  99.             Dim Read As Integer = 0
  100.             Dim Bytes(ReceiveBufferSize) As Byte
  101.             Read = Receive(Bytes, Bytes.Length, SocketFlags.None)
  102.             Dim TempBytes(Read - 1) As Byte
  103.             For i As Integer = 0 To UBound(TempBytes)
  104.                 TempBytes(i) = Bytes(i)
  105.             Next
  106.             Dim Data As String = System.Text.UTF8Encoding.UTF8.GetString(Bytes)
  107.             Dim Stringa() As String = Data.Split("|")
  108.             Dim Rsa As New System.Security.Cryptography.RSACryptoServiceProvider
  109.             Rsa.FromXmlString(Stringa(1))
  110.             _BytesPassword = Me.GeneratePassword()
  111.             Dim BytesPasswordEncrypted() As Byte = Rsa.Encrypt(BytesPassword, False)
  112.             Dim Password As String = Convert.ToBase64String(BytesPasswordEncrypted)
  113.             Send(System.Text.UTF8Encoding.UTF8.GetBytes(Password), Password.Length, SocketFlags.None)
  114.             _Authenticated = True
  115.         Else
  116.             Throw New Exception
  117.         End If
  118.     End Sub
  119.  
  120.     Public Function SendEncryptedBytes(ByVal buffer() As Byte, ByVal size As Integer, ByVal socketFlags As System.Net.Sockets.SocketFlags) As Integer
  121.         If _Authenticated = True Then
  122.             Dim Read As Integer = 0
  123.             Dim Output() As Byte = Encrypt(buffer, size)
  124.             Read = Send(Output, Output.Length, socketFlags)
  125.             Return Read
  126.         Else
  127.             Throw New Exception
  128.         End If
  129.     End Function
  130.  
  131.     Public Function ReceiveEncryptedBytes(ByRef buffer() As Byte, ByVal size As Integer, ByVal socketFlags As System.Net.Sockets.SocketFlags) As Integer
  132.         If _Authenticated = True Then
  133.             Dim Read As Integer = 0
  134.             Dim Input(size) As Byte
  135.             Read = Receive(Input, size, socketFlags)
  136.             buffer = Decrypt(Input, size)
  137.             Return Read
  138.         Else
  139.             Throw New Exception
  140.         End If
  141.     End Function
  142. End Class


Ultima modifica effettuata da klez91 il 05/08/2009 alle 10:38
PM Quote
Pagine: [ 1 2 ] Precedente | Prossimo