-2
我有一個Android客戶端和多線程Java服務器。這個服務器最初是用Python編寫的,效果很好,但現在我用Java重新編寫了它,但它似乎沒有工作。以下是我的服務器代碼。值得一提的是,Python的實現並不是多線程的,但我不認爲我需要爲此改變客戶端。的Java線程TCP服務器套接字
import java.net.*;
import java.io.*;
import org.apache.commons.io.FileUtils;
public class MultiServerThread extends Thread {
private Socket socket = null;
public MultiServerThread(Socket socket) {
super("MultiServerThread");
this.socket = socket;
}
@Override
public void run() {
try {
String path = "C:/Users/LandClan/Desktop/cubikal";
int count = 0;
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(socket.getOutputStream()));
DataInputStream dis = new DataInputStream(new BufferedInputStream(
socket.getInputStream()));
File[] files = new File(path).listFiles();
for (File file : files) {
String filename = file.getName();
String extension = filename.substring(
filename.lastIndexOf(".") + 1, filename.length());
if ("png".equals(extension)) {
count += 1;
}
}
System.out.println("Sending " + count + " files");
dos.writeInt(count);
byte[] temp = new byte[1024];
int n = 0;
for (File file : files) {
String filename = file.getName();
String extension = filename.substring(
filename.lastIndexOf(".") + 1, filename.length());
if ("png".equals(extension)) {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
byte fileContent[] = new byte[(int) file.length()];
bis.read(fileContent);
int dataLength = fileContent.length;
dos.writeInt(dataLength);
System.out.println(filename + " is " + dataLength
+ " bytes long");
while ((dataLength > 0)
&& (n = bis.read(temp, 0,
(int) Math.min(temp.length, dataLength))) != -1) {
dos.write(temp, 0, n);
dos.flush();
dataLength -= n;
}
// System.out.println("Sent file "+filename);
fis.close();
}
}
for (File file1 : files) {
String filename = file1.getName();
String extension = filename.substring(
filename.lastIndexOf(".") + 1, filename.length());
if ("txt".equals(extension)) {
FileInputStream fis = new FileInputStream(file1);
BufferedInputStream bis = new BufferedInputStream(fis);
byte fileContent[] = new byte[(int) file1.length()];
bis.read(fileContent);
int dataLength = fileContent.length;
dos.writeInt(dataLength);
System.out.println("file is " + dataLength + "long");
while ((dataLength > 0)
&& (n = bis.read(temp, 0,
(int) Math.min(temp.length, dataLength))) != -1) {
dos.write(temp, 0, n);
dos.flush();
dataLength -= n;
}
// System.out.println("Sent file");
fis.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
這裏是服務器
package server;
import java.net.*;
import java.io.*;
public class Server {
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
try {
serverSocket = new ServerSocket(4447);
} catch (IOException e) {
System.err.println("Could not liten on port: 4447.");
System.exit(-1);
}
while (listening) {
new MultiServerThread(serverSocket.accept()).start();
}
serverSocket.close();
}
}
如果你告訴我們你想要它做的事情,這將有助於?而不是閱讀你的代碼,並嘗試理解。 – roni
它應該是從服務器發送圖片和文本文件給客戶 – user1661396
就可以說明問題,而不是工作太一般。 – roni