java - 无法解析服务器输入流

标签 java eclipse encryption tcp compression

对于一个项目,我必须编写一个多线程客户端-服务器实现 使用加密和压缩的消息服务。我正在服务器文件中编码 run 方法,当我到达以下几行时遇到错误:

CompressedMessage compmsg = (CompressedMessage)serverInputStream.readObject(); - 这里我在 serverInputStream 下收到一条错误消息

serverInputStream cannot be resolved

以及在每个带有消息的 fromClient

fromClient cannot be resolved

谁能解释一下导致这些错误的原因以及如何解决这些错误? 这是程序的一部分

    public void run()
    {   try
        {   //send greeting to this client
            threadOutputStream.writeObject(getCompressedMessage("Welcome to the chat server " + clientName));
            // inform this client of other clients online
            threadOutputStream.writeObject(getCompressedMessage("online" + getChatClients()));
            // output to server window
            addOutput(clientName + " known as " + chatName + " has joined");
            // inform other clients that this client has joined
            sendMessage("join" + chatName);

            boolean quit = false, broadcast = false;
            // this loop will continue until the client quits the chat service
            while(!quit)
            {   // read next compressed message from client
                CompressedMessage compmsg = (CompressedMessage)serverInputStream.readObject();
                // decompress message
                compmsg.decompress();
                // retrieve decompressed message
                compmsg.getMessage();
                // find position of separating character
                int foundPos = fromClient.indexOf('#');
                // list of recipients for message
                String sendTo = fromClient.substring(0,foundPos);
                // message to be sent to recipients
                String message = fromClient.substring(foundPos+1);

                // if the message is "quit" then this client wishes to leave the chat service
                if(message.equals("quit"))
                {   // add message to server output area
                    addOutput(clientName + " has " + message);
                    // inform other clients that this client has quit
                    sendMessage("quit" + chatName);
                    //send "goodbye" message to this client
                    threadOutputStream.writeObject("Goodbye");
                    // remove this client from the list of clients
                    remove(chatName);
                }
                else
                {   // add message to server output area
                    addOutput(clientName + ">> " + message);
                    // split string to separate recipients names
                    String[] recipients = sendTo.split(",\\s*");
                    // sort this array to use binarySearch
                    Arrays.sort(recipients);
                    // identify if this message is to be sent to all other clients
                    foundPos = Arrays.binarySearch(recipients, chattag[chattag.length-1]);
                    if(foundPos >= 0)
                       // send this message to all other clients
                        sendMessage(chatName + ">> " + message);
                    else
                        // send this message to all clients in recipients array
                        sendMessage(chatName + ">> " + message, recipients);
                }
            } // end while

            // close input stream
            threadInputStream.close();
            // close output stream
            threadOutputStream.close();
        }
        catch(IOException e) // thrown by method readObject, writeObject, close
        {   System.out.println(e);
            System.exit(1);
        }
        catch(ClassNotFoundException e) // thrown by method readObject
        {   System.out.println(e);
            System.exit(1);
        }
    }

serverInputStream 在名为 void getClients() 的程序上方的方法中声明,如下所示 ObjectInputStream serverInputStream = new ObjectInputStream(client.getInputStream()); ObjectOutputStream serverOutputStream = new ObjectOutputStream(client.getOutputStream()); 并且 from client 没有在任何地方声明,它位于提供的框架代码中,并且仅出现在该代码段中

最佳答案

您不能只在代码中的任何位置使用变量。使用变量时它必须在作用域内。

因此,如果serverInputStream在方法getClients()中声明,则只能在该方法中使用。考虑在更大的范围内声明变量,例如成员变量。浏览Java Classes and Objects tutorial.

关于java - 无法解析服务器输入流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20456454/

相关文章:

具有 base 64 + hMac 编码的 iOS 应用程序

linux - 使用 Authbind 在 Ubuntu 的 80 端口从 Eclipse 启动 Tomcat

android - 如何在 Eclipse 中使用 Proguard 混淆 Android 库(.jar 文件)

java - 在java中将KMS CipherText blob解密为纯文本

linux - 无法在 : 中找到 Javac 编译器

android - 如何在 Eclipse 中使用 pocketsphinx 制作语音识别应用程序?

ssl - 如何获取 Diffie–Hellman key 交换参数?

java - Hadoop映射器构造函数,何时以及如何?

java - 如何将http响应放入Set中

java - 可变参数和无参数方法