1 Replies - 904 Views - Last Post: 21 August 2013 - 10:39 PM Rate Topic: -----

#1 jkoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-July 13

Chat with specific Client

Posted 21 August 2013 - 04:17 AM

i have a client-server application chat.
the server have an ability to get connected with multiple clients.

the server code start with asynchronous 'new listener' with specific ip address and port.
after that, client connect to server with tcp client to server's ip address and server's port.
every client that are connected to server, will be added to new list of class 'ChatClient'

now with my code, every client can read and write message from and to another clients, just like broadcast.
server is also can broadcast to every single clients.

my idea is, every time client connect to the server, the client's remoteipendpoint will be add to listbox on server application. and than server can choose from listbox, which one of client that the server want to chat.

my question is, how to server only can communicate with one client?
i already looking for that about 5 days, but getting no cleared answer :helpsmilie: :helpsmilie::helpsmilie:

here is my server code :
Imports System.Net
Imports System.Net.Sockets
Imports System.IO

Public Class frmLogin
    Dim serverport As Int32
    Dim server As TcpListener
    Dim cclient As ChatClient
    Dim ClientList As New List(Of ChatClient)
    Dim IDToFind As String


    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        Try
            server = Nothing
            serverport = numServerPort.Text
            server = New TcpListener(IPAddress.Any, serverport)
            server.Start()
            rtbMessage.Text = "[INFO] - SERVER ONLINE - ON " & numServerPort.Text & "@ " & DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt" & vbNewLine)
            tsServerStatus.Text = "Server Online"
            btnStart.Enabled = False
            btnStop.Enabled = True
            server.BeginAcceptTcpClient(New AsyncCallback(AddressOf DoAcceptTcpClientCallback), server)
        Catch
            tsServerStatus.Text = "Server Error"
            btnStart.Enabled = True
            btnStop.Enabled = False
        End Try
    End Sub
    
    Private Sub DoAcceptTcpClientCallback(ar As IAsyncResult)
        cclient = New ChatClient(server.EndAcceptTcpClient(ar))
        AddHandler (cclient.MessageRecieved), AddressOf MessageRecieved
        AddHandler (cclient.ClientExited), AddressOf ClientExited
        ClientList.Add(cclient)
        Dim clientip As String
        clientip = ChatClient.ipaddressclient
        updateclient(clientip)
        xUpdate("New Client Joined", False)
        server.BeginAcceptTcpClient(New AsyncCallback(AddressOf DoAcceptTcpClientCallback), server)
       
    End Sub
    
    Sub MessageRecieved(ByVal Str As String)
        xUpdate(Str, False)
    End Sub
    
    Sub ClientExited(ByVal Client As ChatClient)
        ClientList.Remove(Client)
        xUpdate("Client Exited", False)
    End Sub
    
    Delegate Sub _updateclient(ByVal Str As String)
    Sub updateclient(ByVal Str As String)
        On Error Resume Next
        If InvokeRequired Then
            Invoke(New _updateclient(AddressOf updateclient), Str)
        Else
            lsvclient.Items.Add(Str)
        End If
    End Sub
    
    Delegate Sub _xUpdate(ByVal Str As String, ByVal Relay As Boolean)
    Sub xUpdate(ByVal Str As String, ByVal Relay As Boolean)
        On Error Resume Next
        If InvokeRequired Then
            Invoke(New _xUpdate(AddressOf xUpdate), Str, Relay)
        Else
            rtbMessage.AppendText(Str & vbNewLine)
            If Relay Then Send(Str)
        End If
    End Sub
    
    Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
        xUpdate("Server Says: " & TextBox1.Text, True)
        TextBox1.Clear()
    End Sub
    
    Sub Send(ByVal Str As String)
        For i As Integer = 0 To ClientList.Count - 1
            Try
                ClientList(i).Send(Str)
            Catch
                ClientList.RemoveAt(i)
            End Try
        Next
    End Sub
    
    
    Private Sub btnPrivate_Click(sender As Object, e As EventArgs) Handles btnPrivate.Click
        


    End Sub



