Java Server将单词列表发送回客户端

时间:2014-02-26 12:52:44

标签: java sockets

基本上我有一个客户端将一个单词列表发送到服务器。服务器删除重复的单词。我将'remmoved duplicate'字样存储在一个新集合中,但我无法将此数据结构中的新单词发送回客户端。该程序似乎停留在这个循环'while((clientSentence = inFromClient.readLine())!= null)'从服务器类,然后我不能将新的单词集写回客户端。有人可以帮我这个吗?

任何人都可以帮我这个吗

这是服务器代码:

import java.io.*;

import java.net.*;
import java.util.LinkedHashSet;
import java.util.Set;

class Server {

    private static boolean isFinished;

    public static void main(String argv[]) throws Exception

    {

        String clientSentence;

        String capitalizedSentence;

        ServerSocket welcomeSocket = new ServerSocket(6789);

        Set<String> lines = new LinkedHashSet<String>(10000);

        while (true) {

        Socket connectionSocket = welcomeSocket.accept();

        BufferedReader inFromClient = new BufferedReader(new InputStreamReader(
                connectionSocket.getInputStream()));
        DataOutputStream outToClient = new DataOutputStream(
                connectionSocket.getOutputStream());

        while ((clientSentence = inFromClient.readLine()) != null) {

            capitalizedSentence = clientSentence.toUpperCase();

            lines.add(capitalizedSentence);


        }

        for (String string : lines) {
            outToClient.writeBytes(string);

        }

        }
    }
}

客户端

import java.io.*;

import java.net.*;
import java.util.LinkedHashSet;
import java.util.Set;

public class Client {

    public static void main(String argv[]) throws Exception

    {
        String sentence;
        String modifiedSentence;
        File myfile = new File("C:/Users/Tudor/Desktop/test.txt");

        BufferedReader inFromUser = new BufferedReader(new FileReader(myfile));
        Socket clientSocket = new Socket("localhost", 6789);
        DataOutputStream outToServer = new DataOutputStream(
                clientSocket.getOutputStream());

        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));
        Set<String> lines = new LinkedHashSet<String>(10000);

        while ((sentence = inFromUser.readLine()) != null) {
            outToServer.writeBytes(sentence + "\n");

            modifiedSentence = inFromServer.readLine();

            lines.add(modifiedSentence);
            System.out.println("FROM SERVER: " + modifiedSentence);
        }

        // for (String string : lines) {
        // System.out.println(string);
        // }
        clientSocket.close();
    }
}

1 个答案:

答案 0 :(得分:0)

在客户端,您正在阅读BufferedReader#readLine,希望\n来。

但是从服务器,您发送stringwriteBytes(string))而没有\n,因此readLine将如何读取该字符串。

\n附加到您要发送给客户的string

主要问题在于:

在客户端,您希望在将每一行发送到服务器后从服务器获取数据

while ((sentence = inFromUser.readLine()) != null) {
            outToServer.writeBytes(sentence + "\n");

            modifiedSentence = inFromServer.readLine();// here you are expecting the answer from server but server is busy in listening more data 

            lines.add(modifiedSentence);
            System.out.println("FROM SERVER: " + modifiedSentence);
        }

您的服务器仍在忙于收听数据。

while ((clientSentence = inFromClient.readLine()) != null) {

            capitalizedSentence = clientSentence.toUpperCase();

            lines.add(capitalizedSentence);


        }

在服务器端代码中进行一些更改

while ((clientSentence = inFromClient.readLine()) != null) {

            capitalizedSentence = clientSentence.toUpperCase();

           outToClient.writeBytes(capitalizedSentence+"\n");
          // imideately send data after converting it as client is expecting the data to receive and must append '\n'  


        }

另一个解决方案是::

更改客户端代码,如下所示:

while ((sentence = inFromUser.readLine()) != null) {
            outToServer.writeBytes(sentence + "\n");

// modifiedSentence = inFromServer.readLine(); Remove this line from here

//            lines.add(modifiedSentence);
//            System.out.println("FROM SERVER: " + modifiedSentence);
        }

使用while循环从服务器读取数据

while ((modifiedSentence = inFromServer.readLine()) != null) {

       // make 'clientlines'  as a set at client side  and store all lines coming from server       
               clientlines.add(capitalizedSentence);


            }

提示:如果您使用DataOutputStream进行撰写,请使用DataInputStream进行阅读。