缓存读者挂在Java中

时间:2014-12-08 23:51:43

标签: java client bufferedreader server printwriter

我的任务是为我的一个编程任务创建一个简单的Java服务器。这个,我没有遇到麻烦。服务器本质上是一个存储有关音乐数据的点唱机。 (它实际上并不存储任何音乐用于分配)客户端应该能够与此服务器交互以添加歌曲,查看歌曲列表,并增加歌曲流行度。执行其中一项操作后,客户端将断开连接。这个,我没有遇到任何问题。我制作了一个带套接字和服务器套接字的简单多线程服务器。我在telnet上测试了它,所有所需的功能都完美无缺。我的问题来自于我们必须做的一些测试。为了正式测试服务器,我们要创建一个只创建一堆客户端套接字并让它们与服务器交互的java类。这是一个例子。

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");

    }
}
}

据我所知,第一个客户端成功完成其交互并退出。然后,第二个客户端连接,但程序然后挂起。该程序应打印披头士/ nYellow潜艇/ n0。我猜这与我的BufferedReader的while循环有关,但老实说我不知道​​,我不是最了解我学到的JAVA IO。如果有人能帮助我推断导致挂起的原因以及为什么没有打印出来,我将不胜感激。作为参考,这里是服务器程序的片段,显示服务器处理客户端的操作。

 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");
            }
        }

来自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;

}

1 个答案:

答案 0 :(得分:0)

尝试在out.flush();之后添加out.println("2");