我正在开发一个应用程序,我需要在Android和PC之间建立通信,以通过Wi-Fi传输一些数据。我可以通过wifi在两台PC之间进行通信。因此,PC端的代码已准备就绪。我需要一个从android端使用Wifi的参考。类似于蓝牙聊天的东西很有帮助。我能够扫描Android中存在的Wifi网络,但无法继续进行。 干杯
答案 0 :(得分:1)
这个收到一个文件
private String ReceiveFile() {
try {
ServerSocket socket = new ServerSocket(port);
socket.setSoTimeout(5000);
Socket os = null;
try {
os = socket.accept();
} catch (SocketTimeoutException t) {
if (!socket.isClosed()) socket.close();
return "TIMEOUT";
}
InputStream bos = os.getInputStream();
FileOutputStream fos = new FileOutputStream(FILENAME);
DataOutputStream bw = new DataOutputStream(fos);
int Total = 0;
byte[] buffer = new byte[4096];
int read;
while (true) {
read = bos.read(buffer);
if (read <= 0) break;
bw.write(buffer, 0, read);
Total = Total + read;
}
if (!socket.isClosed()) socket.close();
return "SUCCESS";
} catch (Exception e) {
e.printStackTrace();
return "FAILURE";
}
}
答案 1 :(得分:0)
如果不知道你想要实现的目标,很难更具体,但这个片段会收到短暂的数据突发。
DatagramSocket serverSocket = new DatagramSocket(PORTNUMBER);
byte[] receiveData = new byte[50];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.setSoTimeout(5000);
serverSocket.receive(receivePacket);
serverSocket.close();