我正在制作一个使用套接字通信将图像发送到计算机上运行的 java 应用程序的 android 应用程序
这是发生了什么:桌面正在运行服务器 java 应用程序,客户端 android 应用程序在设备上运行,它将图像传输到服务器并且该部分运行良好。之后,服务器应用程序从控制台接收一行并将其传递回 android 应用程序。直到 android 应用程序收到该消息,它应该显示一个进度对话框并且它卡在那里。android 应用程序应该使用 readLine() 读取桌面应用程序传递的字符串,但是当我尝试通过 android 应用程序中的套接字打开输入流时,它给了我异常。
以下是代码,首先是桌面服务器,然后是安卓客户端
导入 java.net.ServerSocket;导入 java.net.Socket;导入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();
}
}
}
安卓客户端:
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 行显示为 true 查看第 351 行的错误。