Android-dev.it Community

 
Annunci

:arrow: Benvenuto!, se non sei ancora un utente registrato: puoi registrati qui, oppure effettua il login
:arrow: Apri un forum sul tuo nuovo smartphone Android e collabora con noi! (dettagli)

www.agendadigitale.org
Image


Sponsor
It is currently Thu May 24, 2012 3:57 pm

All times are UTC + 1 hour


 Topics   Replies   Views   Last post 
No new posts Applicazione per gestire SMS

by filippo729 on Thu May 24, 2012 2:56 pm in Sviluppo e programmazione in Android

2

29

Thu May 24, 2012 2:56 pm

filippo729

No new posts На интернет-витрине

by popowahgjfghjoiuyt on Sat May 19, 2012 2:25 pm in Amministrazione

0

19

Sat May 19, 2012 2:25 pm

popowahgjfghjoiuyt

No new posts Apri un forum sul tuo nuovo smartphone Android!

[ Go to page: 1, 2, 3 ]

by ReattileGar on Fri May 18, 2012 5:27 pm in Amministrazione

27

12569

Fri May 18, 2012 5:27 pm

ReattileGar

No new posts Новый: базы для хрумера - профессиональный софт для СЕО.

by Renatafe on Fri May 18, 2012 3:14 pm in Amministrazione

0

27

Fri May 18, 2012 3:14 pm

Renatafe

No new posts Pace is undoubtedly the vital affordable Jerseys Wholesale d

by barnettmmolive2012 on Wed May 16, 2012 7:36 am in Amministrazione

1

49

Wed May 16, 2012 7:36 am

barnettmmolive2012




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Socket non bloccante
PostPosted: Wed May 04, 2011 8:18 pm 
Offline
User avatar

Joined: Sun May 01, 2011 9:28 am
Posts: 12
Operatore: 3
Punti Android: ?
Ciao! Una parte della mia app contiene un socket tcp/ip. questo socket deve connettersi in remoto ad un serve e scambiare dei dati. L'invio dei dati funziona, ma la ricezione mi da problemi. Il socket è contenuto in un thread ed ho bisogno che la ricezione non sia di tipo "bloccante". Ho provato con questo cercando un po' in rete:

Code:
                  ByteBuffer incomingLengthInBytes = ByteBuffer.allocate(4); // size of an 'int'
                 
                  socketChannel.read(incomingLengthInBytes);
                 
                  incomingLengthInBytes.rewind();
                 
                  int incomingLength = incomingLengthInBytes.getInt();
                 
                  if (incomingLength > 0)
                  {
                     inviaToast("Got Incoming Length as: " + incomingLength + " bytes");
                 
                     // now allocate the correct size for the message...
                     ByteBuffer incomingData = ByteBuffer.allocate(incomingLength);
                 
                     socketChannel.read(incomingData);
                 
                     incomingData.rewind();
                 
                     incomingData.get(bustaRicevuta.pacchetto);
                     bustaRicevuta.lenPacchetto = incomingLength;
                  }


Il socket non è bloccante e tutto sembra funzionare, ma quando arrivano effettivamente dei dati, la variabile incomingLenght contiene un numero da fantascienza invece che la vera quantità di dati nel socket...

Qualche suggerimento? Alternative per un metodo non bloccante che funzioni? Bye bye :)


Top
 Profile  
 
 Post subject: Re: Socket non bloccante
PostPosted: Thu May 05, 2011 5:03 pm 
Offline
User avatar

Joined: Thu Dec 27, 2007 7:09 pm
Posts: 659
Cellulare Android: HTC Dream
Operatore: TIM
Punti Android: 758
Ciao,
se non ricordo male il metodo rfead di SocketChannel restituisce il numero di byte letti, quindi:
Code:
int incomingLength = socketChannel.read(incomingLengthInBytes);

_________________
***URLs are hidden from guests, please register and login to view the hyperlink***


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
phpBB SEO

All trademarks and logos used in this site are of properties of their respective owners.