2

我的程序已设置并准备就绪,问题出在服务器内。当我的客户发送消息时,它应该存储在哪里?我尝试使用队列但没有用,也尝试使用通常的字符串寄存器,但它部分工作。我使用“读/写 UTF”创建了一个用于发送的线程和一个用于存储的线程。如果有人为我提供算法,或者至少是一个更好的主意,我将不胜感激。代码 :

class clientThread extends Thread {
    DataInputStream fromClient;
    int counter = 0;

    public clientThread(Socket cs) throws IOException
    {
        fromClient = new DataInputStream(cs.getInputStream());
    }

    public void run()
    {
        while (true)
        {
            try {
                toall=Integer.toString(counter)+fromClient.readUTF();
            } catch (IOException ex) {
                Logger.getLogger(ChatTerminalS.class.getName()).log(Level.SEVERE, null, ex);
            }
            counter++;
        }
    }
}

class SendingThread extends Thread
{
    DataOutputStream toClient;
    String s = "";int counter=0;

    public SendingThread(Socket cs) throws IOException
    {
        toClient = new DataOutputStream(cs.getOutputStream());
    }

    public void run()
    {
        while (true)
        {
            if(toall.charAt(0)+""==Integer.toString(counter))
            {}
            else
            {
                try {
                    toClient.writeUTF(toall);
                } catch (IOException ex) {
            Logger.getLogger(ChatTerminalS.class.getName()).log(Level.SEVERE, null, ex);
            }
        counter++;
        }
    }
}
4

0 回答 0