我想实现带套接字的传输,用于传输图片,如果图片被加载则检查服务器,如果确实没有传输图片,只需更改图像。
我有一个客户端和一台服务器,只需在两台设备之间传输图片。但我想在发送之前每次发送一次每张图片的哈希值。我的主要是在线程管理器上,我已经实现了第一个线程的监听器(在客户端/服务器上发送/接收图片的哈希)以知道它何时完成,然后启动第二个线程(在客户端/服务器上发送/接收图片) 。但我不知道我的方法是否正常,因为它不能很好地工作(服务器在开始时接收哈希而不是另一个)。
我希望我很清楚,这是我的代码部分,感谢有人能给我一个解决方案。
服务器
私有类ReceiverHash扩展了线程{
private List<TaskListener> listeners = new ArrayList<TaskListener>();
public void addListener(TaskListener listener) {
listeners.add(listener);
}
void getTaskState(Boolean state) {
for (TaskListener listener : listeners) {
listener.getTaskState(state);
}
}
@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(socketServerPORT);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
while (true) {
String str = br.readLine(); // lecture du message
if (str.equals("END")) {
serverSocket.close();
getTaskState(true);
break;
}
hashList.add(str);
Log.d("HASH_RECEIVER_LOG", str); // Log debug hash
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class Receiver extends Thread {
int pos = 0;
int cmpt = 0;
@Override
public void run() {
// create ServerSocket using specified port
try {
ServerSocket serverSocket = new ServerSocket(socketServerPORT);
while (true) {
//checkFileExist();
File f = new File(getFilesDir(), "img" + Integer.toString(pos) + ".png");
fileList.add(f);
Log.d("HASH_RECEIVER_LOG", "Attente d'une réponse du client");
Socket socket = serverSocket.accept();
Log.d("HASH_RECEIVER_LOG", "Reception d'une réponse");
f.createNewFile();
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = new FileOutputStream(f);
byte[] bytes = new byte[2 * 1024];
int count;
while ((count = inputStream.read(bytes)) > 0) {
Log.d("RECEIVER_CMPT", Integer.toString(cmpt));
outputStream.write(bytes, 0, count);
cmpt = cmpt + count;
}
myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
runOnUiThread(new Runnable() {
@Override
public void run() {
imgDiapo.setImageBitmap(myBitmap);
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
我如何在活动中使用听众:
ReceiverHash receiverHash = new ReceiverHash();
receiverHash.start();
receiverHash.addListener(this);
客户端
public class Sender extends AsyncTask<Void, Void, Void> {
private String dstAddress;
private int dstPort;
private String fPath;
public Sender(String address, int port, String path) {
dstAddress = address;
dstPort = port;
fPath = path;
}
@Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
FileInputStream inputStreamSend = new FileInputStream(fPath);
OutputStream outputStreamSend = socket.getOutputStream();
byte[] buffer = new byte[2 * 1024];
int count;
while ((count = inputStreamSend.read(buffer)) > 0) {
outputStreamSend.write(buffer, 0, count);
}
outputStreamSend.close();
inputStreamSend.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public class HashSender extends AsyncTask<Void, Void, Void> {
private String dstAddress;
private int dstPort;
String hash;
//private ArrayList <String> listHash;
private List<TaskListener> listeners = new ArrayList<TaskListener>();
public HashSender(String address, int port, String path) throws IOException, NoSuchAlgorithmException {
dstAddress = address;
dstPort = port;
hash = sha1(new File(path)); //methode encryption in sha1
}
public void addListener(TaskListener listener) {
listeners.add(listener);
}
void getTaskState(Boolean state) {
for (TaskListener listener : listeners) {
listener.getTaskState(state);
}
}
@Override
protected void onPostExecute(Void v) {
getTaskState(true);
}
@Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
OutputStream outputStreamSend = socket.getOutputStream();
BufferedWriter buf = new BufferedWriter(new OutputStreamWriter(outputStreamSend));
buf.write(hash);
buf.newLine();
buf.flush();
Log.d("toto", hash);
buf.write("END");
buf.newLine();
buf.flush();
outputStreamSend.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
我如何在活动中使用听众:
HashSender hashSender = new HashSender(dstAddress.get(i), dstPort, selectedItemPath.get(pos));
hashSender.addListener(ControlsActivity.this);
hashSender.execute();