简体   繁体   English

通过Tcp发送数据后,字符串比较不起作用

[英]String Compare doesnt work after sending data over Tcp

I have following code. 我有以下代码。 One reads a File called "denglish.txt" splits it into words and sends every 2nd word over Tcp socket connection to a server. 读取一个名为“ denglish.txt”的文件,将其拆分为多个单词,并通过Tcp套接字连接将每个第二个单词发送到服务器。 The Server is now supposed to read this word and compare a to a word from a dictionary. 现在,服务器应读取该单词并将a与字典中的单词进行比较。 Weird thing is the comparison always fails if i send it that way. 奇怪的是,如果我以这种方式发送比较,它总是会失败。 But if the clients sends something like out.Print("make") it works . 但是,如果客户端发送诸如out.Print(“ make”)之类的东西,它将起作用。 I really dont understand why its like this. 我真的不明白为什么会这样。 I can send and print out the word from the client on the Server and he prints out the correct words but the stringcompare fails. 我可以从服务器上的客户端发送和打印单词,他可以打印正确的单词,但是stringcompare失败。 MAybe someone can help me. 也许有人可以帮助我。 THank you guys 感谢大伙们

    Socket socket = new Socket ("localhost" , 47777);
        PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
        //Socket socket1 = new Socket ("localhost" , 47778);
        //PrintWriter out1 = new PrintWriter(socket1.getOutputStream(),true);


            String line;
            BufferedReader text = new BufferedReader(new InputStreamReader(new FileInputStream("denglisch.txt")));
            String text1 = "";


           while((line = text.readLine()) != null) {  // handle lines
                text1 = text1 + line;
                }

           int i = 0;
           //out.println("make");
           StringTokenizer tokenizer = new StringTokenizer( text1,",.\" : !" );
           while ( tokenizer.hasMoreTokens() )
           {
                if (i % 2 == 0)
                    {

                    out.println(tokenizer.nextToken().toLowerCase().toString());
                    out.flush();
                    }
                if (i % 2 ==1 )
                    {
                    //out1.println(tokenizer.nextToken());
                    }
                i = i+1;


Server CODE
    //Server Codepublic




<pre>   filteritserver(BufferedReader dict, String plusminus){
    this.dict = dict;   
    this.port = 47777;
    this.plusminus = plusminus;
    }
      public void run() { 

        ServerSocket server;

        boolean isopen = false;
        while (isopen == false){
        try {
            System.out.println(port);
            server = new ServerSocket(port);
            Socket cSocket = server.accept();
            msg = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));
            isopen = true;

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            System.out.println("Port is already in used trying next one");
            port = port +1;

            }
        }
        System.err.println(port);
        int i = 0;
        String s = "word";
      try {
        while ((word = msg.readLine())!= null)
         // while ( i < 3)
          {
          //System.out.println("prozess started und das Vorzeichen ist "+plusminus);
         // System.out.println("das Wort "+ word +" ist hier angekommen");
           try { 
           while(true) { // blunt sequential dictionary search


               String line = dict.readLine();
               //System.out.println(line);
               if(line == null){
                   break;  
               }
               else if(word.compareTo(line) < 0)
               {
                 if (plusminus.equals("-"))
                         {
                     System.out.println("Dieses Wort kommt im Dictionary nicht vor " + word);
                         }

               }
               else if(word.compareTo(line)== 0 ){
                 if (plusminus.equals("+")){
                     System.out.println("Dieses Wort kommt im Dictionary 1234 vor " + word);
                 }
                 break;

               }

               else /* word.compareTo(line) > 0 */ continue;  }
           } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           finally{
           }

           }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

text1 = text1 + line; text1 = text1 +行; You don't use a space or a delimiter in here. 您在此处不要使用空格或定界符。 Can that be the problem? 这可能是问题吗? )(What I mean is that you print several words, like CatHouseDogFood which isn't a word in the dictionary). )(我的意思是您要打印几个单词,例如CatHouseDogFood,它在词典中不是一个单词)。 It would make sense why it works when you only print one word (ie "make") 当您仅打印一个单词(即“ make”)时,为什么它会起作用是很有意义的

What do you get if you print the compareTo result at server side? 如果在服务器端打印compareTo结果,会得到什么?

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

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