End Class



here is my ChatClient class code
Imports System.Net.Sockets, System.IO
Imports System.Net

Public Class ChatClient

    Public Event MessageRecieved(ByVal Str As String)
    Public Event ClientExited(ByVal Client As ChatClient)
    Public Shared ipaddressclient As String

    Private sWriter As StreamWriter
    Private Client As TcpClient
    

    Sub New(ByVal xclient As TcpClient)
        Client = xclient
        ipaddressclient = Client.Client.RemoteEndPoint.ToString
        Client.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
        'Dim clientip As String = (IPAddress.Parse(CType(Client.Client.RemoteEndPoint, IPEndPoint).Address.ToString())).ToString()
        'Dim clientport As String = (CType(Client.Client.RemoteEndPoint, IPEndPoint).Port.ToString())
    End Sub

    Private Sub Read()
        Try
            Dim userreq As String = New StreamReader(Client.GetStream).ReadLine
            RaiseEvent MessageRecieved(userreq)
            Client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf Read), Nothing)
            Call Process(userreq)
        Catch ex As Exception
            RaiseEvent ClientExited(Me)
        End Try
    End Sub

    Public Sub Send(ByVal Message As String)
        sWriter = New StreamWriter(Client.GetStream)
        sWriter.WriteLine(Message)
        sWriter.Flush()
    End Sub

    Private Sub Process(userreq As String)
        If userreq = "1" Then
            Send("one")
            RaiseEvent MessageRecieved("one")
        Else
            Send("unknown")
            RaiseEvent MessageRecieved("unknown")
        End If
    End Sub

    Public Function clientInfo()
        ipaddressclient = Client.Client.RemoteEndPoint.ToString
        Return ipaddressclient
    End Function

End Class



here is client application code :
Imports System.IO, System.Net, System.Net.Sockets
Imports System.Text

Public Class frmMain

    Dim Client As TcpClient
    Dim sWriter As StreamWriter
    
    Sub Read(ByVal ar As IAsyncResult)
        Try
            xUpdate(New StreamReader(Client.GetStream).ReadLine)
            Client.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
        Catch
            xUpdate("You have been disconnected from the server.")
            Exit Sub
        End Try
    End Sub

    Private Sub Send(ByVal Str As String)
        Try
            sWriter = New StreamWriter(Client.GetStream)
            sWriter.WriteLine(Str)
            sWriter.Flush()
        Catch
            xUpdate("You are not the server.")
        End Try
    End Sub

    Delegate Sub _xUpdate(ByVal Str As String)
    Sub xUpdate(ByVal Str As String)
        If InvokeRequired Then
            Invoke(New _xUpdate(AddressOf xUpdate), Str)
        Else
            RichTextBox1.AppendText(Str & vbNewLine)
        End If
    End Sub

    Private Sub btnconnect_Click(sender As Object, e As EventArgs) Handles btnconnect.Click
        Try
            Client = New TcpClient(txtAddress.Text, CInt(txtPort.Text))
            Client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf Read), Nothing)
            btnconnect.Enabled = False
            btnDisconnect.Enabled = True
        Catch
            xUpdate("Can not connect to the server.")
        End Try
    End Sub

    Private Sub btnDisconnect_Click(sender As Object, e As EventArgs) Handles btnDisconnect.Click
        Client.Client.Close()
        Client = Nothing
        btnconnect.Enabled = True
        btnDisconnect.Enabled = False
    End Sub

    Private Sub btnsend_Click(sender As Object, e As EventArgs)
        Send(NickPrefix & " Says: " & RichTextBox2.Text)
        RichTextBox2.Clear()
    End Sub
      
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Chat with specific Client

#2 jkoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-July 13

Re: Chat with specific Client

Posted 21 August 2013 - 10:39 PM

someone please..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1