neves92 0 Posted March 28, 2011 Report Share Posted March 28, 2011 Estou a tentar fazer um servidor em modo design com código baseado numa consola, mas quando clico no botão iniciar da erro na seguinte linha: ClienteSoket = Servidor.AcceptTcpClient Private Sub BT_Iniciar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_Iniciar.Click Try Dim ClienteSoket As TcpClient = Nothing Dim Contador As Integer = 0 Servidor.Start() Me.Hide() TB_Reguistos.Text = "==> Servidor ligado com sucesso!!! <==" NotifyIcon1.BalloonTipText = "O servidor foi ligado com sucesso" NotifyIcon1.ShowBalloonTip(2000) Me.Refresh() Contador += 1 ClienteSoket = Servidor.AcceptTcpClient Dim BytesFrom(10024) As Byte Dim DadosCliente As String Dim NetworkStream As NetworkStream = ClienteSoket.GetStream NetworkStream.Read(BytesFrom, 0, CInt(ClienteSoket.ReceiveBufferSize)) DadosCliente = Encoding.ASCII.GetString(BytesFrom) 'Esta função seleciona deste o index 0 ate o index X, onde X é um index tambe. 'A função DadosCliente.IndexOf("$") procura na String DadosCliente o index do primeiro cararter $ e devolve o Index do mesmo DadosCliente = DadosCliente.Substring(0, DadosCliente.IndexOf("$")) 'Adiciona o cliete a Lista de Clientes onde o se "BI" é o que esta dentro de parentece o o seu valor é o que esta a ser atribuido ListaClientes(DadosCliente) = ClienteSoket broadcast(DadosCliente & " Entrou ", DadosCliente, False) NotifyIcon1.BalloonTipText = DadosCliente & "Ligou-se ao servidor" TB_Reguistos.Text = TB_Reguistos.Text & vbCrLf & DadosCliente & " Entro no Chat... " Dim cliente As New TratarCliente cliente.iniciaCliente(ClienteSoket, DadosCliente) Flag = True BWorker.CancelAsync() Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical, "Erro") End Try End Sub Link to post Share on other sites
ribeiro55 47 Posted March 28, 2011 Report Share Posted March 28, 2011 Deves sempre indicar o erro que te dá, mas neste caso eu até o vou adivinhar: NullReferenceException was unhandled: Object reference not set to an instance of an object. Acontece que estás a inicializar ClienteSoket a Nothing, e nunca lhe atribuís uma referência válida ou uma nova instância. EDIT: Bem, a menos que Servidor.AcceptTcpClient devolva um objecto desse tipo, o que eu duvido. Indica o erro que te é devolvido. Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips" Link to post Share on other sites
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now