0

I'm having an issue using sockets in flash builder 4. The code below sends a set of bytes to a receiving c# sockerServer. If I dismiss the error I get in flash builder manually, the bytes are sent fine and all comes across as it should on 127.0.0.1:10. Now if I could just get the same results without an error being displayed in Flex.

So, I have two questions:

1) Why does it return an error when I try to close the socket? See closeConnection() below for context. I tried flushing it just before which didn't help.

2) Why is nothing sent when I use socket.flush()?

 package
 {
import flash.events.IOErrorEvent;
import flash.net.Socket;
import flash.utils.ByteArray;

public class socketClient
{
    private var socket:Socket;
    public function openConnection(address:String, port:int):void
    {
        if (socket != null && socket.connected)
            socket.close();

        socket = new Socket();
        try {
            socket.connect( address, port );            
        }
        catch( e:Error ) { }            
    }
    public function sendProtocol(p:socketProtocol):void {
        //p.serialize() gets me a bunch of bytes in a ByteArray
        var buffer:ByteArray = p.serialize();
        socket.writeBytes(buffer, 0, buffer.length);
        //Nothing happens when I flush
        socket.flush();
    }
    public function closeConnection():void {
        //As soon as I get to socket.close(), I get this
        //"Unhandled IOErrorEvent:. text=Error #2031: Socket Error."
        socket.close();
    }
}

}

I use the class like this:

var socket:socketClient = new socketClient();

//works fine, I see the connection on the server
socket.openConnection("127.0.0.1", 10); 

//no errors, but nothing sent
socket.sendProtocol(protocol); 

//returns the error. (if manually dismissed, data is sent)
socket.closeConnection(); 
BlueVoodoo
  • 23
  • 1
  • 6
  • Similar issue: http://stackoverflow.com/questions/3645988/socket-error-sometimes – Nikolai Fetissov Nov 30 '10 at 14:46
  • Yes, I've seen that one and tried adding the event:IOErrorEvent. Didn't help me understanding the issue much though. I also have no problem opening the socket. Connects fine on the receiving server. – BlueVoodoo Nov 30 '10 at 14:48
  • Try a port number that's above 1024. There might be a security issue with opening ports bellow that. (Although that wouldn't explain why you are seeing an open connection on the server side.) – martineno Nov 30 '10 at 17:23
  • You might also want to look at http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html since you might need a socket policy file similar to a crossdomain file. – martineno Nov 30 '10 at 17:27
  • Tried port 8888 but I get the same issue. Will read through the link you posted to. – BlueVoodoo Nov 30 '10 at 17:44
  • Read throught the link and did some googeling. Accordnig to http://stackoverflow.com/questions/1526982/airflex-local-socket-connection this should not be needed since I am connecting from one app to another. – BlueVoodoo Nov 30 '10 at 19:21

1 Answers1

0

I finally solved it after hammering this one since I posted the question.

I had to add a

socket.addEventListener(flash.events.Event.CLOSE, closeHandler)

and do the socket.close() from there.

BlueVoodoo
  • 23
  • 1
  • 6