Socket Multiple Connection

eawedat

Member
Joined
Nov 20, 2011
Messages
8
Programming Experience
1-3
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.
 

eawedat

Member
Joined
Nov 20, 2011
Messages
8
Programming Experience
1-3
I did this update to the CLIENT section
I've added : clientSocket.Close()
but I still get same result -> only one pc gets the message and client stops to send to another .

CLIENT :

Code:
[COLOR=#00008B]Dim[/COLOR] ip [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]String[/COLOR]
[COLOR=#00008B]Dim[/COLOR] i [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Integer[/COLOR]
[COLOR=#00008B]Dim[/COLOR] serverStream [COLOR=#00008B]As[/COLOR] NetworkStream
[COLOR=#00008B]Dim[/COLOR] outStream [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Byte[/COLOR]()

[COLOR=#00008B]Dim[/COLOR] counter [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Integer[/COLOR] = [COLOR=#800000]0[/COLOR]
[COLOR=#00008B]For[/COLOR] i = [COLOR=#800000]0[/COLOR] [COLOR=#00008B]To[/COLOR] [COLOR=#800000]100[/COLOR]

[COLOR=#00008B]Try[/COLOR]
ip = txtRange.Text & i

clientSocket.Connect(ip, [COLOR=#800000]8888[/COLOR])

[COLOR=#00008B]If[/COLOR] clientSocket.Connected = [COLOR=#800000]True[/COLOR] [COLOR=#00008B]Then[/COLOR]

serverStream = clientSocket.GetStream()
outStream = System.Text.Encoding.ASCII.GetBytes([COLOR=#800000]"Message from the client$"[/COLOR])
serverStream.Write(outStream, [COLOR=#800000]0[/COLOR], outStream.Length)
serverStream.Flush()
[/B]
clientSocket.Close()[B]
[COLOR=#00008B]End[/COLOR] [COLOR=#00008B]If[/COLOR]

[COLOR=#00008B]Catch[/COLOR] ex [COLOR=#00008B]As[/COLOR] Exception
[COLOR=#00008B]End[/COLOR] [COLOR=#00008B]Try[/COLOR]
[COLOR=#00008B]Next
[/COLOR][/B]
[B]
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,283
Location
Sydney, Australia
Programming Experience
10+
You can't connect to a new server with the same Socket without disconnecting from the old one first.
The documentation for the Close method says this:
Closes the Socket connection and releases all associated resources.
The documentation for the Disconnect method says this:
Closes the socket connection and allows reuse of the socket.
This is an example of why you should always read the documentation and make sure that you read it thoroughly when trying to work out how to do something new.
 

eawedat

Member
Joined
Nov 20, 2011
Messages
8
Programming Experience
1-3
jmcilhinney thank you for your help.

I've replaced clientSocket.Close() with

clientSocket.Client.Disconnect(True)

I get same results :S

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()

clientSocket.Client.Disconnect(True)

End If

Catch ex As Exception
End Try
Next
 

eawedat

Member
Joined
Nov 20, 2011
Messages
8
Programming Experience
1-3
I get 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
I searched google and I got this solution :

clientSocket.SendTimeout = 1000
clientSocket.ReceiveTimeout = 1000
But still getting the same 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
 
Top Bottom