简体   繁体   中英

How to establish a stream connection with a C# program

I'm attempting to create a proxy for an AIR application. What I'm trying to do at the moment is establish a connection between a C# program (I'm pretty sure I've cracked that side of things) and an AS3 AIR program.

I'm not really sure what I'm doing in all honesty with AS3 in regards to this, I've found the networking classes and I'm attempting to create a new NetConnection and feed that into a NetStream.

What I need is to be able to do is start up my AS3 application from my C# proxy (again I've got that sorted I think). Then I need to establish a connection between the two and exchange just something like "Hello" for now. Past that I'll be okay with getting things sorted, but it's just establishing this stream connection to allow me to send commands and responses in the end.

Sadly AS3 is looking pretty dead now so finding help with it isn't easy!

Any advice or direction would be much appreciated.

Does the AS3 AIR application have some kind of configuration file, where you can setup up an IP address, port etc?

If it does, you could try and establish a TCP connection to the application.

Here is a quick and short sample code to get you going for trying a TCP connection to that app, and receiving data:

        IPAddress address = IPAddress.Parse("10.0.0.2");
        IPEndPoint endPoint = new IPEndPoint(address, 3999);

        TcpClient client = new TcpClient();
        client.Connect(endPoint);

        if (client.Connected)
        {
            NetworkStream stream = client.GetStream();

            //use this stream for communication
        }

This should get you going. I have not tested this for error catching etc, and you should rather look at Asynchronous methods, but this will get you started.

Have a look at this !

Sorted it and all working!

The method I used (but not the only way) was using a socket server set up.

I created open a socket using C# and get it ready to receive on the UTF stream.

Then I created a simple little socket in AS3 in a dummy project, this is done with these few chunks of code:

        socket = new Socket();
        socket.addEventListener(Event.CONNECT, onConnection);
        socket.addEventListener(Event.CLOSE, onClose);
        socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
        socket.addEventListener(ProgressEvent.SOCKET_DATA, dataIn);

        if (host && port) {
            socket.connect(host, port);
        }

Then I had my few little event handler methods which were:

    private function onConnection(e:Event):void {
        var messageOut:String;
        messageOut = "Ready";
        trace("sending: " + messageOut);
        socket.writeUTFBytes(messageOut.toString());
        socket.flush();
    }

    private function onClose(e:Event):void {
        socket.close();
        trace("Socket closed");
    }

    private function dataIn(e:ProgressEvent):void {
        var messageIn;
    }

    private function onError(e:IOErrorEvent):void {
        trace("There was an error: " + e.toString());
        socket.close();
    }

This code doesn't do much but it's the perfect framework for this. Hopefully it's useful for someone, if not then me when I need this again!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM