The problem is that if you send several messages from the server at once, then everything will turn into a mess on the client and the ProgressEvent.SOCKET_DATA socket listener will only work once. Here is the Java code:

public static void main(String[] args) throws IOException { String msg = "test msg"; ss = new ServerSocket(12345); while(true){ s = ss.accept(); s.setTcpNoDelay(true); for(int i = 0; i< 10;i++){ send(msg,s); } } } public static synchronized void send(String msg,Socket sender) throws IOException{ OutputStream os = sender.getOutputStream(); os.write(msg.getBytes(), 0, msg.length()); os.flush(); } 

AS3:

 var socket:Socket = new Socket(); socket.connect("127.0.0.1", 12345); socket.addEventListener(Event.CONNECT, function():void{ trace("conneted"); }); socket.addEventListener(ProgressEvent.SOCKET_DATA, function(e:Event):void{ var bytes:ByteArray = new ByteArray(); var size:int = socket.bytesAvailable; socket.readBytes(bytes, 0, size) var a:String = bytes.readUTFBytes(size); trace("что то пришло: " + a); }); 

Here is what happened in the client logs:

"something came: test msgtest msgtest msgtest msgtest msgtest msgtest msgtest msgtest msgtest msg"

    1 answer 1

    There is no cereal here. A socket is a two-way data channel. All bytes sent to the socket via send in the same order will be received by the receiving side. Sending and receiving data have no boundaries.

    Example: The sending side sends two data sets, each by 8 bytes: "aaaabbbb", "ccccdddd".

    1. The receiving party may receive two data sets "aaaabbbb" and "ccccdddd". This can be achieved if the sending side makes a short pause after sending the first data set.

    2. A more likely option is to come one data set "aaaabbbbccccdddd" - if there was no pauses between the send .

    3. Any other combination is also possible. In the extreme case, the receiving data handler on the receiving side can work at least 16 times, each time receiving exactly 1 byte.

    In general, sending and receiving data should be understood as a continuous stream. From this it follows that it is necessary to independently develop a data exchange protocol that would solve the problem of splitting data into messages. This is easily done by submitting the message as a pair of Header and Body. The simplest header must have at least one field - the length of the message. Additionally, you can enter another field - the message type. Your messages can be presented as follows:

     { length: длина сообщения, с заголовком или без, по вкусу type: тип сообщения body: "test msg" } 

    This is an example of a text protocol. In the same way, you can implement a binary protocol. In the simplest case, if the protocol is based on text commands, you can limit yourself to a simple line terminator. It can be a null character, or any other unused character. For example, take the character "|". Then the sent data should be supplied with this separator. The receiving party in the end (remember that the data comes in chunks) will receive "test msg|test msg|test msg|test msg|test m" I deliberately cut off the stream, because in reality it will be so. The receiving party is obliged to parse this thread. That is, read the bytes until we find a separator, this signals that the next command has been completely read. The end of the message demonstrates the possibility of not meeting the limiter. In this case, the data must be saved and wait for the next portion of data, for example - "sgtest msg|test msg|" We do the same with protocol based on the header.

    1. We are convinced that the accumulated data correspond to the length of the header (here the binary protocol is more convenient, since the header has a fixed length). If not, wait for the next batch of data.
    2. Make sure that the accumulated data corresponding to the message body. If not, wait for the next batch of data.