2012-04-15 84 views
0

我正在和使用socket通信Android應用程序將圖像發送到一個Java應用程序的計算機SocketException - 「關閉套接字」即使isConnected()返回true

這裏上運行就是發生的事情:在桌面正在運行一個服務器java應用程序,客戶端android應用程序在設備上運行,它將圖像傳輸到服務器,並且該部分順利。之後,服務器應用程序從控制檯接收一行並將其傳回給android應用程序。直到那個消息被android應用程序接收到,它應該顯示一個進度對話框,並在那裏卡住。 android應用程序應該使用readLine()讀取桌面應用程序傳遞的字符串,但是當我嘗試通過android應用程序中的套接字打開輸入流時,它會給我例外。

以下是代碼,第一個爲桌面服務器,那麼對於Android客戶端

進口java.net.ServerSocket的; import java.net.Socket; import java.io. *;

class ProjectServer 
{ 
    ServerSocket serSock; 
    Socket sock; 

    BufferedReader in; 
    PrintWriter out; 
    public static void main(String ar[]) 
    { 
     try 
     { 
      ProjectServer cs=new ProjectServer(); 
      cs.startServer(); 
     } 
     catch(Exception e) 
     { 

     } 
    } 

    public void startServer() 
    { 
     try 
     { 
      serSock=new ServerSocket(8070); 
      System.out.println("Waiting for client..."); 
      sock=serSock.accept(); 


      System.out.println("Connections done"); 


      //Accept File 
      System.out.println("Connected"); 
      System.out.println(sock.isConnected()+"1"); 

      //receive code 

      int filesize=450660; 
      int bytesRead; 
      int current=0; 
      // receive file 
      byte [] mybytearray = new byte [filesize]; 
      InputStream is = sock.getInputStream(); 
      FileOutputStream fos = new FileOutputStream("C:\\Project Server\\Capture.png"); 
      BufferedOutputStream bos = new BufferedOutputStream(fos); 
      bytesRead = is.read(mybytearray,0,mybytearray.length); 
      current = bytesRead; 


      do { 
       bytesRead = 
        is.read(mybytearray, current, (mybytearray.length-current)); 
       if(bytesRead >= 0) current += bytesRead; 
      } while(bytesRead > -1); 

      bos.write(mybytearray, 0 , current); 
      bos.flush(); 
      System.out.println("end-start"); 
      bos.close(); 
      //sock.close(); 
      //receive code ends 

      //System.out.println(br.readLine()); 


      //Matlab computation 

      //Send result 
      System.out.println(sock.isConnected()+"2"); 
      PrintWriter pr=new PrintWriter(sock.getOutputStream(),true); 

      pr.println((new BufferedReader(new InputStreamReader(System.in))).readLine()); 
      System.out.println(sock.isConnected()+"3"); 
      (new BufferedReader(new InputStreamReader(System.in))).readLine(); 
      System.out.println(sock.isConnected()+"4"); 
     } 
     catch(Exception e) 
     { 
      System.out.println(e); 
      e.printStackTrace(); 
     } 

    } 
} 

Android客戶端:

package com.site.custom; 

import java.io.BufferedInputStream; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.PrintWriter; 
import java.net.Socket; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 


public class Act2 extends Activity 
{ 
    private ProgressDialog pd; 
    private String serverIP="58.146.100.187"; 
    private BufferedReader in; 
    private PrintWriter out; 
    private String path; 
    private Socket cliSock; 

    public void onCreate(Bundle onCreateInstance) 
    { 
     super.onCreate(onCreateInstance); 
     setContentView(R.layout.act2); 
     this.setTitle("This has started"); 
     path=getIntent().getStringExtra("path"); 

     //Establish Connection 
     //pd=ProgressDialog.show(this, "Establishing connection", "Finding server",false,true); 

     try 
     { 
      cliSock=new Socket(serverIP,8070); 

      //pd.dismiss(); 
      //Log.v("MERA MSG","changing text"); 
      ((TextView)findViewById(R.id.tview)).setText(path); 
     } 
     catch(Exception e) 
     { 
      Log.v("MERA MSG",e.toString()); 
     } 

     //Send file 
     //Log.v("MERA MSG","changing text1"); 

     ProgressDialog pd=ProgressDialog.show(this, "Sending image", "Image chosen:"+path.substring(path.lastIndexOf("//")+1),false,true); 
     //Log.v("MERA MSG","changing text2"); 

     try 
     { 


      File myFile = new File (path); 
      System.out.println((int)myFile.length()); 
      byte[] mybytearray = new byte[450560]; 
      FileInputStream fis = new FileInputStream(myFile); 
      BufferedInputStream bis = new BufferedInputStream(fis); 
      bis.read(mybytearray,0,mybytearray.length); 
      OutputStream os = cliSock.getOutputStream(); 
      System.out.println("Sending..."); 
      os.write(mybytearray,0,mybytearray.length); 
      os.flush(); 
      os.close(); 
      bis.close(); 
      //sock.close(); 
      //System.out.println("Completed"); 
      System.out.println(cliSock.isConnected()+"1"); 
      pd.dismiss(); 
      //System.out.println("Done"); 
      System.out.println(cliSock.isConnected()+"2"); 
      //Show dialog box till computation results arrive 
      pd=ProgressDialog.show(this, "Recognizing...", "(waiting for server reply)",false,true); 
      System.out.println(cliSock.isConnected()+"3"); 
      in=new BufferedReader(new InputStreamReader(cliSock.getInputStream())); 
      System.out.println(in.readLine()); 
      pd.dismiss(); 

     } 
     catch(Exception e) 
     { 
      Log.v("MERA MSG",e.toString()); 
      e.printStackTrace(); 
     } 



    } 
} 

而且順便說一句這裏有原木的logcat: http://pastebin.com/atHMycTa 可以確認插座還在這裏連接,因爲它顯示了線350和349 看真在351行處理錯誤。

+0

我不確定這是否可能是原因,但是您可以在從套接字讀取數據後嘗試將'os.close()'調用移動到。我不記得是否關閉輸出流也關閉了套接字,或者如果它只是在關閉輸入流時發生這種情況.. – Patrick 2012-04-15 16:25:55

+0

@patrick不,它不,請參閱我的答案。 – EJP 2012-04-15 22:45:49

+0

@EJP:哦,對不起,一定是把它和別的東西混淆了。 – Patrick 2012-04-16 08:06:28

回答

2

isConnected()只告訴你是否曾經連接過這個Socket,你有哪個,所以它返回true。這並不表示連接的狀態。

+1

好的,謝謝。但你仍然不告訴我如何解決我的問題.. – GrowinMan 2012-04-16 01:43:51

+0

@GrowinMan你的問題是你關閉了你的套接字,然後繼續使用它。關閉輸出流會關閉套接字。只需刷新它,然後獲取輸入流,然後繼續。你也不需要任何'ByteArrayOutputStreams'。他們只是在浪費時間和空間。從文件讀取,寫入套接字,或反之亦然。* – EJP 2012-04-16 03:16:56

+0

flush命令()似乎不起作用。如果我只是做flush(),那麼服務器仍然在等待圖像被接收。只有在我做os.close()之後,它才真正刷新流,服務器完全接收圖像,然後繼續執行。有沒有一種方法可以在不關閉的情況下刷新輸出流? – GrowinMan 2012-04-17 03:24:38