aspose file tools*
The moose likes Android and the fly likes Reading from a socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Reading from a socket" Watch "Reading from a socket" New topic
Author

Reading from a socket

no thanks
Greenhorn

Joined: Feb 18, 2012
Posts: 1
So im going crazy here, im doing a tcp chat and thus I need a socket to read and write from. Ive got all the code I need (atleast I think I do) to make it work. Ive gotten the write part done and ive written two classes for reading, one doesnt work the other I dont know how to call :p.

So my class is as follows:

Ive left out all that doesnt really have anything to do with my question to do such as connect and send message as those work as they should.

Sorry for the mass of code but the problem is this, right now im trying to execute the reading from the socket in a thread but it doesnt work, if you notice the Log.d at the start I only get to the first one so im thinking the thread is actually started. Im trying with run() now as the start() crashes the app. This leads me to believe a: the class is faulty, b: theres some difference between run and start but as far as ive read they should do the same?

The async class I havent tested because to be hones I have no idea on how to start it? Yes I am a massive noob at android (well java in general as im more C# and visual studio oriented) and ive never dealt with async task before besides some async stuff in ajax which doesnt really apply here. The write method is easy enough, I start it by executing it with execute(the string i wanna send) and thats that but how do you make an async task just run continously and listen to the socket?

Well no method works so the questions, what is wrong with my thread and how would i "start" the asyncRead? Which of those 2 classes is preferred for listening to a socket? Any help is much appreciated as im going crazy here, been trying different ways and changing existing classes so much I barely know what im doing at this point.


*Edit*
Well added .start() at the end of the thread class so it seems to enter the while loop now which in turn also was wrong so I changed it to while(socket.isConnected()) which im guessing entered the while loop which in turn made the app crash with a null pointer exception?

*Edit again*
Sorry all, total retard alert on me, kinda helps to actually make the bufferedreader thats supposed to read :p, guess im to tired to think right now but I would still like to know which of the two is to prefer, async task or thread? And how do you run the asynctask?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading from a socket