Reputation: 9
Ok here goes.
On my network I have a couple of pc's all running a bespoke server which will return the username of the person logged in when queried.
I have the following piece of code.
private function LoopAndList():void
{
var loopSocket:Socket;
var fourthOctet:Number = 6;
var stubIP:String = "192.168.0.";
var loophost:String;
loopSocket = new Socket();
loopSocket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
loopSocket.addEventListener(Event.CONNECT, connectHandler);
loopSocket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
loopSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
textarea1.text = "";
while (fourthOctet != 0)
{
loophost = stubIP + fourthOctet;
textarea1.text = textarea1.text + loophost + "\n";
textarea1.text = textarea1.text + "Connecting to: " + loophost + "\n";
try
{
loopSocket.connect("192.168.0.4", 19001);
fourthOctet--;
}
catch(ioError:IOError)
{
textarea1.text = textarea1.text + "Sec error \n ";
fourthOctet--;
}
catch(secError:SecurityError)
{
textarea1.text = textarea1.text + "io error \n";
fourthOctet--;
}
Now when this runs it doesn't work as I first thought. The iterations go though. the fourthOctet value is decremented. But somethinjg strange happens. You will notice in the above code that I have a hardcoded IP address in the loop. I have put this there to ensure that there is always a valid connection. So am aware it will always be connecting to 192.168.0.4.
But the connection only happens on the last connect call.
The ouput is:
192.168.0.6
Connecting to 192.168.0.6
192.168.0.5
Connecting to 192.168.0.5
...
192.168.0.1
Connecting to 192.168.0.1
Sending Data
IN REad function
data received
username/192.168.0.1
Where I would expect all to connect and run happily. Does anyone have any ideas on where this is failing?
Upvotes: 0
Views: 226
Reputation: 9
OK Here is the solution I came up with involving timers, a counter and an array of values. I prepopulate the array with a loop on start.
var timer:Timer = new Timer(1000, 4);
textarea1.text = "";
loopSocket = new Socket();
timer.start();
timer.addEventListener(TimerEvent.TIMER, bogoffflex);
function bogoffflex():void
{
textarea1.text = textarea1.text + "Timer fired " + count + "\n";
textarea1.text = textarea1.text + loopSockArr[count] + "\n";
loopSocket.addEventListener(Event.CONNECT, onConnect);
loopSocket.addEventListener(Event.CLOSE, onClose);
loopSocket.addEventListener(ProgressEvent.SOCKET_DATA, recvdata);
try
{
//loopSocket.timeout = 1000;
loopSocket.connect(loopSockArr[count] , 19001);
count ++;
}
The result, is a little slower than I wanted but is acceptable.
Upvotes: 1
Reputation: 38885
It seems like you don't understand that Socket.connect() is asynchronous. Therefore doing your loop and modifying text isn't doing what you think because you haven't connected in that loop. After connect() is called it returns immediately not waiting to connect to a host. You should modify the text area in the connctHandler callback because only at that point are you truly connected. Same goes for your error handling as well. You're probably not seeing responses because the servers might not be turned on, or busy, a timeout etc. The one that's coming back first is probably the active server, and the others don't exist, not reachable, network error, whatever. Basically their connects will timeout over 30 seconds waiting on a response.
Your question is missing intent. What are you trying to accomplish because the code is doing what you asked it to do. Why you are doing this isn't clear.
Basically it's all asynchronous and your output isn't going to be a clean as this:
Connected to 1, here is what I sent to 1, here is 1 told me
Connect to 2, here is what I sent to host 2, here is what host 2 told me
Connect 3, etc.
It's going to all happen in parallel and requests and responses are going to return as each host and network response allows it. So it will all get mushed together. You can modify your code to serialize it, but you'll have to chain the connect() calls onto the end of the responses from the last connection inside your callbacks. Trickier, but doable.
If you are trying to establish a connection with at least one server then you'll need to do something like what I'm describing. Try 1 if there is a problem, Try 2, etc. But you'll have to initiate the next connect() call from within the callbacks. Not synchronously as you've written.
Upvotes: 1