我正在和使用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行處理錯誤。
我不確定這是否可能是原因,但是您可以在從套接字讀取數據後嘗試將'os.close()'調用移動到。我不記得是否關閉輸出流也關閉了套接字,或者如果它只是在關閉輸入流時發生這種情況.. – Patrick 2012-04-15 16:25:55
@patrick不,它不,請參閱我的答案。 – EJP 2012-04-15 22:45:49
@EJP:哦,對不起,一定是把它和別的東西混淆了。 – Patrick 2012-04-16 08:06:28