当前位置: 首页 > 知识库问答 >
问题:

通过同一套接字的多条消息不起作用

仇正豪
2023-03-14

我在一个简单的ftp服务器上工作,客户端必须向服务器发送多条消息,对于每条消息,服务器向客户端发回一个应答。当客户端发送一条消息时,它可以完美地工作,并且服务器没有任何问题地响应,例如,当客户端发送“用户用户名”时,服务器向客户端发回“需要密码”。

但是当客户端发送另一条消息“PASS密码”(使用同一个套接字)时,它不起作用!只有第一个交换有效(对于用户名),当发送第一条消息时,服务器应答器没有任何问题,但当它想发送第二条消息(对于密码)时,它会阻塞。

有人能帮我吗?谢谢!!

这是我的代码:

 @Test
public void testProcessPASS() throws IOException{

    Socket socket = new Socket(server.getAddress(), server.getcmdPort());

    this.ClientReceiveMessage(socket); // to flush
    String cmd = "USER user_test\r\n";
    this.ClientSendMessage(socket, cmd);
    String anwser = this.ClientReceiveMessage(socket); 
    assertEquals("Response error.", Constants.MSG_331.replace("\r\n", ""), anwser);

    //PROBLEME STARTS HERE :/

    String cmd2 = "PASS pass_test\r\n";
    this.ClientSendMessage(socket, cmd2);
    String anwser2 = this.ClientReceiveMessage(socket); 
    assertEquals(Constants.MSG_230.replace("\r\n", ""), anwser2);
    socket.close();

}


public void ClientSendMessage(Socket skt, String msg) throws IOException{

    PrintWriter messageClient = new PrintWriter(new OutputStreamWriter(skt.getOutputStream()),true);
    messageClient.println(msg);
    messageClient.flush();

}

public String ClientReceiveMessage(Socket skt) throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
    String res = br.readLine() ;
    return res; 
}

这是服务器代码:

 public class Server implements Runnable {

private ServerSocket cmdserverSocket;
private ServerSocket dataServerSocket;

private boolean running;

public Server() throws IOException {
    this.cmdserverSocket = new ServerSocket(1024);
    this.dataServerSocket = new ServerSocket(1025);
    this.running = false;
}

public boolean isRunning() {
    return this.running;
}

public InetAddress getAddress() {
    return this.cmdserverSocket.getInetAddress();
}

public int getcmdPort() {
    return this.cmdserverSocket.getLocalPort();
}

public int getDataPort() {
    return this.dataServerSocket.getLocalPort();
}

public void run() {
    // TODO Auto-generated method stub
    this.running = true;
    System.out.println("server started on port : " + this.getcmdPort());

    while (this.running) {
        try {
            Socket socket = this.cmdserverSocket.accept();
            new Thread(new FtpRequest(socket, this.dataServerSocket))
                    .start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("server error : " + e.getMessage());
            this.running = false;
        }
    }
}

}

