Android通过套接字发送图像失败:“client.getOutputStream()”不起作用

时间:2014-06-24 21:02:16

标签: android sockets

我无法使用此代码,特别是指令" outputStream = client.getOutputStream();"似乎不起作用,我无法弄清楚原因。任何帮助将非常感谢,感谢提前的人

public class FileActivity extends Activity {

private FileInputStream fileInputStream;
 private BufferedInputStream bufferedInputStream;
 private OutputStream outputStream;

private byte [] mybytearray;

private String tmp = null;

private TextView tv;

private File myFile;

private int l;

private String path;

private EditText editText;

private ServerSocket serverSocket;

private Socket client;

public static String SERVERIP = "10.0.2.15";

private final int SERVERPORT = 8080;

private byte [] imgbyte;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_file);

    tv = (TextView) findViewById(R.id.textView1);
    editText = (EditText) findViewById(R.id.editText1);

    SERVERIP = getLocalIpAddress();

    Thread sThread = new Thread(new ServerThread());
    sThread.start();
}

public void sendListener(View v) {

    tmp = editText.getText().toString();

    path = "/sdcard/" + tmp;

    myFile = new File(path);Log.d("SERVER", "WORKS");           

    l = (int) myFile.length();Log.d("SERVER", "WORKS");

    tv.setText(path + "  " + Integer.toString(l));  

    tmp = Integer.toString(l);  Log.d("SERVER", "WORKS");

    String test = tmp;



    //out.println(tmp);
    Log.d("SERVER", "WORKS");
    try {

        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
        out.println(test);
        out.close();
        Log.d("SERVER", "WORKS");
        byte[] mybytearray = new byte[l]; //create a byte array to file
        Log.d("SERVER", "WORKS");
        fileInputStream = new FileInputStream(myFile);
         bufferedInputStream = new BufferedInputStream(fileInputStream);  
         Log.d("SERVER", "WORKS");

         bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file
         Log.d("SERVER", "WORKS");
         try{
             outputStream = client.getOutputStream();
         } catch(Exception e)
         {
             Log.d("OUTPUT", "UFFFF");
         }
         Log.d("SERVER", "ALMOST");
         outputStream.write(mybytearray, 0, mybytearray.length); Log.d("SERVER", "DONE");//write file to the output stream byte by byte
        /* outputStream.flush();
         try{
             outputStream = client.getOutputStream();
         } catch(Exception e)
         {
             Log.d("OUTPUT", "UFFFF");
         }*/

       /*  
         Log.d("SERVER", "FUUUNZIONAAAA");
         int count = 0;

         while((count = bufferedInputStream.read(mybytearray, 0 , mybytearray.length)) != -1)
         {
             Log.d("SERVER", "FINEEEE");
             outputStream.write(mybytearray, 0, count);
             Log.d("TEST", Integer.toString(count));
         }
         Log.d("SERVER", "FINEEEE");
         bufferedInputStream.close();
         outputStream.close();
               client.close();*/
    } catch (Exception e) {

    }

        /*
    }

    Log.d("SERVER", "FUUUNZIONAAAA");
    tv.setText(path + "  " + Integer.toString(l));      

    try {
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
        fis = new FileInputStream(file);
        bis = new BufferedInputStream(fis);
        os = client.getOutputStream();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {   
        byte [] mybytearray  = new byte[l]; 
        Log.d("SERVER", "FUUUNZIONAAAA");

        Log.d("SERVER", "FUUUNZIONAAAA");
        out.println(Integer.toString(l));
        Log.d("SERVER", "INVIATO0");

        int read_count = 0;
        while ((read_count = bis.read(mybytearray, 0, mybytearray.length)) != -1) {
            os.write(mybytearray, 0, read_count); // Now writes the correct amount of bytes
        }

        Log.d("SERVER", "INVIATO1");

        out.close();
        fis.close();
        bis.close();
        os.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } */


}


public class ServerThread implements Runnable {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            serverSocket = new ServerSocket(SERVERPORT);
            client = serverSocket.accept();
            //outputStream = client.getOutputStream();

            Log.d("SERVER", "Connesso");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.d("TEST", "UFFFAAA");
        }           
    }

}

private String getLocalIpAddress() {

    String tmp = "";

    int i = 0;

    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) 
                { 

                    tmp += "IP: " + inetAddress.getHostAddress() + "\n"; 

                }
            }
        }
    } catch (SocketException ex) {
        Log.e("ServerActivity", ex.toString());
    }
    return tmp;
}

@Override
protected void onStop() {
    super.onStop();
    try {
         serverSocket.close();
     } catch (IOException e) {
         e.printStackTrace();
     }
}

}

这是我遇到的错误

06-25 01:01:41.504: W/System.err(22561): java.net.SocketException: Socket is closed 06-25 01:01:41.504:
at java.net.PlainSocketImpl.checkNotClosed(PlainSocketImpl.java) 06-25 01:01:41.504:
at java.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java) 06-25 01:01:41.504:
at java.net.Socket.getOutputStream(Socket.java) 06-25 01:01:41.504:
at com.illiano.esameserver.FileActivity.sendListener(FileActivity.java:120) 06-25 01:01:41.504:
at java.lang.reflect.Method.invokeNative(Native Method) 06-25 01:01:41.504:
at java.lang.reflect.Method.invoke(Method.java) 06-25 01:01:41.504:
at android.view.View$1.onClick(View.java) 06-25 01:01:41.504:
at android.view.View.performClick(View.java) 06-25 01:01:41.504:
at android.view.View$PerformClick.run(View.java) 06-25 01:01:41.504:
at android.os.Handler.handleCallback(Handler.java) 06-25 01:01:41.504:
at android.os.Handler.dispatchMessage(Handler.java) 06-25 01:01:41.504:
at android.os.Looper.loop(Looper.java) 06-25 01:01:41.504:
at android.app.ActivityThread.main(ActivityThread.java) 06-25 01:01:41.504:
at java.lang.reflect.Method.invokeNative(Native Method) 06-25 01:01:41.504:
at java.lang.reflect.Method.invoke(Method.java) 06-25 01:01:41.504:
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) 06-25 01:01:41.504:
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 06-25 01:01:41.504:
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 06-25 01:01:41.504:
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

java.net.SocketException: Socket is closed

表示关闭了套接字,然后继续使用它。

可能你不知道关闭套接字的输入流或输出流会关闭另一个流和套接字。