如何从java服务器中的客户端获取用户名

时间:2015-03-06 17:59:16

标签: java server

我正在开发一个Android应用程序,用户必须首先输入他的用户名并点击" connect"然后服务器有一个在线用户的ArrayList'用户名,它必须检查此用户名之前是否已被使用,如果不是,它接受连接并将用户名添加到arraylist,否则它会发送错误消息。我不知道如何连接代码。 这是服务器端:

public class server {
private boolean usernameb;
private ArrayList<String> users;
private Hashtable outputStreams = new Hashtable();
public server (int port) throws IOException {
    listen (port);      
}
static public void main( String args[] ) throws Exception {
    //to get the port from the command line
    int port = Integer.parseInt( args[0] );
    //creating new object to accept connections
    new server (port); 
}
//method to check for duplicates username
private boolean dupl (String username) {
    int i = 0;
    for (i = 0; i < users.size(); i++) {
        String temp = users.get(i);
        if (temp.equals(username)){
            this.usernameb = false;
            break;
        }
        else {
            this.usernameb = true;
        }
    }
    return usernameb;
}
private void listen(int port) throws IOException {
    ServerSocket serverS = new ServerSocket (port);
    System.out.println( "Server is ready on : "+ serverS);

    while (true) {
    //Getting next connection
    Socket clientS = serverS.accept();
    System.out.println( "Connection has been accepted from " + clientS );
    //to write data to the other side
    DataOutputStream dout = new DataOutputStream(clientS.getOutputStream());
    outputStreams.put(clientS, dout);
    //creating a new thread for this connection
    new serverThread( this, clientS);
    }
}
void removeConnection(Socket socket) {
    synchronized (outputStreams) {
        System.out.println("Removing connection of :  " + socket);
        outputStreams.remove(socket);
        try {
            socket.close();
        } catch (IOException ie) {
            System.out.println("Error closing : " + socket);
            ie.printStackTrace();
        }
    }
}
Enumeration getOutputStreams() {
    return outputStreams.elements();
    }
void sendToAll(String message) {
    synchronized (outputStreams) {
        for (Enumeration e = getOutputStreams(); e.hasMoreElements();) {

            DataOutputStream dout = (DataOutputStream) e.nextElement();
            try {
                dout.writeUTF(message);
            } catch (IOException ie) {
                System.out.println(ie);
            }
        }
    }
}
}

这是服务器线程代码

public class serverThread extends Thread {
private Socket socket;
private server server;
public serverThread (server server, Socket socket) {
    this.server = server;
    this.socket = socket;
    //start the thread
    start();
}
public void run() {
    try {
        //creating DateInputStream to recieve what the client types
        DataInputStream din = new DataInputStream(socket.getInputStream());
        while (true) {
            //reading messages
            String message = din.readUTF();
            //printing the message
            System.out.println( "Sending " + message);
            //send the message to other clients
            server.sendToAll(message);
        }
        }
    catch(EOFException ie) {

        } 
    catch( IOException ie ) {
        ie.printStackTrace();
        } 
    finally {
        //closing connection
        server.removeConnection(socket);
    }
}
}

1 个答案:

答案 0 :(得分:0)

您应该添加一些协议来处理客户端,即使是非常简单的协议,因为您正在编写客户端和服务器,这应该不是问题。

服务器线程中的

应该是这样的:

        while (true) {
        //reading messages
        String message = din.readUTF();
        String action=Protocol.getRespone(message); 

此位将从客户端获取消息, 并且,例如,如果它的格式为&amp; login:username,它将根据服务器数据库进行检查,对它进行neccisery修改,并返回&#34;用户名已经在使用&#34; if in user,else&#34; User%username%connected succesfuly&#34;。如果消息在&amp; message中:&#34; bla bla bla&#34;它会正常行动,例如返回你想要的东西。