본문 바로가기
Java 프로그래밍 예제

자바 TCP 채팅 코드

by 자유코딩 2017. 11. 5.

서버 코드

 

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package TCP;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
 
class ServerSend extends Thread{
    Socket socket;
    public ServerSend(Socket socket) {
        this.socket = socket;
 
    }
    //서버가 보내는 스레드
    @Override
    public void run() {
        BufferedWriter bw=null;
        Scanner input = new Scanner(System.in);
        
        try {
            while (true) {
                bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                bw.write(input.nextLine()+"\n");
                bw.flush();
                
            }
        } catch (Exception e) {
            if (bw!=null) {
                try {
                    bw.close();                    
                } catch (Exception e2) {
                }
            }
        }
 
    }
}
class ServerReceive extends Thread{
    Socket socket;
    public ServerReceive(Socket socket) {
        this.socket = socket;
    }
    //서버가 받는 스레드
    @Override
    public void run() {
        BufferedReader br = null;
        
        try {
            while (true) {
                br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                System.out.println(br.readLine());
                
            }
            
        } catch (Exception e) {
            try {
                if (br!=null) {
                    br.close();
                }
            } catch (Exception e2) {
            }
        }
    }
}
 
public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(5000);
            Socket socket = serverSocket.accept();
            System.out.println("연결 됨");
            ServerSend ss = new ServerSend(socket);
            ServerReceive sr = new ServerReceive(socket);
            ss.start();
            sr.start();
            
            //Socket socket = new ServerSocket(5000).accept();
            //accept가 Scanner에서 scan.nextint();같은 역할을 한다
            //소켓을 열고 요청을 대기한다. 요청이 들어오면 , 즉 scan.nextint와 같은 상황에서 사용자가 입력을 하듯
            //소켓으로 요청이 들어오면 그것을 socket에 대입한다
            
        } catch (Exception e) {
            // TODO: handle exception
        }
        //괄호 안의 포트 번호
    }
 
}
 
cs

 

클라이언트 코드

 

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
67
68
69
70
71
72
73
74
75
package TCP;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;
 
class ClientSend extends Thread {
    Socket socket;
    public ClientSend(Socket socket) {
        this.socket = socket;
    }
    @Override
    public void run(){
        BufferedWriter bw = null;
        Scanner input = new Scanner(System.in);
        try {
            while (true) {
                bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                bw.write(input.nextLine()+"\n");
                bw.flush();                
            }
        } catch (Exception e) {
            try {
                if (bw!=null) {
                    bw.close();
                }                
            } catch (Exception e2) {
            }
        }
 
    }
}
class ClientReceive extends Thread {
    Socket socket;
    public ClientReceive(Socket socket) {
        this.socket = socket;
    }
    @Override
    public void run() {
        BufferedReader br = null;
        try {
            while (true) {
                br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                System.out.println(br.readLine());                
            }
        } 
        catch (Exception e) {
            try {
                if (br!=null) {
                    br.close();
                }
            } 
            catch (Exception e2) {
            }
        }
    }
}
public class Client {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("192.168.134.1"5000);
            ClientSend cs = new ClientSend(socket);
            ClientReceive cr = new ClientReceive(socket);
            cr.start();
            cs.start();
        } catch (Exception e) {
            // TODO: handle exception
        }
        
    }
}
 
cs

 

실행 화면

 

서버

 

클라이언트

 

 

댓글