CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    9

    Question Socket Multiple Connection VB.NET

    hey all ,

    I wish to send message to multiple computers (LAN network).
    each computer in lab is running the server except one computer which is the client.
    problem : once message has been sent to first computer , the client stops to send to other computers.

    CLIENT :

    Dim ip As String
    Dim i As Integer
    Dim serverStream As NetworkStream
    Dim outStream As Byte()

    Dim counter As Integer = 0
    For i = 0 To 100

    Try
    ip = txtRange.Text & i

    clientSocket.Connect(ip, 8888)

    If clientSocket.Connected = True Then

    serverStream = clientSocket.GetStream()
    outStream = System.Text.Encoding.ASCII.GetBytes("Message from the client$")
    serverStream.Write(outStream, 0, outStream.Length)
    serverStream.Flush()

    End If

    Catch ex As Exception
    End Try
    Next

    SERVER :

    Dim serverSocket As New TcpListener(8888)
    Dim requestCount As Integer
    Dim clientSocket As TcpClient
    serverSocket.Start()
    clientSocket = serverSocket.AcceptTcpClient()

    While (true)

    Dim networkStream As NetworkStream = clientSocket.GetStream()
    Dim bytesFrom(10024) As Byte
    networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
    Dim dataFromClient As String = System.Text.Encoding.ASCII.GetString(bytesFrom)
    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
    MessageBox.Show("Data from client - " + dataFromClient)
    End While


    clientSocket.Close()
    serverSocket.Stop()

    thank you

  2. #2
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Re: Socket Multiple Connection VB.NET

    Assuming that the packets are flowing ok to the first client without issue, then the issue is most likely caused by trying to re-use an open connection. You need to close the connection before continuing to the next client:

    Code:
    ...
    serverStream.Write(outStream, 0, outStream.Length)
    serverStream.Flush()
    
    serverStream.Dispose()
    clientSocket.Close()
    ...
    Catching the exception should also give you some insight in to the problem, as opposed to just ignoring it:

    Code:
    Catch ex As Exception
    '
    'Do something here with ex object
    '
    End Try
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  3. #3
    Join Date
    Oct 2009
    Posts
    9

    Re: Socket Multiple Connection VB.NET

    Craig Gemmill thank you for your help.

    I tried your code of closing serverStream and Client.

    but I got this message :


    A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 192.168.14.0:8888

    Now I searched google and I found this solution :

    clientSocket.SendTimeout = 1000
    clientSocket.ReceiveTimeout = 1000

    I tried them , But I still get the same error message :

    A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 192.168.14.0:8888


    thanks.

  4. #4
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Re: Socket Multiple Connection VB.NET

    Post your code and where the error is encountered. Are you sure the first client is successfully receiving the data?
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

Tags for this Thread

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

Featured