Socket - Need help for Multiple clients to server using VB.NET

U

User

Hi,

Let's say that I run this raw code. This is on a server side, the
server will wait for client connection request.

Private MyTcpListener As System.Net.Sockets.TcpListener
Private MySocket As Socket

Dim MyPort as int32
MyPort = 1000

Dim ipHostInfo As IPHostEntry = Dns.GetHostByName(Dns.GetHostName()) Dim
MyIPAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(MyIPAddress, MyPort)

MyTcpListener = New Net.Sockets.MyTcpListener(localEndPoint)
MyTcpListener.Start()

[...]

'Looping periodically on MyTcpListener to see if any client try to
' connect
If Not (MyTcpListener.Pending()) Then
Exit Sub
else
MySocket = MyTcpListener.AcceptSocket
End If

For a new connection MySocket will containt information about the new
client information (port, IP).

If I want many clients being able to connect to this server, do I have
to create an array of MySocket? Then any new accepted client would be
indexed (from i=0 to n, n=max connect allowed). That index would be
useful for me.

And I wouldn't have to care anymore with port number? Is that right?

How could I connect a "datareceive event" from any client?

Thanks you very much!
 
G

Guest

Hi there
I don’t recommend you create an array instead think of creating a class
which will handle the connection with the client, and you can store each
client object in a hash table with the user name as the key. For more info
you can check .net 101 sample - How to use sockets

Anas H.
 
U

User

This is very useful, thanks you !


Hi there
I don’t recommend you create an array instead think of creating a class
which will handle the connection with the client, and you can store each
client object in a hash table with the user name as the key. For more info
you can check .net 101 sample - How to use sockets

Anas H.

:

Hi,

Let's say that I run this raw code. This is on a server side, the
server will wait for client connection request.

Private MyTcpListener As System.Net.Sockets.TcpListener
Private MySocket As Socket

Dim MyPort as int32
MyPort = 1000

Dim ipHostInfo As IPHostEntry = Dns.GetHostByName(Dns.GetHostName()) Dim
MyIPAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(MyIPAddress, MyPort)

MyTcpListener = New Net.Sockets.MyTcpListener(localEndPoint)
MyTcpListener.Start()

[...]

'Looping periodically on MyTcpListener to see if any client try to
' connect
If Not (MyTcpListener.Pending()) Then
Exit Sub
else
MySocket = MyTcpListener.AcceptSocket
End If

For a new connection MySocket will containt information about the new
client information (port, IP).

If I want many clients being able to connect to this server, do I have
to create an array of MySocket? Then any new accepted client would be
indexed (from i=0 to n, n=max connect allowed). That index would be
useful for me.

And I wouldn't have to care anymore with port number? Is that right?

How could I connect a "datareceive event" from any client?

Thanks you very much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top