AS 3.0 client class code

package main { import flash.net.*; import flash.events.*; public class SocketClient { private var hostName:String = "localhost"; private var port:uint = 4100; private var socket:XMLSocket; public function SocketClient() { socket = new XMLSocket(); configureListeners(socket); if (hostName && port) { socket.connect(hostName, port); } } public function send(data:Object):void { socket.send('<data><viewer_id>1</viewer_id></data>\n');trace('date send: ' + data); } private function configureListeners(dispatcher:IEventDispatcher):void { dispatcher.addEventListener(Event.CONNECT, connectHandler); dispatcher.addEventListener(DataEvent.DATA, dataHandler); } private function connectHandler(event:Event):void { trace("connectHandler: " + event); } private function dataHandler(event:DataEvent):void { trace("dataHandler: " + event); } } } 

Server code On java.

 public void run() { try { readerIn = new BufferedReader(new InputStreamReader(ClientSocket.getInputStream())); printOut = new PrintStream(ClientSocket.getOutputStream()); String str = null; boolean done = false; while(!done) { str = null; System.out.println("Ожидаем прихода сообщения!"); str = readerIn.readLine(); // пока клиент пришлет строку текста. if(str == null) { break; } System.out.println("Пришло : " + str); printOut.println("Пришло : " + str); // отсылаем клиенту обратно ту самую строку текста. System.out.println("Ожидание следующей строки..."); } ClientSocket.close(); } catch(Exception error) { } System.out.println("Поток закрыт!"); } 

The essence of the problem is as follows. When sending data from a client, starting from the second message, an extra character appears before the message itself. Here is the server log.

alt text

    2 answers 2

    What is the character code? Surely this is \ r or \ n. readerIn.readLine () considers them as line end characters. Add readerIn.skip (1) after readerIn.readLine ().

    • THX. helped - Ilya Chizhanov
    • It turned out so. that if you add readerIn.skip (1) then the client does not trigger the dataHandler event. - Ilya Chizhanov
    • character code 0. - Ilya Chizhanov
    • Strange. Try readerIn.read () - nitrocaster

    It was necessary to do more. printOut.println("Пришло : " + str + "\0");

    • ... What for? - nitrocaster