如何从 java 服务器中的客户端获取用户名
How can I get a username from the clients in java server
我正在开发一个 android 应用程序,用户必须先输入他的用户名并单击 "connect" 然后服务器有一个在线用户用户名的 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);
}
}
}
你应该添加一些协议来处理客户端,即使是一个非常简单的协议,因为你同时编写客户端和服务器,所以这应该不是问题。
在服务器线程中应该是这样的:
while (true) {
//reading messages
String message = din.readUTF();
String action=Protocol.getRespone(message);
此位将从客户端获取消息,
并说,例如,如果它的格式为 &login:username ,它将根据服务器数据库检查它,对其进行必要的修改,并且 return "User name already in use" 如果在用户中,否则 "User %username% connected succesfuly"。如果消息在 &message: "bla bla bla" 中,它将正常运行,例如 return 随心所欲。
我正在开发一个 android 应用程序,用户必须先输入他的用户名并单击 "connect" 然后服务器有一个在线用户用户名的 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);
}
}
}
你应该添加一些协议来处理客户端,即使是一个非常简单的协议,因为你同时编写客户端和服务器,所以这应该不是问题。
在服务器线程中应该是这样的:
while (true) {
//reading messages
String message = din.readUTF();
String action=Protocol.getRespone(message);
此位将从客户端获取消息, 并说,例如,如果它的格式为 &login:username ,它将根据服务器数据库检查它,对其进行必要的修改,并且 return "User name already in use" 如果在用户中,否则 "User %username% connected succesfuly"。如果消息在 &message: "bla bla bla" 中,它将正常运行,例如 return 随心所欲。