Android TCP通信太慢了

时间:2015-06-13 00:14:13

标签: java android sockets tcp

我的TCP发送者类是TCPSenderTask作为客户端,我使用TCP服务器服务。

当我尝试发送数据时,数据传输速度太慢。我找不到原因。

package com.example.sampletcpclient;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;

import com.example.model.Order;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;

class TCPSenderTask extends AsyncTask<Void, Void, Boolean> {
    Context mContext;
    TextView tv;

    BufferedReader reader;
    PrintWriter writer;
    Socket socket;

    ArrayList<Order> outgoingOrders;
    ObjectOutputStream oos;


    public TCPSenderTask(Context mContext, ArrayList<Order> outgoingOrders, TextView tv) {
        this.mContext = mContext;
        this.tv = tv;
        this.outgoingOrders = outgoingOrders;

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        try {

            socket = new Socket("192.168.2.3", 4999);
            oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(outgoingOrders);
            oos.close();

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        if(result){
            tv.setText("Successuful");
        } else{
            tv.setText("Unsuccessuful");
        }
    }
}

和我的TCP侦听器服务作为TCP服务器

package com.example.sampletcpserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import com.example.model.Order;


import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

public class TCPListenerService extends Service {

    private final IBinder mBinder = new TCPBinder();

    private int result = Activity.RESULT_CANCELED;

    public static final String NOTIFICATION = "com.example.sampletcpserver";
    public static final String RESULT = "result";
    public static final String MESSAGE = "message";
    private static int FOREGROUND_ID = 1338;

    private String message;

    LocalBroadcastManager broadcaster;

    public static ArrayList<Order> incomingOrders;
    ObjectInputStream ois;

    public int counter = 1;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        broadcaster = LocalBroadcastManager.getInstance(this);

        incomingOrders = new ArrayList<Order>();

        Bitmap icon = BitmapFactory.decodeResource(getResources(),
                R.drawable.service_logo);

        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("")
                .setTicker("")
                .setContentText("Service")
                .setSmallIcon(R.drawable.icon)
                .setLargeIcon(Bitmap.createScaledBitmap(icon, 64, 64, false))
                .setContentIntent(pendingIntent)
                .setOngoing(true)
                .build();


        startForeground(FOREGROUND_ID, notification);

        Thread tcp = new Thread(new TcpServer());
        tcp.start();

        return Service.START_STICKY;
    }

    private void publishResults(String message, int result) {
        Intent intent = new Intent(NOTIFICATION);
        intent.putExtra(MESSAGE, message);
        intent.putExtra(RESULT, result);
        broadcaster.sendBroadcast(intent);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }

    public class TCPBinder extends Binder {
        TCPListenerService getService() {
            return TCPListenerService.this;
        }
    }

    class TcpServer implements Runnable {

        @Override
        public void run() {
            try {
                ServerSocket serverSock = new ServerSocket(4999);

                while (true) {
                    Socket clientSocket = serverSock.accept();



                    Thread t = new Thread(new ClientHandler(clientSocket));
                    t.start();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    class ClientHandler implements Runnable {
        BufferedReader reader;
        Socket sock;

        public ClientHandler(Socket clientSocket) {
            try {
                sock = clientSocket;


                ois = new ObjectInputStream(clientSocket.getInputStream());

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        public void run() {
            String msg = "";
            try {


                incomingOrders = (ArrayList<Order>) ois.readObject();
                ois.close();
                sock.close();
                for (int i = 0; i < incomingOrders.size(); i++) {
                    Log.e("message", incomingOrders.get(i).getTableNo() + incomingOrders.get(i).getFoodName());
                }

                Log.e("packet", "received packet : " + counter++);

    //          Log.e("message", msg);

                result = Activity.RESULT_OK;

    //          publishResults(msg, result);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

}

尝试发送时数据传输速度太慢。任何建议将不胜感激。

0 个答案:

没有答案