1

so what I'm trying to do is understand how sockets work and how to make a simple server for communication between the server and the client application

I tried to accomplish this using AS3 with the project named Red Tamarin (Runs AS3 code on console but it doesn't really matter at the moment)

I managed to create a basic server, It's functionality is basically this:

  • creates a socket and activates these on it: listen() , accept(). when it accepts, it also adds the new socket received from the accept() to an array of connections.
  • a for loop that runs according to the length of the connections array and runs receive() on each socket (I didn't call listen() / accept() on those connections, should I?)

My actionscript 3 application connects to the server successfully, And then for a testing purpose I decided to write() to the server from the client non-stop, just to make sure that the server really is getting the data so I can actually go further into adding more functionality..

But the server doesn't seem to get any further information at all.. It seems to respond only to connection / disconnection, but not really listening between them for any other info..

I know that showing my code would be better, but for now I'd like to know if I'm approaching this correctly, I read about it on the internet and it seems like I am but I came here just to make sure..

Thanks in advance!

EDIT: I was requested for some of my code, here it is, well the most important parts at least

private var _connections:Array;
private var _socket:Socket;
private var _server:Client;
private var _running:Boolean;
private var _port:uint;
private var _address:String;

public function Server(address:String, port:uint)
{
    _connections = [];
    _address = address;
    _port = port;
    _running = false;
    _socket = new Socket();

}
public function start():void {
    _socket.bind(_port, _address);
    _socket.listen(128);
    _running = true;
    _server = new Client(_socket);
    _addClient(_server);
    _start();
}
private function _start():void {
    while (_running) {
        _loop();
    }
}
private function _addClient(client:Client):void {
    _connections.push(client);
}
public function _loop():void 
{
    var i:uint;
    var selected:Client;
    var newconn:Socket;
    for (i = 0; i < _connections.length; i++ ) {
        selected = _connections[i];
        if (selected.hasData()) {
            if (selected == _server) {

                //accept a new client connection
                newconn = _socket.accept();
                if (!newconn.valid){
                //Invalid.
                } 
                else {
                    //Add a new client
                    _addClient(new Client(newconn));
                }
            } else {
                //Read data from clients
                selected._socket.receive();
            }

        }

    }

}
4

2 に答える 2

1

listen()/accept() の質問に答えるために、サーバーの初期設定時に listen() を 1 回呼び出すだけで済みます。その後、作成する新しいクライアント接続ごとに accept() を実行する必要があります (accept() は新しいクライアントを処理するソケット ファイル記述子を返すため)。

クライアントが accept() された後、クライアントのソケット ファイル記述子を使用してデータを receive() または send() します。

于 2013-03-08T19:31:46.503 に答える