如何使Java TCP服务器和客户端应用程序循环,直到发出一定数量的请求

问题描述 投票:0回答:1

我在eclipse中编写了TCP服务器和TCP客户端应用程序。客户端以字符串的形式获取用户输入,然后将其发送给将其大写并将其发回的服务器。他们需要保持循环,直到服务器收到一定数量的请求,在这种情况下,它会关闭套接字和停止之间的连接。不幸的是,它似乎只循环一次。我将在代码之后提供我正在讨论的示例输出。

import java.io.*;
import java.net.*;


public class TCPServer {

public static void main(String argv[]) throws Exception {
    String clientSentence;
    String capitalizedSentence;
    ServerSocket welcomeSocket = new ServerSocket(6789);
    int requests = 0;
    while (true) {
        Socket connectionSocket = welcomeSocket.accept();
        BufferedReader inFromClient = new BufferedReader (new InputStreamReader( connectionSocket.getInputStream()));
        DataOutputStream outToClient = new DataOutputStream (connectionSocket.getOutputStream());
        do {

            clientSentence = inFromClient.readLine();
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            outToClient.writeBytes(capitalizedSentence);
            outToClient.flush();
            requests++;
        }
        while(requests < 10);
        outToClient.writeBytes("REQUEST LIMIT REACHED");

        }

}

}

和客户

import java.io.*;
import java.net.*;

public class TCPClient {

public static void main(String argv[]) throws Exception {
    String sentence;
    String modifiedSentence;
    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
    //Creates socket, replace the hostnmae/ip address with the ipaddress of the computer running the server application.
    Socket clientSocket = new Socket("10.69.88.130", 6789);
    DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    BufferedReader inFromServer = new BufferedReader(new InputStreamReader (clientSocket.getInputStream()));
    do {
        System.out.print("TO SERVER: ");
        sentence = inFromUser.readLine();
        outToServer.writeBytes(sentence + '\n');
        outToServer.flush();
        modifiedSentence = inFromServer.readLine();
        System.out.println("FROM SERVER: " + modifiedSentence);
    }
    while (!modifiedSentence.equals("REQUEST LIMIT REACHED"));
    System.out.println(modifiedSentence);
    clientSocket.close();
    }

}

最后我得到了输出

TO SERVER: testa
FROM SERVER: TESTA
TO SERVER: testb
(nothing else is displayed after this line)
java tcp
1个回答
0
投票

那么你的代码是有效的(有点)。您运行成功,然后重新启动客户端而不重新启动服务器。您的request值不会重置为0,重新启动客户端后,客户端的第一个请求是服务器的第10个请求。那么为什么要求通过呢?因为您首先冲洗字符串然后检查您的计数器。

我稍微修改了你的服务器。也许那就是你要找的?

import java.io.*;
import java.net.*;


public class TCPServer {

    public static void main(String argv[]) throws Exception {
        String clientSentence;
        String capitalizedSentence;
        ServerSocket welcomeSocket = new ServerSocket(6789);
        int requests = 0;
        while (true) {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient = new BufferedReader (new InputStreamReader( connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream (connectionSocket.getOutputStream());
            while (requests < 10) {

                clientSentence = inFromClient.readLine();
                capitalizedSentence = clientSentence.toUpperCase();
                outToClient.writeBytes(capitalizedSentence + " Request: " + requests +  '\n');
                outToClient.flush();
                requests++;
            }
            outToClient.writeBytes("REQUEST LIMIT REACHED");
            requests = 0;
        }

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.