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 - Ennesimo problema Sockets [VB.Net]
Forum - C# / VB.NET - Ennesimo problema Sockets [VB.Net]

Avatar
ProgrMan93 (Ex-Member)
Pro


Messaggi: 68
Iscritto: 25/06/2008

Segnala al moderatore
Postato alle 14:39
Venerdì, 25/07/2008
Questo topic è stato chiuso dal moderatore

Ho realizzo un applicazione (server - Client) per chattare però quando tento di connettermi al server viene generata un'eccezione Null Reference Exception
Ecco il codice
Server:
Codice sorgente - presumibilmente VB.NET

  1. Imports System.Net.Sockets
  2.  
  3. Public Class Form1
  4.     Public Listener As New Net.Sockets.TcpListener(25)
  5.     Public Client As TcpClient
  6.     Public NetStr As NetworkStream
  7.  
  8.  
  9.     Private Sub ControlConnection_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ControlConnection.Tick
  10.         If Listener.Pending Then
  11.             Client = Listener.AcceptTcpClient
  12.             NetStr = Client.GetStream
  13.             ControlConnection.Stop()
  14.             Listener.Stop()
  15.             GetData.Start()
  16.         End If
  17.     End Sub
  18.  
  19.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  20.         Listener.Start()
  21.     End Sub
  22.  
  23.     Private Sub GetData_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetData.Tick
  24.         If Client.Connected Then
  25.             If Client.Available > 0 And NetStr.CanRead Then
  26.                 Dim Bytes(Client.ReceiveBufferSize) As Byte
  27.                 Client.ReceiveBufferSize = 8129
  28.                 NetStr.Read(Bytes, 0, Client.ReceiveBufferSize)
  29.  
  30.                 Dim x As String = System.Text.ASCIIEncoding.ASCII.GetString(Bytes)
  31.                 MsgBox(x)
  32.             End If
  33.         End If
  34.     End Sub
  35.  
  36.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  37.         If Client.Connected Then
  38.             If NetStr.CanWrite Then
  39.                 Dim Bytes() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(TextBox1.Text)
  40.                 NetStr.Write(Bytes, 0, Bytes.Length)
  41.             End If
  42.         End If
  43.     End Sub
  44. End Class



CLient:
Codice sorgente - presumibilmente VB.NET

  1. Imports System.Net.Sockets
  2. Public Class Form1
  3.     Public Client As TcpClient
  4.     Public NetStr As NetworkStream
  5.  
  6.     Private Sub GetData_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetData.Tick
  7.         If Client.Connected Then
  8.             If Client.Available > 0 And NetStr.CanRead Then
  9.                 Dim Bytes(Client.ReceiveBufferSize) As Byte
  10.                 NetStr.Read(Bytes, 0, Client.ReceiveBufferSize)
  11.  
  12.                 Dim y As String = System.Text.ASCIIEncoding.ASCII.GetString(Bytes)
  13.                 MsgBox(y)
  14.             End If
  15.         End If
  16.     End Sub
  17.  
  18.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  19.         If Client.Connected Then
  20.             If NetStr.CanWrite Then
  21.                 Dim Bytes() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(TextBox1.Text)
  22.                 NetStr.Write(Bytes, 0, Bytes.Length)
  23.             End If
  24.         End If
  25.     End Sub
  26.  
  27.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  28.         Client.Connect("127.0.0.1", 25)
  29.         If Client.Connected Then
  30.             NetStr = Client.GetStream()
  31.             GetData.Start()
  32.         End If
  33.     End Sub
  34. End Class



Ultima modifica effettuata da ProgrMan93 il 25/07/2008 alle 14:40
PM
Avatar
klez91 (Normal User)
Pro


Messaggi: 117
Iscritto: 04/05/2008

Segnala al moderatore
Postato alle 16:32
Venerdì, 25/07/2008
Sia nel server che nel client hai dimenticato di utilizzare il costruttore New nella dichiarazione del TCPClient. Prova così:

Codice sorgente - presumibilmente C# / VB.NET

  1. Dim Client As New TCPClient



P.S. - Totem mi sa che devi aggiustarlo anche tu sul tuo sito :D:D:rotfl:

Ciao :k:

PM
Avatar
Il Totem (Admin)
Guru^2


Messaggi: 3635
Iscritto: 24/01/2006

Segnala al moderatore
Postato alle 11:16
Sabato, 26/07/2008
Ti ricordo che Client, nel server viene inizializzato dal metodo Listener.AcceptTcpClient, quindi non è necessario il costruttore lì. Lo è solo nel Client, ma evidentemente mi sono dimenticato di inserirlo nel Form1_Load.
Comunque mi stupisco sempre che non si riesca a risolvere questi problemi.

PM
Avatar
ProgrMan93 (Ex-Member)
Pro


Messaggi: 68
Iscritto: 25/06/2008

Segnala al moderatore
Postato alle 17:37
Sabato, 26/07/2008
Avete ragione! nn l'avevo proprio visto!:d

Ultima modifica effettuata da ProgrMan93 il 26/07/2008 alle 17:47
PM