tobby48 4 years ago
parent
commit
800b50d301

+ 46
- 0
src/main/java/kr/co/swh/lecture/opensource/project/drawing/ChattingClientOutputThread.java View File

@@ -0,0 +1,46 @@
1
+package kr.co.swh.lecture.opensource.project.drawing;
2
+
3
+import java.io.DataOutputStream;
4
+import java.io.IOException;
5
+import java.net.Socket;
6
+import java.util.Scanner;
7
+
8
+/**
9
+ * <pre>
10
+ * kr.co.swh.lecture.network.chat.client 
11
+ * ChattingClientOutputThread.java
12
+ *
13
+ * 설명 :클라이언트 출력 스레드
14
+ * </pre>
15
+ * 
16
+ * @since : 2018. 12. 28.
17
+ * @author : tobby48
18
+ * @version : v1.0
19
+ */
20
+public class ChattingClientOutputThread extends Thread{
21
+	private Socket socket;
22
+	private String nick;
23
+	
24
+	public ChattingClientOutputThread(Socket socket, String nick) {
25
+		this.socket = socket;
26
+		this.nick = String.format("[%s] ", nick);
27
+	}
28
+	
29
+	public void run() {
30
+		DataOutputStream dos = null;
31
+		Scanner scn = null;
32
+		try {
33
+			dos = new DataOutputStream(socket.getOutputStream());
34
+			scn = new Scanner(System.in);
35
+			String line;
36
+			while((line = scn.nextLine())!=null) {
37
+				dos.writeUTF(nick + line);
38
+			}
39
+			socket.close();
40
+		}catch(IOException e) {
41
+			e.printStackTrace();
42
+		}finally {
43
+			try{ dos.close(); scn.close(); }catch(Exception e) {}
44
+		}
45
+	}
46
+}

+ 56
- 0
src/main/java/kr/co/swh/lecture/opensource/project/drawing/DrawingServer.java View File

@@ -0,0 +1,56 @@
1
+package kr.co.swh.lecture.opensource.project.drawing;
2
+
3
+import java.io.IOException;
4
+import java.net.ServerSocket;
5
+import java.net.Socket;
6
+import java.util.HashSet;
7
+import java.util.Iterator;
8
+import java.util.Set;
9
+
10
+/**
11
+ * <pre>
12
+ * kr.co.swh.lecture.network.chat 
13
+ * ChattingServer.java
14
+ *
15
+ * 설명 :채팅 서버 프로그램, 소켓을 관리
16
+ * </pre>
17
+ * 
18
+ * @since : 2018. 12. 28.
19
+ * @author : tobby48
20
+ * @version : v1.0
21
+ */
22
+public class DrawingServer extends Thread{
23
+    Set<ServerThread> sockets = new HashSet<>();
24
+    
25
+    public void run(){
26
+        ServerSocket serverSocket = null;
27
+        try{
28
+            serverSocket = new ServerSocket(9998);
29
+            System.out.println("클라이언트 연결을 기다립니다.");
30
+            while(true){
31
+                Socket s = serverSocket.accept();
32
+                ServerThread t = new ServerThread(this, s);
33
+                t.start();
34
+                sockets.add(t);
35
+            }
36
+        }catch(IOException e){
37
+            System.err.println("서버 에러");
38
+            e.printStackTrace();
39
+        }finally{
40
+            try{ serverSocket.close(); } catch(Exception e) {}
41
+        }
42
+    }
43
+    
44
+    public void broadcast(String message){
45
+        Iterator<ServerThread> it = sockets.iterator();
46
+        while(it.hasNext()){
47
+            ServerThread peer = it.next();
48
+            peer.sendMessage(message);
49
+        }
50
+    }
51
+    
52
+    public static void main(String[] args) {
53
+    	DrawingServer server = new DrawingServer();
54
+    	server.start();
55
+    }
56
+}

+ 125
- 0
src/main/java/kr/co/swh/lecture/opensource/project/drawing/LineDraw.java View File

