eawedat
eawedat

Reputation: 417

Socket Multiple Connection VB.NET

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.

Upvotes: 0

Views: 2432

Answers (1)

Roland Bär
Roland Bär

Reputation: 1730

The client connects but never disconnects. So the second time you try to connect the socket is already connected and throws therefore an exception. This exception is catched but ignored.

So you should change 2 things:

  1. Write the Exception at least to System.Diagnostics.Debug to see the exception while running in Visual Studio
  2. Disconnect the client socket after using and create a new socket before connecting again

Upvotes: 1

Related Questions