안녕하세요. 자바 독학중인 초보자입니다.
현재 자바채팅서버와 안드로이드클라이언트를 구현중인데.
닉네임 중복체크하는 부분이 없어서
동일한 닉네임이 들어오면 접속해있던 닉네임은 어플이 종료되버리는 현상이있습니다.
우선 소스로 설명해드리겠습니다.
public class ChatServer{
public static void main(String[] args) throws IOException
{
ServerSocket serverSoc = null;
try
{
serverSoc = new ServerSocket(3333);
System.out.println("서버가 시작되었습니다.");
HashMap hm = new HashMap();
while(true)
{
Socket sock = serverSoc.accept();
ChatThread chatthread = new ChatThread(sock,hm);
chatthread.start();
}
}
catch (Exception ex)
{
System.out.println(ex);
}
}
}
class ChatThread extends Thread
{
private Socket socket;
private String id;
private BufferedReader br;
private PrintWriter pw;
private HashMap hm;
private boolean initFlag = false;
public ChatThread(Socket socket, HashMap hm)
{
this.socket = socket;
this.hm = hm;
try
{
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
id=br.readLine();
System.out.println("사용자["+ id +"]님이 접속하셨습니다.");
클라이언트에서 접속한후 입력한 문자를 닉네임으로 설정합니다
여기서 접속전에 동일 닉네임이 있나를 비교하고싶습니다
synchronized(hm)
{
hm.put(this.id,pw);
}
}
initFlag = true;
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public void run()
{
try
{
String line = null;
while((line = br.readLine()) != null)
{
}
else{
broadcast(id + " : " +line);
System.out.println(id + " : " +line);
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
synchronized(hm)
{
hm.remove(id);
}
try
{
if(socket!=null)
socket.close();
}
catch (Exception ex)
{
}
}
}
}
현 상황에서 할수있는방법이 있을까요?
간단히 중복체크만하고 동일닉네임이 접속시 접속차단정도만 할수있으면
될것같은데... 방법좀알려주시면 감사하겠습니다
아니면 관련키워드라도 알려주시면 감사하겠습니다!!