-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