VB.NET Socket - Multiple clients to server

U

User

Hi,

I tried to find the information over the internet but didn't find any
answers.

I'm looking for a server side code example of winsock accepting many
clients. I know that in VB.NET it is not implemented like in VB6. What
I've done is one client per socket. Meaning that each client used a
different port on the server, but I find it annoying.

I would like to have all clients to connect to the server using one port.

How can I do that? Where can I find an example?

Thank very much!
 
J

Jon Skeet [C# MVP]

User said:
I tried to find the information over the internet but didn't find any
answers.

I'm looking for a server side code example of winsock accepting many
clients. I know that in VB.NET it is not implemented like in VB6. What
I've done is one client per socket. Meaning that each client used a
different port on the server, but I find it annoying.

I would like to have all clients to connect to the server using one port.

Just because they're using different sockets doesn't mean that clients
have to connect to different ports. Just use TcpListener and call keep
AcceptTcpClient or AcceptSocket and everything will be fine.
 
U

User

I thought that a port number was unique to one socket? Is that right?

In my implementation, I use the port number as an identifier for the
client connection. If I use one socket, is there a unique identifier
for any accepted connection?

Thank you!
 
M

Michael D. Ober

In TCP communications, the listener application binds to an advertised port.
When a client connects to that port, the IP Stack sends a port number to the
server. The "accept" call binds that port as well as a new port on the
server and then sends back the new port on the server. Basically what
happens is that the communications link is taken to a random port pair - one
on the client and one on the server. Accept then resets the listener to
wait for the next connection request. To keep track of your clients, you
simply need to keep track of the sockets used by accept.

Mike Ober.
 
W

William Stacey [MVP]

A socket is unique with 4 pieces of info:
1) server ip
2) server port
3) client ip
4) client port

So you have many sockets all connecting to same server ip and port as long
as client ip/port is different, which they will be. If the same client trys
to connect with same local port twice, it will fail locally trying to grab a
local socket that is using same ip and port (IIRC).
 
J

Jon Skeet [C# MVP]

User said:
I thought that a port number was unique to one socket? Is that right?

I can't remember exactly how it works, but I believe there's the port
number as far as the client is concerned, and the "internal" port
number. One external port number can have many sockets connected, each
with a different "internal" port number.

I could be very wrong though. Hopefully someone who's looked at the
details more recently than me can give more accurate information.
In my implementation, I use the port number as an identifier for the
client connection.

Not a good idea, I suspect.
If I use one socket, is there a unique identifier for any accepted
connection?

There's the Socket object itself...
 
U

User

Let's say that I run this raw code.

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 would I connect a "datareceive event" from any client?

Thanks you!






In TCP communications, the listener application binds to an advertised port.
When a client connects to that port, the IP Stack sends a port number to the
server. The "accept" call binds that port as well as a new port on the
server and then sends back the new port on the server. Basically what
happens is that the communications link is taken to a random port pair - one
on the client and one on the server. Accept then resets the listener to
wait for the next connection request. To keep track of your clients, you
simply need to keep track of the sockets used by accept.

Mike Ober.

I thought that a port number was unique to one socket? Is that right?

In my implementation, I use the port number as an identifier for the
client connection. If I use one socket, is there a unique identifier
for any accepted connection?

Thank you!



Jon Skeet [C# MVP] wrote:

I tried to find the information over the internet but didn't find any
answers.

I'm looking for a server side code example of winsock accepting many
clients. I know that in VB.NET it is not implemented like in VB6. What
I've done is one client per socket. Meaning that each client used a
different port on the server, but I find it annoying.

I would like to have all clients to connect to the server using one
port.
Just because they're using different sockets doesn't mean that clients
have to connect to different ports. Just use MyTcpListener and call keep
AcceptTcpClient or AcceptSocket and everything will be fine.
 
U

User

Let's say that I run this raw code.

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 would I connect a "datareceive event" from any client?

Thanks you!
 
U

User

Let's say that I run this raw code.

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 would I connect a "datareceive event" from any client?

Thanks you!
 

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