我一直在GitHub上运行这个WifiDirectDemo而几乎没有任何修改。我面临的问题是在创建套接字连接时..我是这么认为的。所以当我在一部手机上选择一个图像将其转移到另一部手机时,我的应用程序将在其他手机上关闭,但有以下异常:
java.net.SocketException sendto失败的ECONNRESET(由对等方重置连接)。
我通过wifi直接连接了这两部手机。
以下是我收到错误的代码:
这是调用FileTransferService:
String localIP = Utils.getLocalIPAddress();
// Trick to find the ip in the file /proc/net/arp
String client_mac_fixed = new String(this.device.deviceAddress).replace("99", "19");
String clientIP = Utils.getIPFromMac(client_mac_fixed);
Uri uri = data.getData();
TextView statusText = (TextView) mContentView.findViewById(R.id.status_text);
statusText.setText("Sending: " + uri);
Log.d(WifiDirectActivity.TAG, "Intent----------- " + uri);
Intent serviceIntent = new Intent(getActivity(), FileTransferService.class);
serviceIntent.setAction(FileTransferService.ACTION_SEND_FILE);
serviceIntent.putExtra(FileTransferService.EXTRAS_FILE_PATH, uri.toString());
if(localIP.equals(IP_SERVER)){
serviceIntent.putExtra(FileTransferService.EXTRAS_ADDRESS, clientIP);
}else{
serviceIntent.putExtra(FileTransferService.EXTRAS_ADDRESS, IP_SERVER);
}
serviceIntent.putExtra(FileTransferService.EXTRAS_PORT, PORT);
getActivity().startService(serviceIntent);
打开服务器套接字的BAckground任务:
protected String doInBackground(Void... params) {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
Log.d(WifiDirectActivity.TAG, "Server: Socket opened");
Socket client = serverSocket.accept();
Log.d(WifiDirectActivity.TAG, "Server: connection done");
final File f = new File("amankush.txt");
File dirs = new File(f.getParent());
if (!dirs.exists())
dirs.mkdirs();
f.createNewFile();
Log.d(WifiDirectActivity.TAG, "server: copying files " + f.toString());
InputStream str = client.getInputStream();
copyFile(str, new FileOutputStream(f));
//DataInputStream inputstream =new DataInputStream( client.getInputStream());
//String str = inputstream.readUTF();
serverSocket.close();
server_running = false;
return f.getAbsolutePath();
} catch (IOException e) {
Log.e(WifiDirectActivity.TAG, e.getMessage());
return null;
}
}
@Override
protected void onPostExecute(String result) {
if (result != null) {
statusText.setText("File copied - " + result);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
context.startActivity(intent);
}
}
@Override
protected void onPreExecute() {
statusText.setText("Opening a server socket");
}
最后filetransferservice:
public class FileTransferService extends IntentService
{
private static final int SOCKET_TIMEOUT = 5000;
public static final String ACTION_SEND_FILE = "com.example.android.wifidirect.SEND_FILE";
public static final String EXTRAS_FILE_PATH = "file_url";
public static final String EXTRAS_ADDRESS = "go_host";
public static final String EXTRAS_PORT = "go_port";
public FileTransferService(String name) {
super(name);
}
public FileTransferService() {
super("FileTransferService");
}
@Override
protected void onHandleIntent(Intent intent) {
Context context = getApplicationContext();
if (intent.getAction().equals(ACTION_SEND_FILE)) {
String fileUri = intent.getExtras().getString(EXTRAS_FILE_PATH);
String host = intent.getExtras().getString(EXTRAS_ADDRESS);
Socket socket = new Socket();
int port = intent.getExtras().getInt(EXTRAS_PORT);
try {
Log.d(WifiDirectActivity.TAG, "Opening client socket - ");
socket.bind(null);
socket.connect((new InetSocketAddress(host, port)), SOCKET_TIMEOUT);
if(socket.isBound() && socket.isConnected() && socket.isClosed())
Log.d(WifiDirectActivity.TAG, "Socket not bound");
Log.d(WifiDirectActivity.TAG, "Client socket - " + socket.isConnected());
OutputStream os = socket.getOutputStream();
ContentResolver cr = context.getContentResolver();
InputStream stream = null;
try {
stream = cr.openInputStream(Uri.parse(fileUri));
//stream = new DataOutputStream(socket.getOutputStream());
//stream.writeUTF("a string");
} catch (FileNotFoundException e) {
Log.d(WifiDirectActivity.TAG, e.toString());
}
DeviceDetailFragment.copyFile(stream, os);
Log.d(WifiDirectActivity.TAG, "Client: Data written");
} catch (IOException e) {
Log.e(WifiDirectActivity.TAG, e.getMessage());
} finally {
if (socket != null) {
if (socket.isConnected()) {
try {
socket.close();
} catch (IOException e) {
// Give up
e.printStackTrace();
}
}
}
}
}
有人能告诉我为什么会这样吗?
答案 0 :(得分:0)
我遇到了同样的问题...
看看这个:
String localIP = Utils.getLocalIPAddress();
转到Utils
课程,它始终返回null
。将值更改为return。
希望这对你有所帮助。