안드로이드

채팅 클라이언트 프로그램

raulyo 2012. 2. 23. 14:57
Socket sock = null;
        BufferedReader br = null;
        PrintWriter pw = null;
        boolean endflag = false;
        
        try{
        sock = new Socket("ip주소",6000);
        pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
        br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
        BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
       
        pw.println("id");
        pw.flush();
       
        InputThread it = new InputThread(sock,br);
        it.start();
        String line = null;
        while((line = keyboard.readLine())!=null){
        pw.println(line);
        pw.flush();
        if(line.equals("/quit")){
        endflag = true;
        break;
        }
        }
        }catch (Exception e) {
// TODO: handle exception
}finally{
try{
if(pw != null)
pw.close();
if(br != null)
br.close();
if(sock!=null)
sock.close();
}catch (Exception e) {
// TODO: handle exception
}
}


class InputThread extends Thread{
private Socket sock = null;
private BufferedReader br = null;
public InputThread(Socket sock, BufferedReader br){
this.sock = sock;
this.br = br;
}
public void run(){
try{
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
}catch (Exception e) {
// TODO: handfle exception
}finally{
try{
if(br != null)
br.close();
if(sock != null)
sock.close();
}catch (Exception e) {
// TODO: handle exception
}
}
}