这是处理客户端请求并向客户端发送消息并在新线程上运行的类:

 public class FtpRequest implements Runnable {

private Socket cmdSocket;
private Socket dataSocket;
private BufferedReader cmdBufferedReader;
private DataOutputStream cmdDataOutputStream;
private ServerSocket dataServerSocket;
private boolean anonymous;
private boolean connected;
private String username;
private boolean processRunning;
private String directory;

public FtpRequest(Socket cmds, ServerSocket dts) throws IOException {

    this.cmdSocket = cmds;
    this.dataServerSocket = dts;
    this.cmdBufferedReader = new BufferedReader(new InputStreamReader(
            this.cmdSocket.getInputStream()));
    this.cmdDataOutputStream = new DataOutputStream(
            this.cmdSocket.getOutputStream());
    this.anonymous = true;
    this.connected = false;
    this.username = Constants.ANONYMOUS_USER;
    this.processRunning = true;
    this.directory = "/home";

}

/**
 * send a message on the socket of commands
 * 
 * @param msg
 *            the msg to send on the socket of commands
 * @throws IOException
 */
public void sendMessage(String msg) throws IOException {
    System.out.println("FtpRequest sendMessage : " + msg);
    PrintWriter messageClient = new PrintWriter(new OutputStreamWriter(
            this.cmdDataOutputStream), true);
    messageClient.println(msg);
    messageClient.flush();

    /*
     * this.cmdDataOutputStream.writeBytes(msg);
     * this.cmdDataOutputStream.flush(); this.cmdSocket.close();
     */
}

public void run() {
    // TODO Auto-generated method stub
    System.out.println("FtpRequest running ...");
    try {
        this.sendMessage(Constants.MSG_220); // service ready for new user
        this.handleRequest();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // service ready for new user

}

/**
 * this method handle the request readen from cmd socket and run the
 * required method
 * 
 * @throws IOException
 */
private void handleRequest() throws IOException {

    String rqst = this.cmdBufferedReader.readLine();

    Request request = new Request(rqst);
    System.out.println("FtpRequest handleRequest" + rqst);

    switch (request.getType()) {
    case USER:
        this.processUSER(request);
        break;

    case PASS:
        this.processPASS(request);
        break;

    default:
        this.sendMessage(Constants.MSG_502); // Command not implemented.\r\n
        break;

    }

    /*
     * if (this.processRunning = true) this.handleRequest();
     * 
     * else { this.cmdSocket.close(); System.out.println("socket closed ");
     * }
     */

}

private void processUSER(Request rqst) throws IOException {

    System.out.println("FtpRequest processUSER");
    if (rqst.getArgument().equals(Constants.ANONYMOUS_USER)) {
        this.sendMessage(Constants.MSG_230); // user loged in
        this.connected = true;
        this.anonymous = true;
        this.username = Constants.ANONYMOUS_USER;
    } else if (rqst.getArgument().equals(Constants.USER_TEST)) {
        this.sendMessage(Constants.MSG_331); // User name okay, need
                                                // password.\r\n
        this.username = Constants.USER_TEST;
    } else
        this.sendMessage(Constants.MSG_332);
}

private void processPASS(Request rqst) throws IOException {
    System.out.println("FtpRequest processPASS");
    if (rqst.getArgument().equals(Constants.USER_TEST)
            && rqst.getArgument().equals(Constants.PASS_TEST)) {
        this.sendMessage(Constants.MSG_230);
        this.connected = true;
        this.anonymous = false;
    } else
        this.sendMessage(Constants.MSG_332); // au cas seulement le mot de
                                                // passe est fourni
}


}

共有1个答案

丰景同
2023-03-14

您的代码存在一些问题。

ClientSendMessage()正在使用PrintWriter.println(),它输出换行符。但是您的输入字符串上已经有换行符,因此println()正在发送额外的换行符。此外,换行符println()输出依赖于平台,而FTP专门使用CRLF。所以您根本不应该使用println()

ClientReceiveMessage()不考虑多行响应。根据RFC 959,第4.2节“FTP回复”:

A reply is defined to contain the 3-digit code, followed by Space
<SP>, followed by one line of text (where some maximum line length
has been specified), and terminated by the Telnet end-of-line
code.  There will be cases however, where the text is longer than
a single line.  In these cases the complete text must be bracketed
so the User-process knows when it may stop reading the reply (i.e.
stop processing input on the control connection) and go do other
things.  This requires a special format on the first line to
indicate that more than one line is coming, and another on the
last line to designate it as the last.  At least one of these must
contain the appropriate reply code to indicate the state of the
transaction.  To satisfy all factions, it was decided that both
the first and last line codes should be the same.

   Thus the format for multi-line replies is that the first line
   will begin with the exact required reply code, followed
   immediately by a Hyphen, "-" (also known as Minus), followed by
   text.  The last line will begin with the same code, followed
   immediately by Space <SP>, optionally some text, and the Telnet
   end-of-line code.

      For example:
                          123-First line
                          Second line
                            234 A line beginning with numbers
                          123 The last line

   The user-process then simply needs to search for the second
   occurrence of the same reply code, followed by <SP> (Space), at
   the beginning of a line, and ignore all intermediary lines.  If
   an intermediary line begins with a 3-digit number, the Server
   must pad the front to avoid confusion.

服务器的初始问候语可能是多行的,但对任何命令的任何响应都可能是多行的,因此您需要处理它。

但更重要的是,在进行错误检查时,您只需要查看3位数的响应代码,而不是伴随它的文本。除了几个选择命令,如< code>PASV、< code > MLST /< code > MLSD 等,文本是任意的,服务器可以发送它想要的任何内容。因此,您需要忽略这些文本,除非在实际需要的情况下,或者在向用户报告错误消息时。

尝试类似的操作:

private Socket socket;
private BufferedReader br;

 @Test
public void testProcessPASS() throws IOException{    

    socket = new Socket(server.getAddress(), server.getcmdPort());
    br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    this.ClientReceiveMessage(220);
    this.ClientSendMessage("USER user_test", 331);
    this.ClientSendMessage("PASS pass_test", 230);
    this.ClientSendMessage("QUIT", 221);

    socket.close();

    br = null;
    socket = null;
}

public int ClientSendMessage(String msg, int ExpectedReplyCode) throws IOException{

    Writer bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    bw.write(msg);
    bw.write("\r\n");
    bw.flush();

    return ClientReceiveMessage(ExpectedReplyCode);
}

public int ClientReceiveMessage(int ExpectedReplyCode) throws IOException{

    String line = br.readLine();
    String msgText = msgText.substring(4);

    if ((line.length() >= 4) && (line[3] == '-')) {
        String endStr = line.substring(0, 2) + " ";
        do {
            line = br.readLine();
            msgText += ("\r\n" + line.substring(4));
        }
        while (line.substring(0, 3) != endStr);
    }

    int actualReplyCode = Integer.parseInt(line.substring(0, 2));
    assertEquals("Response error. " + msgText, ExpectedReplyCode, actualReplyCode);

    // TODO: if the caller wants the msgText for any reason,
    // figure out a way to pass it back here...

    return actualReplyCode;
}
 类似资料:
  • 我是一个初学者程序员,所以这很可能是显而易见的,我忽略了答案。但说到这个问题。 我有一个由两部分组成的程序(它比这个例子稍微复杂一点,但情况是一样的)。该程序在客户端和服务器之间触发了多条消息。我在服务器端有一个PrintWriter来向客户机发送消息,在客户机上有一个BufferedReader来读取发送的消息。当这个例子运行时,我得到了两行作为输出。第一个消息是两个消息,第二个消息是NULL。

  • 关于使用Java套接字的例子有上百万个——每个都是一样的!每一个都显示了一个正在创建的客户机套接字,一些文本正在发送,并且套接字已关闭。 我正在编写一些测试代码。我希望我的客户端循环并发送相当多的消息。每次关闭客户端套接字并重新创建似乎很傻,所以我想我只创建一个客户端套接字,循环循环并在同一个套接字上发送数据。问题是——我的服务器套接字不会打印出它收到的内容,直到客户端发送最后一条消息并且客户端套

  • 我有这个tcp插座 接受一条消息,将其转换为字符串并显示它,但是如果连接没有被客户端关闭,我就不能像预期的那样看到服务器上显示的消息 如何在不需要关闭客户端连接的情况下(由客户端)从客户端发送多条消息? 这里是NodeJs中的客户端 谢谢valeriano cossu

  • 我有几个关于Java套接字编程的问题。 > 我有一个进程需要跨多个套接字连接发送一条消息。现在我有如下内容 这是原子能的吗?或者,在并发环境中,它们是否有可能在几乎相同的时间发送? 在并发环境中,套接字连接(设置为PrintWriter)的输出流是否已经互斥?比如说,两个线程想要同时写入PrintWriter。如果没有在PrintWriter上显式互斥,流的任何输出都会被弄乱吗?

  • 我在询问之前搜索了这个问题,但我找不到类似的东西。我开发了一个客户端/服务器解决方案来发送/接收HL7消息。我使用套接字将客户端连接到服务器,从这个连接中,我只能使用OutputSteam对象发送1条HL7消息。我如何在同一个套接字连接中发送多个HL7?我尝试了不同的方法,但它们不能正常工作。 以下是我的客户端代码: 从服务器端 如何在同一套接字连接中发送更多HL7消息?

  • 我一直在开发一个简单的python套接字聊天室,客户端和服务器可以在其中相互发送消息。我遇到的问题是服务器和客户端一次只能发送一条消息。我希望它能像任何其他聊天室一样工作,在那里我可以在发送消息时收到消息,任何帮助都会有很大帮助