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
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
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.
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?