Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

AsyncTask and TCP Comms

OP WesleyPipes82

15th February 2014, 02:32 AM   |  #1  
OP Junior Member
Thanks Meter: 0
 
1 posts
Join Date:Joined: Feb 2014
Hey guys,

I have spent at least 24 hours trying to figure this out by myself and still havn't been able to...so I broke down and came here.


This code works...it does not (9/10s sure here...) run asyn and receives the same number of bytes as the picture should have
Code:
class ClientThread implements Runnable {
	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	public void run() {

		while(true){
        try {        
            int bytesRead;
            int totalBytes = 0;
            byte[] pic = new byte[1024];
            byte[] inputPic_bytes = new byte[5000000];
            
            DataInputStream dis;
            DataOutputStream dos;
            try { 
                dis = new DataInputStream(socket.getInputStream());
                dos = new DataOutputStream(socket.getOutputStream());

                dos.writeBytes("snapshot" + '\n');
                
                int pic_size = Integer.parseInt(dis.readLine());
                Log.e("TCP", "Pic size: " + pic_size);
                
                while (totalBytes < pic_size){
                		bytesRead = dis.read(pic);
                		System.arraycopy(pic, 0, inputPic_bytes, totalBytes, bytesRead);
                		totalBytes += bytesRead;
                		pic[0] = (byte) (pic[0]&0xFF);
                		Log.e("TCP", "Read bytes: " + bytesRead + " Total bytes: " + totalBytes + " Bytes: " + pic[0] + " " + pic[1] + " " + pic[2]);
                }
this code runs async and receives ~5k more bytes than the picture size which does not even seem possible to me (read_mode = 2 gets the pic)

Code:
protected Boolean doInBackground(Void... params) { //This runs on a different thread
            boolean result = false;
            try {
                //create a new socket instance
                SocketAddress sockaddr = new InetSocketAddress("192.168.0.5", 50007);
                nsocket = new Socket();
                nsocket.connect(sockaddr, 5000);//connect and set a 10 second connection timeout
                if (nsocket.isConnected()) {//when connected
                    nis = new DataInputStream(nsocket.getInputStream());
                    nos = nsocket.getOutputStream();//and output stream from the socket
                    Log.e("Buffer Size", "Size: " + nsocket.getReceiveBufferSize());
                    networktask.SendDataToNetwork("sensors");
                    
                    while(true){//while connected
                    	if(read_mode == 0){
                    		String getPicLength = nis.readLine();//read the lines coming from the socket
                    		byte[] array_getPicLength = getPicLength.getBytes();
                    		publishProgress(array_getPicLength);//update the publishProgress
                    		Log.e("While_Count_0", "Count");
                    		read_mode = 0;     		
                    	}
                    	else if(read_mode == 1){
                    		String getPicLength = nis.readLine();//read the lines coming from the socket
                    		byte[] array_getPicLength = getPicLength.getBytes();
                    		publishProgress(array_getPicLength);//update the publishProgress
                    		Log.e("While_Count_1", "Count");
                    		read_mode = 2;
                    	}
                    	else if(read_mode == 2){
                    		nis.read(pic);
                    		publishProgress(pic);//update the publishProgress
                    	}
                        
                    }
                }
Not sure if I have included enough info here...but this is seriously troubling me as I used to have it working.

What is so different about async that it is causing to receive so many extra bytes of "picture" data?

Thanks all...this is really bothering me.
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes