CHAT EM VISUAL BASIC
tenho esse codigo para chat em vb que está funcionando perfeitamente
a duvida é como criar no servidor um cadastro de usuarios e senhas e quando o cliente conectar pedir usuario e senha
queria que quando ablir a aplicação pedir a senha e o login
Imports System.Net
Imports System.Text
Imports System.Net.Sockets
Module Module1
Dim listaClientes As New Hashtable
Sub Main()
Dim enderecoLocal As IPAddress = IPAddress.Parse([Ô]127.0.0.1[Ô])
Dim serverSocket As New TcpListener(enderecoLocal, 8888)
Dim clientSocket As TcpClient = Nothing
Dim contador As Integer
serverSocket.Start()
Mensagem([Ô]Servidor Iniciado ....[Ô])
contador = 0
While (True)
contador += 1
clientSocket = serverSocket.AcceptTcpClient()
Dim bytesFrom(10024) As Byte
Dim dadosDoCliente As String
Dim networkStream As NetworkStream = clientSocket.GetStream()
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
dadosDoCliente = Encoding.ASCII.GetString(bytesFrom)
dadosDoCliente = dadosDoCliente.Substring(0, dadosDoCliente.IndexOf([Ô]$[Ô]))
listaClientes(dadosDoCliente) = clientSocket
broadcast(dadosDoCliente + [Ô] Entrou [Ô], dadosDoCliente, False)
Mensagem(dadosDoCliente + [Ô] Entrou na Sala [Ô])
Dim cliente As New tratarCliente
cliente.iniciaCliente(clientSocket, dadosDoCliente, listaClientes)
End While
clientSocket.Close()
serverSocket.Stop()
Mensagem([Ô]sair[Ô])
Console.ReadLine()
End Sub
Sub Mensagem(ByVal texto As String)
texto.Trim()
Console.WriteLine([Ô] >> [Ô] + texto)
End Sub
Private Sub broadcast(ByVal Mensagem As String, ByVal nomeUsuario As String, ByVal flag As Boolean)
Dim Item As DictionaryEntry
For Each Item In listaClientes
Dim broadcastSocket As TcpClient
broadcastSocket = CType(Item.Value, TcpClient)
Try
Dim broadcastStream As NetworkStream = broadcastSocket.GetStream()
Dim broadcastBytes As [Byte]()
If flag = True Then
broadcastBytes = Encoding.ASCII.GetBytes(nomeUsuario + [Ô] diz : [Ô] + Mensagem)
Else
broadcastBytes = Encoding.ASCII.GetBytes(Mensagem)
End If
broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
broadcastStream.Flush()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Next
End Sub
Public Class tratarCliente
Dim clientSocket As TcpClient
Dim clNo As String
Dim listaClientes As Hashtable
Public Sub iniciaCliente(ByVal inClientSocket As TcpClient, ByVal clineNo As String, ByVal cList As Hashtable)
Me.clientSocket = inClientSocket
Me.clNo = clineNo
Me.listaClientes = cList
Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
ctThread.Start()
End Sub
Private Sub doChat()
Dim contadorRequisicao As Integer
Dim bytesFrom(10024) As Byte
Dim dadosDoCliente As String
Dim rContador As String
contadorRequisicao = 0
While (True)
Try
contadorRequisicao = contadorRequisicao + 1
Dim networkStream As NetworkStream = clientSocket.GetStream()
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
dadosDoCliente = System.Text.Encoding.ASCII.GetString(bytesFrom)
dadosDoCliente = dadosDoCliente.Substring(0, dadosDoCliente.IndexOf([Ô]$[Ô]))
Mensagem([Ô]Cliente - [Ô] + clNo + [Ô] : [Ô] + dadosDoCliente)
rContador = Convert.ToString(contadorRequisicao)
broadcast(dadosDoCliente, clNo, True)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End While
End Sub
End Class
End Module
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Dim clientSocket As New TcpClient()
Dim serverStream As NetworkStream
Dim lerDados As String
Private Sub btnConectar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConectar.Click
lerDados = [Ô]Conetado com o Servidor ...[Ô]
Mensagem()
Try
clientSocket.Connect([Ô]127.0.0.1[Ô], 8888)
serverStream = clientSocket.GetStream()
Dim outStream As Byte() = Encoding.ASCII.GetBytes(txtNome.Text + [Ô]$[Ô])
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
[ô]cria uma nova thread para enviar mensagens
Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf getMensagem)
ctThread.Start()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Mensagem()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf Mensagem))
Else
txtDados.Text = txtDados.Text + Environment.NewLine + [Ô] >> [Ô] + lerDados
End If
End Sub
Private Sub btnEnviarMensagem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnviarMensagem.Click
Try
Dim outStream As Byte() = Encoding.ASCII.GetBytes(txtMensagem.Text + [Ô]$[Ô])
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub getMensagem()
[ô]loop infinito
While (True)
Try
serverStream = clientSocket.GetStream()
Dim buffSize As Integer
Dim inStream(10024) As Byte
buffSize = clientSocket.ReceiveBufferSize
serverStream.Read(inStream, 0, buffSize)
Dim dadosRetornados As String = Encoding.ASCII.GetString(inStream)
lerDados = [Ô][Ô] + dadosRetornados
Mensagem()
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
End While
End Sub
End Class
a duvida é como criar no servidor um cadastro de usuarios e senhas e quando o cliente conectar pedir usuario e senha
queria que quando ablir a aplicação pedir a senha e o login
Imports System.Net
Imports System.Text
Imports System.Net.Sockets
Module Module1
Dim listaClientes As New Hashtable
Sub Main()
Dim enderecoLocal As IPAddress = IPAddress.Parse([Ô]127.0.0.1[Ô])
Dim serverSocket As New TcpListener(enderecoLocal, 8888)
Dim clientSocket As TcpClient = Nothing
Dim contador As Integer
serverSocket.Start()
Mensagem([Ô]Servidor Iniciado ....[Ô])
contador = 0
While (True)
contador += 1
clientSocket = serverSocket.AcceptTcpClient()
Dim bytesFrom(10024) As Byte
Dim dadosDoCliente As String
Dim networkStream As NetworkStream = clientSocket.GetStream()
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
dadosDoCliente = Encoding.ASCII.GetString(bytesFrom)
dadosDoCliente = dadosDoCliente.Substring(0, dadosDoCliente.IndexOf([Ô]$[Ô]))
listaClientes(dadosDoCliente) = clientSocket
broadcast(dadosDoCliente + [Ô] Entrou [Ô], dadosDoCliente, False)
Mensagem(dadosDoCliente + [Ô] Entrou na Sala [Ô])
Dim cliente As New tratarCliente
cliente.iniciaCliente(clientSocket, dadosDoCliente, listaClientes)
End While
clientSocket.Close()
serverSocket.Stop()
Mensagem([Ô]sair[Ô])
Console.ReadLine()
End Sub
Sub Mensagem(ByVal texto As String)
texto.Trim()
Console.WriteLine([Ô] >> [Ô] + texto)
End Sub
Private Sub broadcast(ByVal Mensagem As String, ByVal nomeUsuario As String, ByVal flag As Boolean)
Dim Item As DictionaryEntry
For Each Item In listaClientes
Dim broadcastSocket As TcpClient
broadcastSocket = CType(Item.Value, TcpClient)
Try
Dim broadcastStream As NetworkStream = broadcastSocket.GetStream()
Dim broadcastBytes As [Byte]()
If flag = True Then
broadcastBytes = Encoding.ASCII.GetBytes(nomeUsuario + [Ô] diz : [Ô] + Mensagem)
Else
broadcastBytes = Encoding.ASCII.GetBytes(Mensagem)
End If
broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
broadcastStream.Flush()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Next
End Sub
Public Class tratarCliente
Dim clientSocket As TcpClient
Dim clNo As String
Dim listaClientes As Hashtable
Public Sub iniciaCliente(ByVal inClientSocket As TcpClient, ByVal clineNo As String, ByVal cList As Hashtable)
Me.clientSocket = inClientSocket
Me.clNo = clineNo
Me.listaClientes = cList
Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
ctThread.Start()
End Sub
Private Sub doChat()
Dim contadorRequisicao As Integer
Dim bytesFrom(10024) As Byte
Dim dadosDoCliente As String
Dim rContador As String
contadorRequisicao = 0
While (True)
Try
contadorRequisicao = contadorRequisicao + 1
Dim networkStream As NetworkStream = clientSocket.GetStream()
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
dadosDoCliente = System.Text.Encoding.ASCII.GetString(bytesFrom)
dadosDoCliente = dadosDoCliente.Substring(0, dadosDoCliente.IndexOf([Ô]$[Ô]))
Mensagem([Ô]Cliente - [Ô] + clNo + [Ô] : [Ô] + dadosDoCliente)
rContador = Convert.ToString(contadorRequisicao)
broadcast(dadosDoCliente, clNo, True)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End While
End Sub
End Class
End Module
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Dim clientSocket As New TcpClient()
Dim serverStream As NetworkStream
Dim lerDados As String
Private Sub btnConectar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConectar.Click
lerDados = [Ô]Conetado com o Servidor ...[Ô]
Mensagem()
Try
clientSocket.Connect([Ô]127.0.0.1[Ô], 8888)
serverStream = clientSocket.GetStream()
Dim outStream As Byte() = Encoding.ASCII.GetBytes(txtNome.Text + [Ô]$[Ô])
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
[ô]cria uma nova thread para enviar mensagens
Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf getMensagem)
ctThread.Start()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Mensagem()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf Mensagem))
Else
txtDados.Text = txtDados.Text + Environment.NewLine + [Ô] >> [Ô] + lerDados
End If
End Sub
Private Sub btnEnviarMensagem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnviarMensagem.Click
Try
Dim outStream As Byte() = Encoding.ASCII.GetBytes(txtMensagem.Text + [Ô]$[Ô])
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub getMensagem()
[ô]loop infinito
While (True)
Try
serverStream = clientSocket.GetStream()
Dim buffSize As Integer
Dim inStream(10024) As Byte
buffSize = clientSocket.ReceiveBufferSize
serverStream.Read(inStream, 0, buffSize)
Dim dadosRetornados As String = Encoding.ASCII.GetString(inStream)
lerDados = [Ô][Ô] + dadosRetornados
Mensagem()
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
End While
End Sub
End Class
Eu tenho um aonde cada usuario pode criar suas salas, e so entra quem tiver a senha e assim por diante...Tente banco mysql...
Se tiver usando em rede pode fazer a autenticação direto no AD (Active Directory), você pode criar um grupo e se o usuário não fizer parte desse grupo não autentica.
Tópico encerrado , respostas não são mais permitidas