0

Am working on asynchronous socket programming and am using this code

https://learn.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-server-socket-example

Am dynamically creating these listeners(New instance of Socket Listener on each Port by passing different Port numbers and i need to pass an associated information for each port number when the socket is listening so i can use that information when i receive data.

basically my Start listening method will be like

public static void StartListening(string addInfo) 

i understand how to pass additional parameter for Asynch function after referring this

https://stackoverflow.com/a/9192678/1481690

but in this socket code we already passing socket as a parameter (Listener)

listener.BeginAccept(   
                    new AsyncCallback(AcceptCallback),  
                    listener ); 

How do i add one more additional Parameter i need to pass on to BeginAccept?

what other ways i can get this to work as we already have a socket(Listener) object sent as a parameter.

or how else can i dynamically ramp up the socket servers with different ports and some how i can map additional Info to each Socket

Peru
  • 2,871
  • 5
  • 37
  • 66
  • The state parameter is whatever you'd like it to be. For example, an object which contains a reference to your listener, and whatever other parameter(s) you can think of. – glenebob Oct 23 '18 at 17:41
  • @glenebob Exactly i understand that !!! but if you look at the Asynch program in details. it is already using the state parameter in listener.BeginAccept and i cant change the class as it is "Socket" – Peru Oct 23 '18 at 17:43
  • You can't "add" any additional parameters to the call. Perhaps you could use an associative collection to map a socket object to additional data. – glenebob Oct 23 '18 at 18:03
  • @glenebob can you share some code with the sample i shared. am not quite catching up – Peru Oct 23 '18 at 18:21

1 Answers1

2

There are a couple of ways to handle this, given that you cannot change the type of the state object in the call to BeginAccept().

1: Inject additional data into the callback code path:

AdditionalInformation additionalInformation = new AdditionalInformation(whatever);
listener.BeginAccept((asyncResult) => AcceptCallback(asyncResult, additionalInformation), listener);

2: Use an associative collection to store additional information. In this case, AcceptCallback would refer back to the collection to retrieve the additional information:

Dictionary<string, AdditionalInformation> additionalInformationMap = new Dictionary<string, AdditionalInformation>();

AdditionalInformation additionalInformation = new AdditionalInformation(listener, whatever, whateverElse);
additionalInformationMap[MakeKey(listener)] = additionalInformation;
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
glenebob
  • 1,943
  • 11
  • 11
  • that helps to get some thing out of it. i can use Tuple to make it simpler .. thoughts? – Peru Oct 23 '18 at 19:01
  • I don't see why Tuple wouldn't work, but I would avoid it. A well named class makes for more readable code than a Tuple does. – glenebob Oct 23 '18 at 19:16
  • makes sense . in your option 1 what does the AcceptCallback method looks like . will it be parameter followed by state as last parameter? – Peru Oct 23 '18 at 19:24
  • AcceptCallback is the name of the callback method you used in the question, and is merely an example. It looks however you need it to look. – glenebob Oct 23 '18 at 19:32
  • Can someone show working code based on https://learn.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-server-socket-example to pass additional parameter, let's say it is string myname = "I pass my name as a additional parameter"; –  May 06 '21 at 07:20