简体   繁体   English

挂在Java中的缓冲读取器

[英]Buffered Reader Hanging in Java

I have been tasked with making a simple Java server for one of my programming assignments. 我的任务是为我的一项编程任务制作一个简单的Java服务器。 This, I had no trouble with. 这,我没有问题。 The server is essentially a jukebox that stores data about music. 该服务器本质上是一个自动存储塔,用于存储有关音乐的数据。 (It doesn't actually store any music for the purpose of the assignment) Clients should be able to interact with this server to add songs, see the list of songs, and increment a songs popularity. (出于分配目的,它实际上并不存储任何音乐)客户端应该能够与该服务器进行交互以添加歌曲,查看歌曲列表并增加歌曲的流行度。 After doing one of these things, the client is disconnected. 完成这些操作之一后,客户端将断开连接。 This, I had no problem implementing. 这一点,我实现起来没有问题。 I made a simple multi-threaded server with sockets and server sockets. 我用套接字和服务器套接字制作了一个简单的多线程服务器。 I had tested it in telnet and all of the desired functionality worked perfectly. 我已经在telnet中对其进行了测试,并且所有所需的功能都能完美运行。 My problem arises from some testing we have to do. 我的问题来自我们必须进行的一些测试。 In order to officially test the server, we are to make a java class that just creates a bunch of client sockets and has them interact with the server. 为了正式测试服务器,我们将创建一个Java类,该类仅创建一堆客户端套接字并使它们与服务器交互。 This is an example. 这是一个例子。

public class LikeTunesServerTest    {

public static void main(String[] args)  {

    try {

        Socket client = new Socket("localhost", 12010);
        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
        out.println("0");  // add tune
        out.println("The Beatles");
        out.println("Yellow Submarine");
        out.flush();
        client.close();

        client = new Socket("localhost", 12010);
        in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
        out.println("2"); // request all tunes in alphabetical order
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        client.close();

    }

    catch (IOException e)   {

        System.out.println("e");

    }
}
}

As far as I am aware, the first client successfully completes its interaction and exits out. 据我所知,第一个客户端成功完成了其交互并退出。 Then, the second client connects but the program then just hangs. 然后,第二个客户端连接,但是程序随后挂起。 The program should print out The Beatles/nYellow Submarine/n0 . 该程序应打印出甲壳虫乐队/ nYellow Submarine / n0。 I am guessing this has to do with my while loop for the BufferedReader, but honestly I have no idea, I am not the best with the JAVA IO I learned. 我猜想这与我的BufferedReader的while循环有关,但是老实说,我不知道,对于我学到的JAVA IO来说,我不是最好的。 If anyone could help me deduce what is causing the hang and why nothing gets printed out, I would be grateful. 如果有人可以帮助我推断是什么原因导致挂起,以及为什么什么都没打印出来,我将不胜感激。 For reference, here is a snippet of the server program that shows what the server does to handle the clients. 作为参考,这是服务器程序的片段,显示了服务器如何处理客户端。

 try    {

        // Open I/O

        in = new BufferedReader(new InputStreamReader(incomingConnection.getInputStream()));
        out = new PrintStream(incomingConnection.getOutputStream());

        // Ask for input (For Telnet Testing)

        //out.println("Welcome to the LikeTunes Server!.");
        //out.println("Type 0 to add a tune to the list.");
        //out.println("Type 1 to like an existing Tune.");
        //out.println("Type 2 to view the list of Tunes in alphabetical order.");
        //out.println("Type 3 to view the list of Tunes in order of popularity.");
        try {

            final int temp = Integer.parseInt(in.readLine());

            // Add a Tune to the TuneLst

            if (temp == 0)  {

                // Get Tune to be added
                // TuneList method is synchronised to prevent duplicates

                //out.println("Please input the name of the artist.");
                String artistName = new String(in.readLine());
                //out.println("Please input the name of the Tune.");
                String title = new String(in.readLine());

                // If the client leaves an artist's name or title blank

                if (artistName.equals("") || title.equals(""))  {

                    out.println("5");
                }

                // Otherwise, add the Tune

                else    {

                    successCheck = LikeTunesServer.tl.addTune(artistName,title);

                    // Tune was added successfully 

                    if (successCheck == 1)
                    out.println("Request Completed, closing server");

                    // Tune was a duplicate

                    else
                    out.println("Tune was a duplicate, closing server");
                }
            }

            // Like a Tune on the TuneList

            else if (temp == 1) {

                // Get Tune to be liked
                // TuneList method is synchronised to prevent incorrect Tune likes

                //out.println("Please input the name of the artist.");
                String artistName = new String(in.readLine());
                //out.println("Please input the name of the Tune.");
                String title = new String(in.readLine());

                // If the client leaves an artist's name or title blank

                if (artistName.equals("") || title.equals(""))  {

                    out.println("5");
                }

                // Otherwise, like the Tune

                else    {

                    successCheck = LikeTunesServer.tl.likeTune(artistName,title);

                    // Tune was liked successfully

                    if (successCheck == 1)
                    out.println("Request Completed, closing server");

                    // Tune was not there to like

                    else
                    out.println("Could not like Tune, it does not exist on the server yet!");
                }
            }

            // List Tunes Alphabetically

            else if (temp == 2) {

                String result = new String(LikeTunesServer.tl.listAlphabetically());
                out.println(result);
                out.println("Request Completed, closing server");
            }

            // List Tunes by Popularity

            else if (temp == 3) {

                String result = new String(LikeTunesServer.tl.listByLikes());
                out.println(result);
                out.println("Request Completed, closing server");
            }

            // Client made an unrecognised request

            else    {

                out.println("7");
            }
        }

Methods from Tunelist.java Tunelist.java中的方法

    public String listAlphabetically()  {

    String result = new String();
    Tune currentTune = theListAlph;

    // Loops through the alphabetical list concatenating the Tune's info to the result string

    for (int i = 0; i < length(); i++)  {

        if  (i == 0)    {

            result = currentTune.returnTune();

        }

        else    {

            result = result.concat(currentTune.returnTune());

        }

        currentTune = currentTune.nextAlph;

    }

    return result;

}



 public synchronized int addTune(String artistName, String title)   {

    // If the list is empty, add the first Tune

    int temp = 0;
    if  (theListAlph == null)   {

        Tune newNode = new Tune(artistName, title);
        theListAlph = newNode;
        theListPop = newNode;
        temp = 1;

    }

    // Otherwise, begin searching this List for the new Tune's correct lexicographical position in the Linked List

    else    {

        Tune currentTune = theListAlph;
        while (temp == 0)   {

            String testArtist = currentTune.artistName;
            String newArtist = artistName;
            int compare = newArtist.compareTo(testArtist);

            // This artist of the new Tune comes before the tested artist alphabetically

            if (compare < 0)    {

                Tune newNode = new Tune(artistName, title);

                // If the tested Tune was the first Tune in the alphabetical List, insert the new Tune as the new beginning of the alphabetical Linked List

                if (theListAlph == currentTune) {

                    insertNewFirst(newNode, currentTune, theListPop);

                }

                // Inserting the new Tune in all other locations in the alphabetical Linked List

                else    {

                    insertBeforeNotFirst(newNode, currentTune, theListPop);

                }

                temp = 1;

            }

            // The new artist is the same as the tested artist, begin testing the titles of the Tunes lexicographically 

            if (compare == 0)   {

                String testTitle = currentTune.title;
                String newTitle = title;
                int compareTitle = newTitle.compareTo(testTitle);

                // The new title comes before the tested title lexicographically

                if  (compareTitle < 0)  {

                    Tune newNode = new Tune(artistName, title);

                    // If the tested Tune was the first Tune in the alphabetical List, insert the new Tune as the new beginning of the alphabetical Linked List

                    if  (theListAlph == currentTune)    {

                        insertNewFirst(newNode, currentTune, theListPop);

                    }

                    // Inserting the new Tune in all other locations in the alphabetical Linked List

                    else    {

                        insertBeforeNotFirst(newNode, currentTune, theListPop);

                    }

                    temp = 1;

                }

                //The titles are the same
                //The same tune cannot be entered twice

                if  (compareTitle == 0) {

                    temp = 2;

                }

            }       

            // The new tune comes last alphabetically, it will be added to the end of the List

            if  (currentTune.nextAlph == null && temp == 0) {

                Tune newNode = new Tune(artistName, title);
                currentTune.nextAlph = newNode;
                newNode.prevAlph = currentTune;
                currentTune = theListPop;
                while   (currentTune.nextPop != null)   {

                    currentTune = currentTune.nextPop;

                }

                currentTune.nextPop = newNode;
                newNode.prevPop = currentTune;
                temp = 1;

            }

            currentTune = currentTune.nextAlph;

        }

    }

    return temp;

}

Try adding out.flush(); 尝试添加out.flush(); after out.println("2"); out.println("2"); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM