Actionscript3.0 socket programming sends data to the socket server

Source: Internet
Author: User

Solution:
For a socket object, use the write method (writebyte (), writeutfbytes (), and other methods .) first write data to the cache area, and then use the flush () method to send data. for xmlsocket objects, use the send () method.

Discussion:
The methods for sending data from the socket and xmlsocket classes to the socket server are different. Let's first look at the methods of the socket class.

When you use a socket object to send data to the server, you first need to write the data to a buffer. The socket class sets a series of methods to write data. Each method is used to write
Data of the same data type (or different data). These methods are: writeboolean (), writebyte (), writebytes (
), Writedouble (), writefloat (), writeint (), writemultibyte (),
Writeobject (), writeshort (), write-unsignedint (), writeutf (),
And writeutfbytes ().
Most of these methods only accept one parameter. The parameter type matches the method name. For example, the writeboolean () method accepts a Boolean value as the parameter, while
Writebyte (), writedouble (), writefloat (), writeint (), writeshort (),
Writeunsignedint ()
Method accepts a numeric parameter. writeobject () method accepts an object type as a parameter, but the object must be serialized into AMF format. writebytes (
) Method allows you to pass a bytearray parameter with two parameters: Offset and length. For example, the following code calls a writebytes (
) Method. This method transfers all BYT values from the bytearray object (offset is 0, length and bytearray length ):
Socket. writebytes (bytearray, 0, bytearray. Length );

Writeutf () and writeutfbytes ()
Method allows you to send string parameters. Each method only accepts one string as the parameter. writeutfbytes (
) The method simply uses the string as the bytes sending. writeutf () method to write the number of bytes before writing real data.

Writemultibyte (
) The method also allows parameters of the string type, but uses non-default character sets. This method requires two parameters: string and Character Set name. In the flash and flex help documents, there is a proprietary
A list of character sets. The tags and descriptors in the list correspond one to one. Use the tag value as writemultibyte (
) As a character set. For example, the following code sends a Unicode-encoded string:

Socket. writemultibyte ("example", "Unicode ");

The method for passing a value to a socket object relies entirely on the types of all your data and the types of data accepted by the Service. Using a socket object, you can use
Write a telnet and pop command in the Action column.
The mail client. Both protocols support ASCII characters. For example, after connecting to a POP server, you can specify a user by using the USER command. The following code
The socket object sends a command:

// POP servers expect CT a newline (/N) to execute the preceding command.
Socket. writeutfbytes ("User exampleusername/N ");

Writing data to a socket object does not actually send data to the socket server. each time a write method is called, a data is added to the socket object. for example, the following code adds four bytes of data to a socket object, but no one sends data.

Socket. writebyte (1 );
Socket. writebyte (5 );
Socket. writebyte (4 );
Socket. writebyte (8 );

To send the accumulated data to the socket server, you need to call the flush () method. Flush () method to send all written data and clear the buffer zone:

Socket. Flush ();

The xmlsocket class is a very simple API for sending data. The. Send () method written to send data is completed by the send () method and can accept parameters of any data type.
It can convert all parameters into a string type and send it to the server. Normally, the parameter is an XML object or a string containing data structures similar to XML data:

Xmlsocket. Send (XML );

However, accurate formats depend entirely on the formats acceptable to the server. if the server accepts data in XML format, you must send data in XML format. if the server only accepts URL-encoded data, you must also send URL-encoded data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.