ActionScript Socket Connection Help with .Net

I am using ActionScript to connect to a C # socket server. In the client (ActionScript), I use the following to send data:

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

On the server (C # 4.0) I use this:

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

I can connect the spotlight to the server. But the server does not receive a message (hello) from the client. The server simply ignores the message because it is not sent. But when I read .ReadString () again, I get a message (so I have to read twice to get every input).

I think I know the problem - this is how Flash writes the line: http://livedocs.adobe.com/flex/3/langref/flash/net/Socket.html#writeUTF ()

And here is how C # reads: http://msdn.microsoft.com/en-us/library/system.io.binaryreader.read7bitencodedint.aspx

, # ( ): http://msdn.microsoft.com/en-us/library/system.io.binarywriter.write7bitencodedint.aspx

- , ?
, .

+3
1

, , , :

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]);
              }
           }
        }

, , - , , . #

0

Source: https://habr.com/ru/post/1754868/


All Articles