Oppure

Loading
Questo topic e' stato chiuso dal moderatore.
25/07/08 12:39
ProgrMan93
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:
Imports System.Net.Sockets

Public Class Form1
    Public Listener As New Net.Sockets.TcpListener(25)
    Public Client As TcpClient
    Public NetStr As NetworkStream


    Private Sub ControlConnection_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ControlConnection.Tick
        If Listener.Pending Then
            Client = Listener.AcceptTcpClient
            NetStr = Client.GetStream
            ControlConnection.Stop()
            Listener.Stop()
            GetData.Start()
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Listener.Start()
    End Sub

    Private Sub GetData_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetData.Tick
        If Client.Connected Then
            If Client.Available > 0 And NetStr.CanRead Then
                Dim Bytes(Client.ReceiveBufferSize) As Byte
                Client.ReceiveBufferSize = 8129
                NetStr.Read(Bytes, 0, Client.ReceiveBufferSize)

                Dim x As String = System.Text.ASCIIEncoding.ASCII.GetString(Bytes)
                MsgBox(x)
            End If
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Client.Connected Then
            If NetStr.CanWrite Then
                Dim Bytes() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(TextBox1.Text)
                NetStr.Write(Bytes, 0, Bytes.Length)
            End If
        End If
    End Sub
End Class


CLient:
Imports System.Net.Sockets
Public Class Form1
    Public Client As TcpClient
    Public NetStr As NetworkStream

    Private Sub GetData_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetData.Tick
        If Client.Connected Then
            If Client.Available > 0 And NetStr.CanRead Then
                Dim Bytes(Client.ReceiveBufferSize) As Byte
                NetStr.Read(Bytes, 0, Client.ReceiveBufferSize)

                Dim y As String = System.Text.ASCIIEncoding.ASCII.GetString(Bytes)
                MsgBox(y)
            End If
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Client.Connected Then
            If NetStr.CanWrite Then
                Dim Bytes() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(TextBox1.Text)
                NetStr.Write(Bytes, 0, Bytes.Length)
            End If
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Client.Connect("127.0.0.1", 25)
        If Client.Connected Then
            NetStr = Client.GetStream()
            GetData.Start()
        End If
    End Sub
End Class

Ultima modifica effettuata da ProgrMan93 25/07/08 12:40
aaa
25/07/08 14:32
klez91
Sia nel server che nel client hai dimenticato di utilizzare il costruttore New nella dichiarazione del TCPClient. Prova così:

Dim Client As New TCPClient


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

Ciao :k:
aaa
26/07/08 9:16
Il Totem
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.
aaa
26/07/08 15:37
ProgrMan93
Avete ragione! nn l'avevo proprio visto!:d
Ultima modifica effettuata da ProgrMan93 26/07/08 15:47
aaa