有关.Net的ActionScript套接字通信的帮助

时间:2010-07-15 18:57:22

标签: .net flash sockets actionscript c#-4.0

我正在使用ActionScript连接到C#套接字服务器。 在客户端(ActionScript)中,我使用以下命令发送数据:

var socket:Socket = new Socket("localhost", 8080);
socket.writeUTF("hello");
socket.flush();

在服务器(C#4.0)中,我使用它:

server = new TcpListener(IPAddress.Any, 8080);
server.Start();
TcpClient client = server.AcceptTcpClient();
BinaryReader reader = new BinaryReader(client.GetStream(), Encoding.UTF8);
Console.WriteLine(reader.ReadString());

我可以将闪存连接到服务器。但是服务器没有从客户端收到消息(“hello”)。服务器只是忽略了它没有发送的消息。但是,当我再次执行reader.ReadString()时,我会收到消息(因此我必须阅读两次以获取每个输入)。

我想我知道这个问题 - 这就是Flash写字符串的方式: http://livedocs.adobe.com/flex/3/langref/flash/net/Socket.html#writeUTF()

这就是C#读取它的方式: http://msdn.microsoft.com/en-us/library/system.io.binaryreader.read7bitencodedint.aspx

有关C#如何读取它的有关信息(请参阅备注):http://msdn.microsoft.com/en-us/library/system.io.binarywriter.write7bitencodedint.aspx

有人能告诉我如何让客户端和服务器使用二进制数据进行通信?
谢谢,摩西。

1 个答案:

答案 0 :(得分:0)

嘿Zippo从flash写一个字节将是:

socket.writeByte( byte )

这里还有一段我写的用来处理客户端数据的服务器代码。

        NetworkStream clientStream = tcpListener.AcceptTcpClient().GetStream();

        byte[] message = new byte[4096];
        int bytesRead;

        while (true)
        {
           bytesRead = 0;

           try
           {
              //blocks until a client sends a message
              bytesRead = clientStream.Read(message, 0, 4096);
           }
           catch
           {
              //a socket error has occured
              break;
           }

           if (bytesRead == 0)
           {
              //the client has disconnected from the server
              break;
           }

           //message has successfully been received
           ASCIIEncoding encoder = new ASCIIEncoding();
           String received = encoder.GetString(message, 0, bytesRead);

           for (int i = 0; i < bytesRead; i++)
           {
              if (message[i] == MESSAGE_BEGIN)
              {
                 player.currentMessage = new Message();
              }
              else if (message[i] == MESSAGE_END)
              {
                 _GotMessage(player, player.currentMessage);
              }
              else if (message[i] == TYPE_BEGIN)
              {
                 player.currentString = "";
              }
              else if (message[i] == TYPE_END)
              {
                 player.currentMessage.Type = player.currentString;
              }
              else if (message[i] == STRING_PARAM_BEGIN)
              {
                 player.currentString = "";
              }
              else if (message[i] == STRING_PARAM_END)
              {
                 int val;
                 bool isInt = Int32.TryParse(player.currentString, out val);
                 if (isInt)
                 {
                    player.currentMessage.content.Add(val);
                 }
                 else
                 {
                    player.currentMessage.content.Add(player.currentString);
                 }
              }
              else
              {
                 player.currentString += System.Convert.ToChar(message[i]);
              }
           }
        }

我将所有代码都包含在内,但如果您有任何疑问,请随时提出。服务器位于C#