@@ -0,0 +1,125 @@
1
+package kr.co.swh.lecture.opensource.project.drawing;
2
+
3
+
4
+import java.awt.BasicStroke;
5
+import java.awt.Color;
6
+import java.awt.Graphics;
7
+import java.awt.Graphics2D;
8
+import java.awt.Point;
9
+import java.awt.event.MouseAdapter;
10
+import java.awt.event.MouseEvent;
11
+import java.awt.event.MouseMotionAdapter;
12
+import java.awt.geom.Line2D;
13
+import java.io.BufferedWriter;
14
+import java.io.DataInputStream;
15
+import java.io.DataOutputStream;
16
+import java.io.IOException;
17
+import java.net.Socket;
18
+import java.net.UnknownHostException;
19
+import java.util.ArrayList;
20
+import java.util.List;
21
+
22
+import javax.swing.JFrame;
23
+import javax.swing.JPanel;
24
+
25
+import com.google.gson.Gson;
26
+import com.google.gson.reflect.TypeToken;
27
+
28
+public class LineDraw extends JPanel {
29
+	private List<List<Point>> point = new ArrayList<List<Point>>();
30
+	private List<List<Point>> drawingPoint = new ArrayList<List<Point>>();
31
+	DataOutputStream dos;
32
+	
33
+	class ChattingClientInputThread extends Thread{
34
+		private Socket socket;
35
+
36
+		public ChattingClientInputThread(Socket socket) {
37
+			this.socket = socket;
38
+		}
39
+
40
+		public void run() {
41
+			DataInputStream dis = null;
42
+			try {
43
+				dis = new DataInputStream(socket.getInputStream());
44
+				String line = dis.readUTF();
45
+				Gson gson = new Gson();
46
+				drawingPoint = gson.fromJson(line, new TypeToken<List<List<Point>>>(){}.getType());
47
+				System.out.println(drawingPoint);
48
+				repaint();
49
+			}catch(IOException e) {
50
+				e.printStackTrace();
51
+			}finally{
52
+				try{ dis.close(); }catch(Exception e) {}
53
+			}
54
+		}
55
+	}
56
+	
57
+	public LineDraw(Socket socket) {
58
+		ChattingClientInputThread thread = new ChattingClientInputThread(socket);
59
+		thread.start();
60
+		try {
61
+			dos = new DataOutputStream(socket.getOutputStream());
62
+		} catch (IOException e1) {
63
+			// TODO Auto-generated catch block
64
+			e1.printStackTrace();
65
+		}
66
+		
67
+		addMouseListener(new MouseAdapter() {
68
+			public void mousePressed(MouseEvent event) {
69
+				List<Point> list = new ArrayList<Point>();
70
+				point.add(list);
71
+			}
72
+			
73
+			@Override
74
+			public void mouseClicked(MouseEvent e) {
75
+				// TODO Auto-generated method stub
76
+
77
+			}
78
+			
79
+		});
80
+
81
+		addMouseMotionListener(new MouseMotionAdapter() {
82
+			public void mouseDragged(MouseEvent event) {
83
+				Point p = event.getPoint();
84
+				Gson gg = new Gson();
85
+				point.get(point.size()-1).add(event.getPoint());
86
+//				repaint();
87
+				
88
+				
89
+				try {
90
+					dos.writeUTF(gg.toJson(point));
91
+				} catch (IOException e) {
92
+					// TODO Auto-generated catch block
93
+					e.printStackTrace();
94
+				}
95
+			}
96
+			
97
+		});
98
+		
99
+	}
100
+
101
+	public void paintComponent(Graphics g) {
102
+		super.paintComponent(g);
103
+		Graphics2D g2 = (Graphics2D) g;
104
+		g2.setColor(new Color(0, 0, 128));
105
+		g2.setStroke(new BasicStroke(15f,
106
+				BasicStroke.CAP_ROUND,
107
+				BasicStroke.JOIN_ROUND));
108
+		for (int i = 0; i < drawingPoint.size(); i++) {
109
+			List<Point> l = drawingPoint.get(i);
110
+			for (int j = 1; j < l.size(); j++) {
111
+				g2.draw(new Line2D.Float(l.get(j-1), l.get(j)));
112
+			}
113
+		}
114
+	}
115
+
116
+	public static void main(String[] args) throws UnknownHostException, IOException {
117
+//		Thread.currentThread().getContextClassLoader().getResourceAsStream("db.pro")
118
+		Socket socket = new Socket("127.0.0.1", 9998);
119
+		JFrame f = new JFrame();
120
+		f.add(new LineDraw(socket));
121
+		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
122
+		f.setSize(800, 600);
123
+		f.setVisible(true);
124
+	}
125
+}

+ 63
- 0
src/main/java/kr/co/swh/lecture/opensource/project/drawing/ServerThread.java View File

@@ -0,0 +1,63 @@
1
+package kr.co.swh.lecture.opensource.project.drawing; 
2
+
3
+import java.io.DataInputStream;
4
+import java.io.DataOutputStream;
5
+import java.io.IOException;
6
+import java.io.InputStream;
7
+import java.io.OutputStream;
8
+import java.net.Socket;
9
+
10
+/**
11
+ * <pre>
12
+ * kr.co.swh.lecture.network.chat 
13
+ * ServerThread.java
14
+ *
15
+ * 설명 :채팅 서버 메세지 전송 스레드
16
+ * </pre>
17
+ * 
18
+ * @since : 2018. 12. 28.
19
+ * @author : tobby48
20
+ * @version : v1.0
21
+ */
22
+public class ServerThread extends Thread{
23
+    private Socket socket;
24
+    private DrawingServer server;
25
+    private OutputStream os;
26
+    
27
+    public ServerThread(DrawingServer server, Socket s){
28
+        this.server = server;
29
+        this.socket = s;
30
+        System.out.println("클라이언트 연결 : " + s.getInetAddress().getHostAddress());
31
+    }
32
+    
33
+    public void run(){
34
+        InputStream is = null;
35
+        DataInputStream dis = null;
36
+        
37
+        try{
38
+            is = socket.getInputStream();
39
+            dis = new DataInputStream(is);
40
+            String line;
41
+            while(true){
42
+                line = dis.readUTF();
43
+                //	서버로 들어온 메세지는 접속된 모든 클라이언트에게 전송
44
+                server.broadcast(line);
45
+            }
46
+        }catch(IOException e){
47
+            e.printStackTrace();
48
+        }finally{
49
+            try{ is.close(); } catch(IOException e) {}
50
+            try{ socket.close(); } catch(IOException e) {}
51
+        }
52
+    }
53
+    
54
+    public void sendMessage(String message){
55
+        try {
56
+            os = socket.getOutputStream();
57
+            DataOutputStream dos = new DataOutputStream(os);
58
+            dos.writeUTF(message);
59
+        }catch(IOException e) {
60
+            e.printStackTrace();
61
+        }
62
+    }
63
+}