Results 1 to 11 of 11

Thread: TCP server-multiclients problem

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    8

    Question TCP server-multiclients problem

    From Atheist example and msdn examples i managed to do this small application : i can send and receive text from a client to a server and back.
    The problem is i got stuck on : making multiple client connexions , identify the clients and can send msg to a certain client . any help pls ?
    i use vb 2009
    The server source code :
    ---------------------------
    Code:
    Imports System.Net.Sockets
    Imports System.Net.Sockets
    Public Class Form1
        Dim server As TcpListener
        Dim port As Int32 = 13000
        '  Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
        Dim bytes(1024) As Byte
        Dim data As String = Nothing
        Dim stream As NetworkStream
        Dim client As TcpClient
        Dim listenThread As System.Threading.Thread
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                server = New TcpListener(System.Net.IPAddress.Any, port)
                server.Start()
                Label1.Text = "server up"
                listenThread = New System.Threading.Thread(AddressOf doListen)
                listenThread.IsBackground = True
                listenThread.Start()
            Catch ex As Exception
                Label1.Text = "server down"
            End Try
    
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
            stream = client.GetStream()
            stream.Write(msg, 0, msg.Length)
            TextBox1.Text = ""
        End Sub
        Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
            server.Stop()
            client.Close()
        End Sub
        Private Delegate Sub StringDelegate(ByVal text As String)
        Private Sub SetConnectionLabelText(ByVal text As String)
            If Me.TextBox2.InvokeRequired Then
                Me.Invoke(New StringDelegate(AddressOf SetConnectionLabelText), text)
            Else
                Me.TextBox2.Text = text
            End If
        End Sub
        Private Sub doListen()
            client = server.AcceptTcpClient()
            Do
                data = Nothing
                stream = client.GetStream()
                Dim iq As Int32
                iq = stream.Read(bytes, 0, bytes.Length)
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, iq)
                SetConnectionLabelText(data)
            Loop
        End Sub
    End Class
    -------------------------------------------------------------------
    the client source code:
    -----------------------
    Code:
    Imports System.Net.Sockets
    Public Class Form1
        Dim port As Integer = 13000
        Dim server As String
        Dim client As TcpClient
        Dim stream As NetworkStream
        Dim listenThread As System.Threading.Thread
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                server = TextBox3.Text
                client = New TcpClient(server, port)
                stream = client.GetStream()
                Label1.Text = "connected"
                listenThread = New System.Threading.Thread(AddressOf doListen)
                listenThread.IsBackground = True
                listenThread.Start()
            Catch ex As Exception
                Label1.Text = "not connected"
            End Try
    
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
            stream.Write(data, 0, data.Length)
            TextBox1.Text = ""
        End Sub
        Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
            client.Close()
        End Sub
        Private Delegate Sub StringDelegate(ByVal text As String)
        Private Sub SetConnectionLabelText(ByVal text As String)
            If Me.TextBox2.InvokeRequired Then
                Me.Invoke(New StringDelegate(AddressOf SetConnectionLabelText), text)
            Else
                Me.TextBox2.Text = text
            End If
        End Sub
        Private Sub doListen()
            Do
                Dim data As [Byte]() = New [Byte](256) {}
                Dim responseData As [String] = [String].Empty
                Dim bytes As Int32 = stream.Read(data, 0, data.Length)
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
                SetConnectionLabelText(responseData)
            Loop
        End Sub
    End Class
    Last edited by ancientrd; Apr 10th, 2009 at 09:07 AM.

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