我正在尝试使用Wifi P2p在android上制作一个简单的文件共享应用。 但是未建立连接,因为服务器未接受客户端的连接。 这是我发件人设备的代码。
private class FileSender implements Runnable
{
private String filename;
private File mFile;
private File rootFolder;
private ServerSocket serverSocket;
private Socket socket;
private FileInputStream fileInputStream;
private BufferedInputStream bufferedInputStream;
private DataOutputStream dataOutputStream;
FileSender(File rootFolder, String filename) {
this.rootFolder = rootFolder;
this.filename = filename;
this.mFile = new File(this.rootFolder.getAbsolutePath() + "/" + this.filename);
if(mFile.exists())
{
Log.e("File","Found");
}else
{
Log.e("File","NOT Found");
}
}
@Override
public void run() {
try {
Log.e("Connecting to","Client");
serverSocket = new ServerSocket(2050);
socket = serverSocket.accept();
Log.e("Connection","Made");
dataOutputStream = new DataOutputStream(socket.getOutputStream());
fileInputStream = new FileInputStream(mFile);
bufferedInputStream = new BufferedInputStream(fileInputStream);
int len = (int)mFile.length();
byte data[] = new byte[len];
int l;
while( (l = bufferedInputStream.read(data,0,len)) > 0)
{
dataOutputStream.write(data,0,l);
}
//Toast.makeText(getApplicationContext(),"File Sent",Toast.LENGTH_LONG).show();
Log.e("File","Sent");
} catch (IOException e) {
e.printStackTrace();
}
}
void stop()
{
try
{
dataOutputStream.close();
bufferedInputStream.close();
fileInputStream.close();
socket.close();
serverSocket.close();
}catch(Exception e){
}
}
}
接收器设备:
private class FileReceiver implements Runnable
{
InetAddress host_address;
String filename;
Socket socket;
DataInputStream dataInputStream;
File rootFolder;
File mFile;
FileOutputStream fileOutputStream;
BufferedOutputStream bufferedOutputStream;
boolean fileExists;
FileReceiver(InetAddress host_address,File rootFolder,String filename) {
this.host_address = host_address;
this.filename = filename;
this.rootFolder = rootFolder;
this.mFile = new File(this.rootFolder.getAbsolutePath() + "/" + this.filename);
if(!mFile.exists())
{
try {
if(mFile.createNewFile())
{
Log.e("File","Created");
}else
{
Log.e("File NOT","Created");
}
fileExists = false;
} catch (IOException e) {
e.printStackTrace();
}
}else
{
Log.e("File","Already Exists");
fileExists = true;
}
}
@Override
public void run() {
try {
Log.e("Connecting to","Server");
socket = new Socket(host_address,2050);
Log.e("Connection","Made");
fileOutputStream = new FileOutputStream(mFile);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
dataInputStream = new DataInputStream(socket.getInputStream());
byte data[] = new byte[1000005];
int l;
while( (l = dataInputStream.read(data,0,1000005)) > 0)
{
bufferedOutputStream.write(data,0,l);
}
Toast.makeText(getApplication(),"File Received",Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
void stop()
{
try {
if(bufferedOutputStream != null)
bufferedOutputStream.close();
if(fileOutputStream!=null)
fileOutputStream.close();
if(dataInputStream != null)
dataInputStream.close();
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在记录“连接到客户端”后,发送方不会继续前进,并且在记录“连接到服务器”之后,接收方会等待。 服务器设备的InetAddress是正确的,因为一旦建立wifi连接,它就会由android WifiP2p管理器生成。
此外,在此之前,我已经实现了聊天功能。我已经在两个设备上的特定端口(2048)上创建了套接字,以实现聊天功能。它运行完美,并且可以在设备之间轻松建立连接。
文件传输功能的套接字不同,端口(2050)也不同,但这不起作用。
您还可以建议是否有更好的方法来实现这两个功能。