聊天服务器吐出内存地址。不知道为什么

时间:2013-10-20 23:15:29

标签: java client-server chat

我目前正在开发一个线程服务器,允许多个客户端通过“/ username message”格式连接和发送消息。服务器运行,我的客户端类能够成功连接并被要求输入用户名,但是一旦我尝试发送消息而不是接收输出“To [username]:message”,我收到“To:[memory address]: [记忆地址]“我很困惑,因为我没有尝试打印任何物体。

此外,客户端上不显示任何应该位于邮件另一端的内容。我处理客户端线程的方式是使用ArrayList,它们在创建和启动后添加到它们。我尝试将消息发送到另一端的客户端的方式是使用服务器类中的方法迭代客户端线程的ArrayList,并将消息输出到具有相应名称的消息。

提前:我知道我的parseUserName和parseMessage方法不太好,但我选择让他们坐下来,直到我能处理更大的问题。作为参考,这是我的服务器类。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author
 */
public class ThreadedChatServer 
{


    //private ServerSocket server = new ServerSocket(5679);
    private ServerSocket server;
    ArrayList<ClientThread> clientThreads;

    public ThreadedChatServer(ServerSocket s) 
    {
        server = s;
        clientThreads = new ArrayList<ClientThread>();
    }


    public void openServer() throws IOException 
    {
        while(true) 
        {
            Socket client = server.accept();
            System.out.println("The server is connected to " + client.getInetAddress());

            // starts a thread for this client
            ClientThread c = new ClientThread(client, this);
            c.start();
            clientThreads.add(c);
        }
    }

    // Iterates through the clientThreads ArrayList and prints the given message
    // to the client whose name matches the "to" parameter.
    public void sendMessage(String from, String to, String m) 
    {
        for (int i = 0; i < clientThreads.size(); i++) 
        {
            if (clientThreads.get(i).getUserName() != from &&
                    clientThreads.get(i).getUserName() == to) 
            {
                clientThreads.get(i).toClient.println(m);
            }
        }
    }

    public static void main(String[] a) throws IOException 
    {
        new ThreadedChatServer(new ServerSocket(5679)).openServer();
    }

    public class ClientThread extends Thread
{
    private Socket s;
    private String name;
    private BufferedReader fromClient;
    private PrintWriter toClient;
    private ThreadedChatServer server;

    public ClientThread(Socket c, ThreadedChatServer tc) throws IOException 
    {
        s = c;
        name = null;
        fromClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
        toClient = new PrintWriter(s.getOutputStream(), true);
        server = tc;
    }

    public void run() 
    {
        String s = null;
        int size = 0;
        char[] c = null;





        try 
        {
            toClient.println("Enter a username: ");
            s = fromClient.readLine();
            name = s;

            // Accept/send messages from the user
            while ((s = fromClient.readLine()) != null) 
            {
                size = s.length();
                c = new char[size];
                for (int i = 0; i < size; i++) 
                {
                    c[i] = s.charAt(size - i - 1);
                }
                String output = c.toString();
                String s2 = "To " + parseUserName(output) + ": "
                        + parseMessage(output);
                toClient.println(s2);
                server.sendMessage(this.name, parseUserName(output), 
                        parseMessage(output));

            }

            // Close the connection 
            fromClient.close();
            toClient.close();
            this.s.close();
            clientThreads.remove(c);
        } catch (IOException e) {e.printStackTrace();}
    }

    public String getUserName() {return name;}

    public String parseUserName(String s) 
    {
        Scanner in = new Scanner(s);
        String temp = in.next();
        if (temp.charAt(0) == '/') 
        {
            temp = temp.substring(1, temp.length());
            return temp;
        }

        return temp;

    }

    public String parseMessage(String s) 
    {
        Scanner in = new Scanner(s);
        String temp = in.next();
        Boolean firstSpaceCheck = false;
        for (int i = 0; i < temp.length(); i++) 
        {
            if (temp.charAt(i) == ' ')
            {
                temp = temp.substring(i + 1, temp.length());
                firstSpaceCheck = true;
            }
        }
        return temp;
    }
}
}

1 个答案:

答案 0 :(得分:0)

它不是内存地址,而是来自Object#toString的输出格式

getClass().getName() + '@' + Integer.toHexString(hashCode())

在这种情况下,它看起来像[C@15b7986,它告诉您将字符数组Object的输出发送回客户端,因此输出是Object#toString返回的输出。取代

String output = c.toString();

String output = new String(c);