socket.EndAccept(ar) is Exclusive....any way to change?

G

Guest

Hi, im making an AIM like server using TCP sockets, however i found a big
issue. when a socket is connected to (socket.EndAccept(ar)), its already
bound to a Port. since its bound, i cannot change its access to non
exclusive, but i need this port to still be open because i may need another
socket to connect on this port as well. how can i achieve this?
 
T

Tom Shelton

Hi, im making an AIM like server using TCP sockets, however i found a big
issue. when a socket is connected to (socket.EndAccept(ar)), its already
bound to a Port. since its bound, i cannot change its access to non
exclusive, but i need this port to still be open because i may need another
socket to connect on this port as well. how can i achieve this?

You may need to explain this situation a little more clearly (maybe some short
code samples would be helpfull).
 
M

Martin

From what I remember of doing something similar a few years ago; You have a
'listening' socket and every time that socket detects an incoming connection
you open a new socket on a different port and make the connection on that
port. That way you can continue to listen on the original socket.

I don't have code samples to hand anymore i'm afraid.

HTH
 
T

Tom Shelton

From what I remember of doing something similar a few years ago; You have a
'listening' socket and every time that socket detects an incoming connection
you open a new socket on a different port and make the connection on that
port. That way you can continue to listen on the original socket.

You are correct, that is how it is supposed to work - except, you don't open a
new socket. A new socket is returned to you via the accept method. I think
the op is confused about this - and is actually trying to communicate with the
client using the server socket. But, I'm not quite sure - hence the request
for clarification and code demonstrating the issue.

Some people fine the TcpListener/TcpClient classes useful - but to be honest,
I prefer to use the Socket class directly. That's a personal preference,
probably developed out of problems/bugs I experienced during the early beta's
:)
 
G

Guest

right, thats my problem. a Socket is given to me by the function
EndAccept(..), this socket is already bound to a port, so i cannot change it
to non-exclusive. is there any way to change that?
 
C

Chris Mullins [MVP]

iwdu15 said:
Hi, im making an AIM like server using TCP sockets, however i found a big
issue. when a socket is connected to (socket.EndAccept(ar)), its already
bound to a Port. since its bound, i cannot change its access to non
exclusive, but i need this port to still be open because i may need
another
socket to connect on this port as well. how can i achieve this?

Let's say you have a socket listening, on (for example) port 5222. You put
the socket into BeginAccept mode, and then out of the blue you get a
callback. In your callback method you call EndAccept, and get a reference to
a new socket (this is KEY!).

In the NEW socket, you call BeginReceive to start the whole receive process.
You now have a connected client.

On the original socket you call "BeginAccept()" again - this allows
additional users to connect. This algorithm is then repeated forever, and
you have a server that scales very well.
 
G

Guest

yea, i realize this much. however, what i was confused about was wouldnt the
server throw an error the next time a connection attempt was made? like if
you try to bind more than one socket to a port without saying it does not
have exclusive access, an error is thrown. so when you receive a connection
attempt, the socket handed to you by EndReceive(...) is automatically
non-exclusive?
 
T

Tom Shelton

yea, i realize this much. however, what i was confused about was wouldnt the
server throw an error the next time a connection attempt was made? like if
you try to bind more than one socket to a port without saying it does not
have exclusive access, an error is thrown. so when you receive a connection
attempt, the socket handed to you by EndReceive(...) is automatically
non-exclusive?

The socket handed to you by the EndReceive is a completly new and different
socket then the one that accepted the request. It is on a completly different
port, so you can have many client connect to the server sockets known port. The
conversation between the server and the client happens on the port allocated
to the new socket....
 
B

Bart Mermuys

Hi,

Tom Shelton said:
The socket handed to you by the EndReceive is a completly new and
different
socket then the one that accepted the request.

That's right.
It is on a completly different
port, so you can have many client connect to the server sockets known
port. The
conversation between the server and the client happens on the port
allocated
to the new socket....

That's wrong. The new socket uses the same server port. A TCP server can
have many clients connected to the same port. When (connected) clients sent
packets to the server, the TCP stack can distinguish the received packets
based on the source IP and source port which is part of the IP/TCP header
that is sent along.

HTH,
Greetings
 
T

Tom Shelton

Bart said:
Hi,



That's right.


That's wrong. The new socket uses the same server port. A TCP server can
have many clients connected to the same port. When (connected) clients sent
packets to the server, the TCP stack can distinguish the received packets
based on the source IP and source port which is part of the IP/TCP header
that is sent along.

Your are correct of course. My bad. It was too late at night for me
to be thinking clearly. The local end point of the new socket is the
same as the servers, it is differentiated by the remote endpoint - the
ip:port from which the connection originated.
 

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