A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: [RESOLVED] Socket Server : Replying to client

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    4

    resolved [RESOLVED] Socket Server : Replying to client

    For the past few weeks I've been tinkering around with a basic socket server built from a tutorial which handled the initial policy file request, connection and other basic functions, which since then i have been working into my current project, unfortunately i've now hit an impasse with handling constant transfer of messages between the client and server.

    At the moment my client can write a message through bytearrays and objects and then send this to my server perfectly, no issue, server can read all necessary items from the object sent. The same occurs if my server sends a message itself, eg: after the socket connection event, server sends a string message saying hello, client receives it fine. The main issues seems to occur however if my client has just sent a message, server has received it and then tries to either send the same message to a secondary client, or the the client who just sent the message.

    I can't find out what is happening as it is occurring between my server replying to client and my Clients Data received event, which is not triggering as no data seems to be pushed. This is the current reply function and data received event I have written

    Code:
    ////////////////////////
    		// Replying to client //
    		////////////////////////
    		/**/function reply( Message:String = "", array:Array = null, type:Number = 0 ):void
    		/**/{			
    		/**/
    		/**/var objectMessage1:SerializableObject = new SerializableObject( 100, Message,array,type);
    		/**/	var bytes2:ByteArray = new ByteArray();
    		/**/	
    		/**/	bytes2.writeObject( objectMessage1 );
    		/**/	bytes2.position = 0;
    		/**/try{
    		/**/		//Write the headers
    		/**/		socket.writeUnsignedInt( bytes2.length ); //message length 
    		/**/        //Write message type
    		/**/		socket.writeInt( TYPE_AMF );
    		/**/		//Serialize the object to the socket
    		/**/		socket.writeBytes( bytes2 );
    		/**/		//Make sure it is sent 
    		/**/		socket.flush();
    		/**/		
    		/**/	} catch ( e:Error )
    		/**/	{
    		/**/		log( e.toString() );
    		/**/	}
    		/**/	bytes2.position = 0;
    		/**/	
    		/**/	
    		/**/}
    		////////////////////////////////////////////////////
    Code:
    ////////////////////////////////
    		//Read and display the message//
    		////////////////////////////////
    		/**/private function dataReceived(event:ProgressEvent):void 
    		/**/{ 
    		//////////////////////////
    		//// New receiving data //
    		//////////////////////////
    		/**///Read the data from the socket
    		/**/try
    		/**/{
    		/**/	while( socket.bytesAvailable >= 4 )//while there is at least enough data to read the message size header
    		/**/	{
    		/**/		if( messageLength == 0 ) //is this the start of a new message block?
    		/**/		{
    		/**/			messageLength = socket.readUnsignedInt(); //read the message length header
    		/**/		}
    		/**/			
    		/**/		if( messageLength <= socket.bytesAvailable ) //is there a full message in the socket?
    		/**/		{
    		/**/			var typeFlag:int = socket.readInt(); //read the message type header
    		/**/				
    		/**/			//Read the message based on the type
    		/**/
    		/**/	////////////////////////////////
    		/**/	// When receiving sent object //
    		/**/	////////////////////////////////
    		/**/			if( typeFlag == TYPE_AMF ) //AMF object
    		/**/			{
    		/**/				var dataMessage:SerializableObject = socket.readObject() as SerializableObject;
    		/**/				//send message to server function here
    							trace("socket connection got " + dataMessage.toString())
    		/**/				log(dataMessage.toString(),dataMessage.getarray(),dataMessage.gettype()); 
    		/**/			}
    		/**/			else if ( typeFlag == TYPE_STRING ) //UTF string
    		/**/			{
    		/**/				var utfMessage:String = socket.readUTF();
    		/**/			}
    		/**/			messageLength = 0; //finished reading this message
    		/**/		}
    		/**/		else { //The current message isn't complete -- wait for the socketData event and try again
    		/**/				//trace( "Partial message: " + socket.bytesAvailable + " of " + messageLength )
    		/**/				break;
    		/**/			}
    		/**/		}
    		/**/	}
    		/**/	catch ( e:Error )
    		/**/	{
    		/**/		log( e );
    		/**/	}			
    		/**/}		
    		///////////////////////////////////////////////////////////////////////////////////
    I would be very thankful if anyone has any info concerning either the code, or maybe some reason why writing back to the socket does not occur in this condition as at the moment, apart from maybe saving the message, waiting for a set time, then responding, i have no clue whats happening as no IOerrors or any similar errors are making it easy to find out. Thank you and sorry for the wall of code

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Sorry for the double post but it seems for the time being I found the issue with the data sending. In my sockets connection handler there was a single socket.write command sending a message of "ready" to the client, However as i had changed the method of reading data, this single message was remaining inside the sockets buffer, causing further incoming messages to crash into this causing a heap of information which was not being read accordingly.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center