import java.net.*;
import java.io.*;
import java.util.*;
import java.net.*;

public class ChatClientByConsole
{

 static int port = 0;
 static String host = "";
 public ChatClientByConsole(String host,int port)
 {
  this.port = port;
  this.host = host;
  

 }
 public static void main(String[] args)
 {
  new ChatClientByConsole("127.0.0.1",10001);

  Socket sock = null;;
  PrintWriter pw=null;
  BufferedReader br=null;
  BufferedReader br2=null;
  InputStream in;
  OutputStream out;

  try
  {
   sock = new Socket(host,port);
   br = new BufferedReader(new InputStreamReader(System.in));
   in = sock.getInputStream();
   out = sock.getOutputStream();
   pw = new PrintWriter(new OutputStreamWriter(out));
   br2 = new BufferedReader(new InputStreamReader(in));
   
   pw.println(args[0]);
   pw.flush();
   
   String line= null;
   
   //입력쓰레드 생성
   InputThread it = new InputThread(sock,br2);
   it.start();

   while((line=br.readLine())!=null)
   {
    //System.out.println(args[0]+":"+line);
    pw.println(line);
    pw.flush();
   }

   
  }
  catch (SocketException sqe)
  {
   System.out.println("서버와 연결이 안됨");
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
  }
  finally
  {
   try
   {
    if(pw!=null)
     pw.close(); 
    if(br2!=null)
     br2.close(); 
    //if(br2!=null)
    // br2.close();
    if(sock!=null)
     sock.close(); 
   }
   catch (Exception ex)
   {
   }
   
  }
  //키보드로 부터 입력받는다.
  //입력받은 걸 inputStream으로 바꾼다.
  //그런다음

 }
}

class InputThread extends Thread
{
 Socket sock = null;
 BufferedReader br = null;
 PrintWriter pw = null;
 InputStream in = null;
 OutputStream out = null;

 public InputThread(Socket sock,BufferedReader br)
 {
  this.sock = sock;
  this.br = br;
 }

 public void run()
 {
  try
  {
   in=sock.getInputStream();
   out=sock.getOutputStream();

   //br = new BufferedReader(new InputStreamReader(in));
   //pw = new PrintWriter(new OutputStreamWriter(out));
   
   String line = null;

   while( (line=br.readLine()) != null)
   {
    System.out.println(line);
    
   }
  }
  catch (Exception ex)
  {
   ex.printStackTrace();
  }
  finally
  {
   try
   {
    if(pw!=null)
     pw.close(); 
    if(br!=null)
     br.close();
    //if(br2!=null)
    // br2.close();
    if(sock!=null)
     sock.close(); 
   }
   catch (Exception ex)
   {
   }
   
  }
  
 }
}

'자바 > 채팅방' 카테고리의 다른 글

swing으로 만든 멀티채팅소스  (0) 2010.10.27
멀티서버네번째소스  (0) 2010.10.26
자바 멀티서버 기본소스  (0) 2010.10.24
채팅방클라이언트  (0) 2010.10.22
채팅방 Client UI  (0) 2010.10.21

+ Recent posts