code/JAVA

[JAVA] unicasting 서로 주고 받는 채팅

shallot 2017. 4. 20. 14:24

Server Class


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.io.*;
import java.net.*;
public class  UnicastServer{
    private ServerSocket serverS;
    public UnicastServer(int port){
        try{
            serverS = new ServerSocket(port);
        }catch(IOException ioe){
            ioe.printStackTrace();
            System.exit(0);
        }
        UnicastServerThread ust=null;
        while(true){
            System.out.println("클라이언트 대기중");
            Socket s=null;
            try{
                s = serverS.accept();
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
            System.out.println("client ip : "+
                s.getInetAddress().getHostAddress());
            
            ust = new UnicastServerThread(s);            
            Thread t = new Thread(ust);
            t.start();            
        }
    }
    public static void main(String[] args){
        new UnicastServer(3000);
    }
}
 

 
ServerThread Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.net.*;
import java.io.*;
public class UnicastServerThread implements Runnable{
    private Socket socket;
    private BufferedReader bufferR; 
    private BufferedWriter bufferW;
    private InputStream is;
    private OutputStream os;
    private String message;    
    private BufferedReader file=null;
    public UnicastServerThread(Socket socket){
        this.socket = socket;        
    }
    public void run(){
        boolean isStop = false;        
        try{
            is = socket.getInputStream();
            bufferR = new BufferedReader(
                    new InputStreamReader(is));
            os = socket.getOutputStream();            
            bufferW = new BufferedWriter(
                    new OutputStreamWriter(os));    
        }catch(IOException ioe){
            isStop=true;
        }
        try{    
            while(!isStop){
                            
                    String message = bufferR.readLine();
                    if(message.equals("exit")) isStop = true;
                    System.out.println("received message : "+ message);
                    message += System.getProperty("line.separator");
                    bufferW.write(message);
                    bufferW.flush();    
                    
                    System.out.printf("message : ");
                    file = new BufferedReader(new InputStreamReader(System.in));
                    message = file.readLine();
                    message += System.getProperty("line.separator");
                    bufferW.write(message);
                    bufferW.flush();            
                    message = bufferR.readLine();
                    
            }//while end        
        }catch(IOException ioe){
            System.out.println("클라이언트가 강제로 " +"종료되었습니다.");
            isStop=true;
        }finally{
            try{
                if(bufferR != null) bufferR.close();
                if(bufferW != null) bufferW.close();
                if(socket != null) socket.close();
                if(file !=null) file.close();
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
        }//finally end
    }//run end    
}
 

Client Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.io.*;
import java.net.*;
 
public class UnicastClient {
    private String ip;
    private int port;
    private String message;
    private BufferedReader file = null;
    private BufferedWriter bufferW = null;
    private BufferedReader bufferR = null;
 
    public UnicastClient(String ip, int port) throws IOException {
        this.ip = ip;
        this.port = port;
        boolean isStop = false;
        Socket tcpSocket = getSocket();
        OutputStream os = tcpSocket.getOutputStream();
        InputStream is = tcpSocket.getInputStream();
        bufferW = new BufferedWriter(new OutputStreamWriter(os));
        bufferR = new BufferedReader(new InputStreamReader(is));
        while (!isStop) {
            System.out.print("message : ");
            file = new BufferedReader(new InputStreamReader(System.in));
            message = file.readLine();
            message += System.getProperty("line.separator");
            bufferW.write(message);
            bufferW.flush();
            message = bufferR.readLine();
            
            String message = bufferR.readLine();
            if (message.equals("exit"))
                isStop = true;
            message += System.getProperty("line.separator");
            bufferW.write(message);
            bufferW.flush();
 
            
            if (message.equals("exit")) {
                isStop = true;
                System.out.println("종료되었습니다.");
            } else {
                System.out.println("Received message : " + message);
            }
        }
        tcpSocket.close();
        bufferW.close();
        bufferR.close();
        file.close();
    }
 
    public Socket getSocket() {
        Socket tcpSocket = null;
        try {
            tcpSocket = new Socket(ip, port);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.exit(0);
        }
        return tcpSocket;
    }
 
    public static void main(String[] args) throws IOException {
        new UnicastClient("localhost", 3000);
    }
}