2011-03-16 44 views
5

我目前正在使用java爲桌面應用程序創建身份驗證服務器,到目前爲止,我已經能夠使客戶端和服務器像聊天服務器/客戶端一樣進行通信。套接字認證服務器?

我意識到我只有一小部分工作,有很多事情要學習,但現在在這個階段,我想知道如何完成身份驗證。

例如,這是服務器代碼:

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class LoginServer 
{ 
    public static void main(String[] args) throws Exception { 
     int port = 7000; 
     int id = 1; 

     ServerSocket loginserver = null; 
     try 
     { 
      loginserver = new ServerSocket(port); 
      System.out.println("LoginServer started..."); 
     } 
     catch (IOException e) { 
      System.out.println("Could not listen on port: " + port); 
      System.exit(-1); 
     } 

     while (true) 
     { 
      Socket clientSocket = loginserver.accept(); 
      ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++); 
      cliThread.start(); 
     } 
    } 
} 

class ClientServiceThread extends Thread 
{ 
    Socket clientSocket; 
    int clientID = -1; 
    boolean running = true; 

    ClientServiceThread(Socket s, int i) 
    { 
     clientSocket = s; 
     clientID = i; 
    } 

    public void run() 
    { 
     System.out.println("Accepted Client : ID - " + clientID + " : Address - " + clientSocket.getInetAddress().getHostName()); 
     try 
     { 
      BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
      PrintWriter out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream())); 
      while (running) 
      { 
       String clientCommand = in.readLine(); 
       System.out.println("Client Says :" + clientCommand); 
       if (clientCommand.equalsIgnoreCase("quit")) 
       { 
        running = false; 
        System.out.print("Stopping client thread for client : " + clientID); 
       } 
       else 
       { 
        out.println(clientCommand); 
        out.flush(); 
       } 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

正如你所看到的,我做閱讀客戶端發送的內容,並將其輸出到控制檯以及檢查,如果客戶端發送一個退出或不是命令。

  • 什麼我不知道是我會怎麼做 ,以便客戶端只能連接 到它,如果用戶名和密碼被髮送 ?

  • 或者我應該永遠得到 初始連接,並從那裏 接收認證 信息,如果客戶沒有 送了它在讓我們說3秒 斷開呢?

  • 什麼是正確的接收方式 新的連接和驗證 的用戶呢?

回答

10

你有正確的想法,但你需要更多地把它想象成一個狀態機。

客戶端連接,那麼它需要進入登錄狀態,您希望它發送身份驗證信息。如果不正確或超時,斷開連接。

如果正確,請轉到命令處理狀態。如果您收到退出命令,請移至清除狀態,或者直接斷開連接。

這裏有一個大致輪廓:

String line; 
while ((line = in.readLine()) != null) { 
    switch (state) { 
     case login: 
      processLogin(line); 
      break; 
     case command: 
      processCommand(line); 
      break; 
    } 
} 

processLogin功能看起來是這樣的:

void processLogin(String line) { 
    if (user == null) { 
     user = line; 
    } 
    else if (password == null) { 
     password = line; 
    } 
    else { 
     if (validUser(user, password)) { 
      state = command; 
     } 
     else { 
      socket.close(); 
     } 
    } 
} 

而且你processCommand功能:

void processCommand(String line) { 
    if (line.equals(...)) { 
     // Process each command like this 
    } 
    else if (line.equals("quit")) { 
     socket.close(); 
    } 
    else { 
     System.err.println("Unrecognized command: " + line); 
    } 
} 

state實例變量可能會是一個枚舉。你可以看到使用這個模型會給你一些非常簡單的可擴展性。例如,一條命令可能會讓你進入一個不同的狀態來處理特定的子命令。

+0

感謝您的回答我很高興我沒有太離譜,我也嘗試過尋找任何更直接到認證套接字系統的源代碼或例子,但沒有找到任何作爲例子,並幫助我在這,你會碰巧能夠給我一個簡短的代碼示例,或許可以將我引導到任何可能值得檢查的狀態機的登錄系統或類似的可能幫助他們的鏈接?再次感謝你 – Guapo 2011-03-16 20:55:26

+0

真的很感謝花時間,這真的幫助我;) – Guapo 2011-03-17 19:52:38