JAVA 채팅 소스 -1 Client 쪽

Programming/JAVA,JSP 2014. 7. 12. 14:50 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.Socket;

import java.net.UnknownHostException;

import java.util.Scanner;

 

class RecvThread extends Thread {

     Socket s;

 

     public RecvThread(Socket s) {

          this.s = s;

     }

 

     @Override

     public void run() {

          // 받는 부분

          BufferedReader is;

          try {

              is = new BufferedReader(new InputStreamReader(s.getInputStream()));

              String recvMsg;

              while ((recvMsg = is.readLine()) != null) {

                   System.out.println(recvMsg);

              }

          } catch (IOException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

          }

     }

}

 

class SendThread extends Thread {

     Socket s;

 

     public SendThread(Socket s) {

          this.s = s;

     }

 

     @Override

     public void run() {

          PrintWriter os;

          try {

              os = new PrintWriter(s.getOutputStream(), true);

              // 보내는 부분

              Scanner sc = new Scanner(System.in);

              while (true) {

                   String msg = sc.next();

                   os.println(msg);

              }

          } catch (IOException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

          }

     }

 

}

 

public class ClientTest {

     public static void main(String[] args) {

          try {

              Socket s = new Socket("115.20.247.142", 4000);

              SendThread st = new SendThread(s);

              RecvThread rt = new RecvThread(s);

          } catch (UnknownHostException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

          } catch (IOException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

          }

     }

}

 

 

'Programming > JAVA,JSP' 카테고리의 다른 글

Java ScrollPane 활용  (0) 2014.07.12
java  (0) 2014.07.12
Java 선택 정렬 알고리즘 구현  (0) 2014.06.14
String to int, int to String  (0) 2014.06.07
이클립스 라인넘버 표시  (1) 2014.03.07