Java br.readline()在br.read()工作时卡住
我正在制作一个简单的ftp客户端/服务器程序,根据客户端的命令列出文件,告诉当前目录,下载文件 我的客户机代码运行良好,因为我已经用一台工作的服务器对其进行了测试。但是,我设计的服务器卡在了行字符串message=br.readline()上的run()函数中;如果我改为使用br.read(),那么它可以工作,但我需要字符串形式的命令来知道我必须下载哪个文件,而br.read()返回int。这是我的代码,我使用了线程Java br.readline()在br.read()工作时卡住,java,networking,Java,Networking,我正在制作一个简单的ftp客户端/服务器程序,根据客户端的命令列出文件,告诉当前目录,下载文件 我的客户机代码运行良好,因为我已经用一台工作的服务器对其进行了测试。但是,我设计的服务器卡在了行字符串message=br.readline()上的run()函数中;如果我改为使用br.read(),那么它可以工作,但我需要字符串形式的命令来知道我必须下载哪个文件,而br.read()返回int。这是我的代码,我使用了线程 public class Myserver { static final in
public class Myserver {
static final int PortNumber = 108;
static ServerSocket MyService;
static Socket clientSocket = null;
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File directory;
directory = new File(System.getProperty("user.home"));
try {
MyService = new ServerSocket(PortNumber);
String cd = directory.toString();
System.out.println(cd);
System.out.println("Listening on " + PortNumber);
while(true) {
clientSocket = MyService.accept();
Connecthandle a = new Connecthandle(clientSocket, directory);
a.run();
}
}
catch (IOException e) {
System.out.println(e);
}
}
static class Connecthandle extends Thread {
File Directory;
Socket clientsocket;
// Constructor for class
Connecthandle(Socket clients, File dir) {
clientsocket = clients;
Directory = dir;
}
// Works Fine
void listfiles() throws IOException {
String []Listfile = Directory.list();
String send = "";
for (int j = 0; j < Listfile.length; j++) {
send = send + Listfile[j] + ",";
}
DataOutputStream GoingOut = new DataOutputStream(clientsocket.getOutputStream());
GoingOut.writeBytes(send);
GoingOut.flush();
GoingOut.close();
}
// Works Fine
void currentdirectory() throws IOException {
String cd = Directory.toString();
String cdd = "resp," + cd;
System.out.println(cdd);
DataOutputStream GoingOut = new DataOutputStream(clientsocket.getOutputStream());
GoingOut.writeBytes(cdd);
GoingOut.flush();
GoingOut.close();
System.exit(0);
}
void sendfiles(String fileName) {
try {
File nfile = new File(fileName);
DataOutputStream GoingOut = new DataOutputStream(clientsocket.getOutputStream());
if ( (! nfile.exists()) || nfile.isDirectory() ) {
GoingOut.writeBytes("file not present");
} else {
BufferedReader br = new BufferedReader(new FileReader(nfile));
String line;
while ((line = br.readLine()) != null) {
line = br.readLine();
GoingOut.writeBytes(line+"\n");
}
GoingOut.flush();
GoingOut.close();
br.close();
}
} catch (IOException e) {
System.out.println("Unable to send!");
}
}
@SuppressWarnings("deprecation")
public void run() {
try {
DataInputStream comingin = new DataInputStream(clientsocket.getInputStream());
InputStreamReader isr = new InputStreamReader(comingin, "UTF-8");
BufferedReader br = new BufferedReader(isr);
System.out.println("here");
// if (br.ready())
String message = br.readLine(); // Code gets stuck here, if i use br.read() it works, but i need string output.
if (message.equals("listfiles\n")) {
listfiles();
} else if (message.equals("pwd")) {
currentdirectory();
} else if (message.contains("getfile,")) {
String fileName = new String(message.substring(8, message.length()));
sendfiles(fileName);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
clientsocket.close();
} catch (IOException e) {}
}
}
}
公共类Myserver{
静态最终int端口号=108;
静态服务器套接字MyService;
静态套接字clientSocket=null;
/**
*@param args
*@抛出异常
*/
公共静态void main(字符串[]args)引发IOException{
文件目录;
directory=新文件(System.getProperty(“user.home”);
试一试{
MyService=newserversocket(端口号);
字符串cd=directory.toString();
系统输出打印项次(cd);
System.out.println(“监听”+端口号);
while(true){
clientSocket=MyService.accept();
Connecthandle a=新的Connecthandle(clientSocket,目录);
a、 run();
}
}
捕获(IOE异常){
系统输出打印ln(e);
}
}
静态类Connecthandle扩展线程{
文件目录;
插座客户端插座;
//类的构造函数
Connecthandle(套接字客户端,文件目录){
clientsocket=客户端;
Directory=dir;
}
//很好
void listfiles()引发IOException{
String[]Listfile=Directory.list();
字符串send=“”;
对于(int j=0;j
}如果readLine()被阻塞,而您正在发送数据,则您没有发送换行。不,不完全是。我也可以使用.start()。您发送的文件中有什么内容?请记住,
readLine
正在查找(1)文本和(2)换行符。在发送任意文件时,使用byte[]
几乎总是一个更好的选择,尤其是当您可以使用NIO时。在sendfiles
中,您一次读取两行,这可能是您不希望的。通过run()中的.readline()我尝试读取从客户端发送的命令。我要发送的文件是.txt文件。但是,从clientI读取命令时会出现问题。我将修复sendfiles,但这不会导致run()中的问题