7

ドキュメントを読みましたが、bind()connect()メソッドの違いが明確ではありません。

4

3 に答える 3

13

bind()ソケットが特定のインターフェイス/ポートで着信要求をリッスンするようにします。つまり、サーバーが着信要求に応答するために使用します。ポートをバインドできるソケットは1つだけです。

connect()ソケットが別のソケットによってサービスされるアドレス/ポートに接続するようにします。つまり、クライアントがサーバーに接続するために使用します。複数のクライアントがポートに接続できます。注:connect()は、UDP(データグラム)ソケットで使用する必要はなく、TCP/IPのみで使用できます。UDPはブロードキャストプロトコルであり、connect()はソケットがもう一方の端をリッスンしている必要さえありません。

このようなもの(ドキュメントから採用され、テストされていない)は、「こんにちは、カブ!」というメッセージを送受信する必要があります。ポート12345でそれ自体に:

package
{
    import flash.events.DatagramSocketEvent;
    import flash.net.DatagramSocket;

    public class TestClass
    {
        private var serverSocket:DatagramSocket = new DatagramSocket();
        private var clientSocket:DatagramSocket = new DatagramSocket();

        public function TestClass():void
        {
            this.serverSocket.bind(12345, "127.0.0.1");
            this.serverSocket.addEventListener(DatagramSocketDataEvent.DATA, dataReceived);
            this.serverSocket.receive();

            send("Hello, turnip!");
        }

        public function sendData(message:String):void
        {
            var data:ByteArray = new ByteArray();
            data.writeUTFBytes(message);

            try
            {
                clientSocket.send(data, 0, 0, "127.0.0.1", 12345);
                trace("sending:  " + message);
            }
            catch (error:Error)
            {
                trace(error.message);
            }
        }

        private function dataReceived(e:DatagramSocketDataEvent):void
        {
            var data:String = e.data.readUTFBytes(e.data.bytesAvailable);
            trace("received: " + data);
        }
    }
}
于 2012-10-18T18:07:05.343 に答える
1
  1. Bind is used to allocate a particular port by system to a socket and no other process can use this particular port until the first process releases it.It's typically used in server side.

  2. Listening and binding are not same, listen puts the socket into listening state, in other words, the server socket is saying that I am listening to incoming client connections now.

  3. Connect is used by client to connect to listening server socket.

  4. Finally accept is used by server socket when a client wants to connect to it while it was in the listening state.

于 2013-09-23T00:05:57.603 に答える
0

これに関する簡単な説明は次のとおりです。サーバーとクライアントのソケットserverSockclientSock

あなたが言うときserverSock.bind((localhost,portnumber))、それは一意のポート「」でアドレスserverSock「」にバインドされていることを意味しますlocalhostportnumber

clientSock.connect((localhost, portnumber))一方、クライアント側で言う場合は、ソケットを使用localhostしてポート「 」でホスト名「 」(サーバーの IP アドレスの場合もある) でサーバーに接続するようにクライアントに指示していることを意味します。portnumberclientSock

于 2014-12-08T16:05:19.240 に答える