๋ฉ์์ง์ ์์ ์ ์ก์ ์๋ฅผ ๊ฐ์ฒดํ ํ์ฌ ๊ฐ์ฒด๋ฅผ ๋ฐ์ดํธ ๋ฐ์ดํฐ๋ก ๋ฐ๊พธ์ด ๋ฐ์ดํฐ ์ก์์ ํ๊ธฐ
ChatMsg ํด๋์ค ์ฝ๋
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.Serializable;
public class ChatMsg implements Serializable{
private String sender;
private String receiver;
private String message;
private String uid;
private String upw;
public ChatMsg() { }
public ChatMsg(String sender, String receiver, String message) {
super();
this.sender = sender;
this.receiver = receiver;
this.message = message;
}
public ChatMsg(String msg) {
this.message=msg;
}
public ChatMsg(String uid, String upw) {
this.uid=uid;
this.upw=upw;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUpw() {
return upw;
}
public void setUpw(String upw) {
this.upw = upw;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getReceiver() {
return receiver;
}
public void setReceiver(String receiver) {
this.receiver = receiver;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
|
cs |
๋ฐ์ดํฐ๋ฅผ ๊ฐ์ฒดํ ํ ChatMsg ํด๋์ค๋ฅผ ๋จผ์ ๋ง๋ ๋ค. ๊ฐ์ฒดํ ๋ ๋ฐ์ดํฐ๋ ์ง๋ ฌํ๋ฅผ ํตํด ์คํธ๋ฆผ์ผ๋ก ์ ๋ ฅ ์ถ๋ ฅ๋๊ธฐ ๋๋ฌธ์ ์ง๋ ฌํ ์ธํฐํ์ด์ค๋ฅผ implements ํด์ค์ผํ๋ค.
ํด๋ผ์ด์ธํธ ํด๋์ค ์ฝ๋
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
93
94
95
|
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client
{
public static void main(String[] args)
{
try {
Socket s = new Socket("127.0.0.1",1234);
System.out.println("์๋ฒ์ ์ ์ฑ๊ณต");
//์๋ฒ์์ ์ ์กํ ๋ฉ์์ง๋ฅผ ์์ ํ๋ค
InputStream is = s.getInputStream();
ObjectInputStream oin = new ObjectInputStream(is);
new ClientThread(s, oin).start(); //๋คํธ์ํฌ ์ถ๋ ฅ ์ฐ๋ ๋
while(true) {//๋คํธ์ํฌ ์
๋ ฅ ์ฐ๋ ๋
ChatMsg m = (ChatMsg)oin.readObject();
System.out.println("์๋ฒ์ธก์์ ๋ณด๋ด์จ ๋ฉ์ธ์ง:"+m.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("ํด๋ผ์ด์ธํธ ์ข
๋ฃ");
}
//๋คํธ์ํฌ ์ถ๋ ฅ ๊ธฐ๋ฅ์ ๋ด๋นํ ํด๋ผ์ด์ธํธ ์ธก ์ฐ๋ ๋
static class ClientThread extends Thread {
Socket s;
ObjectOutputStream oos;
ObjectInputStream oin;
boolean loginpass;
ClientThread(Socket s,ObjectInputStream oin){
this.s=s;
this.oin=oin;
try {
ChatMsg msg = (ChatMsg)oin.readObject();
System.out.println("์๋ฒ์ธก์์ ๋ณด๋ด์จ ๋ฉ์ธ์ง:"+msg);
if(msg.getMessage().equals("์ ์ ์ฑ๊ณต")) {
OutputStream os = s.getOutputStream();
this.oos = new ObjectOutputStream(os);
loginpass = login();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
Scanner kbd = new Scanner(System.in);
while(true) {
System.out.println("์
๋ ฅํ ๋ฉ์์ง:");
String line = kbd.nextLine().strip();
ChatMsg cmsg = new ChatMsg();
cmsg.setSender("ChatMsg");
cmsg.setReceiver("Laura");
cmsg.setMessage(line);
try {
oos.writeObject(cmsg);
oos.flush();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private boolean login() {
try {
ChatMsg msg = new ChatMsg("Smith", "12345");
oos.writeObject(msg);
oos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
String loginresult =null;
try {
loginresult = ((ChatMsg)oin.readObject()).getMessage();
System.out.println("๋ก๊ทธ์ธ ๊ฒฐ๊ณผ:"+loginresult);
} catch (Exception e) {
e.printStackTrace();
}
if(loginresult.equals("๋ก๊ทธ์ธ ์ฑ๊ณต")) {
return true;
}else {
return false;
}
}
}
}
|
cs |
ํ ์คํธ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ ์ฐ๋ ์คํธ๋ฆผ(ex PrintWriter)์ ๊ฐ์ฒด๋ฅผ ์ฝ๊ณ ์ฐ๋ ์คํธ๋ฆผ(ObjectOutputStream)์ผ๋ก ๋ฐ๊ฟ์ค๋ค.
์๋ฒ ํด๋์ค ์ฝ๋
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
|
import java.io.*;
import java.net.*;
import java.util.ArrayList;
public class Server
{
static ArrayList<ObjectOutputStream> out = new ArrayList<ObjectOutputStream>();
public static void main(String[] args)
{
try {
ServerSocket ss = new ServerSocket(1234);
while(true) {
System.out.println("์๋ฒ์์ผ ๋๊ธฐ์ค");
Socket s = ss.accept(); //๋ฌดํ๋๊ธฐํ๋ค๊ฐ ํด๋ผ์ด์ธํธ๊ฐ ์ ์ํ๋ฉด ์์ผ์ ๋ง๋ค์ด๋
System.out.println("ํด๋ผ์ด์ธํธ ์ ์๋จ");
new CommThread(s).start();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("์๋ฒ ์ข
๋ฃ");
}
//๋ด๋ถ ํด๋์ค๋ก ๊ตฌํํ ํต์ ์ฉ ์ฐ๋ ๋
static class CommThread extends Thread {
private Socket s;
private ObjectOutputStream oos;
private ObjectInputStream oin;
private boolean loginpass;
CommThread(Socket s) {
this.s = s;
try {
OutputStream os = s.getOutputStream();
this.oos = new ObjectOutputStream(os);
oos.writeObject(new ChatMsg("์ ์ ์ฑ๊ณต"));
oos.flush();
loginpass = login();
}catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
if(loginpass) {
try {
ObjectOutputStream toClient = null;
while(true) {
ChatMsg cmsg = (ChatMsg)oin.readObject();
String receiver = cmsg.getReceiver();
for(int i=0;i<out.size();i++) {
toClient = out.get(i);
toClient.writeObject(cmsg);
toClient.flush();
}
}
}catch (Exception e) {
System.err.println("ํด๋ผ์ด์ธํธ ๋๊ฐ");
out.remove(oos);
System.out.println("์ ์์์" + out.size());
}
}
System.out.println("ํต์ ์ฐ๋ ๋ ์ข
๋ฃ");
}
private boolean login() {
try {
InputStream is = s.getInputStream();
this.oin = new ObjectInputStream(is);
ChatMsg cmsg = (ChatMsg)oin.readObject();
String uid = cmsg.getUid();
String upw = cmsg.getUpw();
if(uid.equals("Smith")&&upw.equals("12345")) {
out.add(oos); //๋ชจ๋ ์ถ๋ ฅ์ฉ ์คํธ๋ฆผ์ ํ๊ฐ์ ๋ฆฌ์คํธ์ ์ ์ฅํ๋ค.
oos.writeObject(new ChatMsg("๋ก๊ทธ์ธ ์ฑ๊ณต"));
oos.flush();
return true;
}
}catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
}
|
cs |
ํด๋ผ์ด์ธํธ๋ฅผ 2๊ฐ ์คํํ๊ณ ํ๊ฐ๋ฅผ ์ข ๋ฃ ์ํจ๋ค ์๋ฒํด๋์ค ์ฝ์ ๊ฐ:
ํด๋ผ์ด์ธํธ ํด๋์ค ์ฝ์ ๊ฐ:
terminated ๋ ํด๋ผ์ด์ธํธ ํด๋์ค ์ฝ์ ๊ฐ:
ํด๋ผ์ด์ธํธ๊ฐ ๋ค๋ฅธ ํด๋ผ์ด์ธํธํํ ๋ฉ์์ง ๋ณด๋ด๊ธฐ
List๋ฅผ Map ์ผ๋ก ๋ฐ๊ฟ์ key๋ฅผ ์์ด๋๋ก ์ฃผ์ด ์์ ์๋ฅผ ์ ํด์ฃผ๋ฉด ๊ทธ ์์ ์์๊ฒ ๋ฉ์์ง๊ฐ ๊ฐ๊ฒ ํ๊ธฐ
์๋ฒ ํด๋์ค ์ฝ๋
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
93
94
95
96
97
|
import java.io.*;
import java.net.*;
import java.util.HashMap;
public class Server
{
static HashMap<String, ObjectOutputStream> outmap = new HashMap<>();
public static void main(String[] args)
{
try {
ServerSocket ss = new ServerSocket(1234);
while(true) {
System.out.println("์๋ฒ์์ผ ๋๊ธฐ์ค");
Socket s = ss.accept(); //๋ฌดํ๋๊ธฐํ๋ค๊ฐ ํด๋ผ์ด์ธํธ๊ฐ ์ ์ํ๋ฉด ์์ผ์ ๋ง๋ค์ด๋
System.out.println("ํด๋ผ์ด์ธํธ ์ ์๋จ");
new CommThread(s).start();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("์๋ฒ ์ข
๋ฃ");
}
//๋ด๋ถ ํด๋์ค๋ก ๊ตฌํํ ํต์ ์ฉ ์ฐ๋ ๋
static class CommThread extends Thread {
private Socket s;
private ObjectOutputStream oos;
private ObjectInputStream oin;
private boolean loginpass;
private String uid;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
CommThread(Socket s) {
this.s = s;
try {
OutputStream os = s.getOutputStream();
this.oos = new ObjectOutputStream(os);
oos.writeObject(new ChatMsg("์ ์ ์ฑ๊ณต"));
oos.flush();
loginpass = login();
}catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
if(loginpass) {
try {
ObjectOutputStream toClient = null;
while(true) {
ChatMsg cmsg = (ChatMsg)oin.readObject();//ํด๋ผ์ด์ธํธ์์ ์จ ๋ฉ์์ง ๋ฐ๊ธฐ
String receiver = cmsg.getReceiver();
outmap.get(receiver).writeObject(cmsg);
outmap.get(receiver).flush();
}
}catch (Exception e) {
System.err.println("ํด๋ผ์ด์ธํธ ๋๊ฐ");
outmap.remove(this.uid);
System.out.println("์ ์์์" + outmap.size());
}
}
System.out.println("ํต์ ์ฐ๋ ๋ ์ข
๋ฃ");
}
private boolean login() {
try {
InputStream is = s.getInputStream();
this.oin = new ObjectInputStream(is);
ChatMsg cmsg = (ChatMsg)oin.readObject();
String uid = cmsg.getUid();
String upw = cmsg.getUpw();
if(!uid.equals("")&&uid!=null
&&!upw.equals("")&&upw!=null) {
this.uid=uid;
outmap.put(uid, oos); //map์ uid์ ๊ทธ์ฌ๋์ ์ถ๋ ฅ์คํธ๋ฆผ์ ์์ผ๋ก ์ ์ฅ
oos.writeObject(new ChatMsg("๋ก๊ทธ์ธ ์ฑ๊ณต"));
oos.flush();
return true;
}
}catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
}
|
cs |
ArrayList๋ฅผ HashMap๋ก ๋ฐ๊ฟ์ค๋ค.
ํด๋ผ์ด์ธํธ ํด๋์ค ์ฝ๋
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
|
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client
{
public static void main(String[] args)
{
try {
Socket s = new Socket("127.0.0.1",1234);
System.out.println("์๋ฒ์ ์ ์ฑ๊ณต");
InputStream is = s.getInputStream();
ObjectInputStream oin = new ObjectInputStream(is);
new ClientThread(s, oin).start(); //๋คํธ์ํฌ ์ถ๋ ฅ ์ฐ๋ ๋
while(true) {//๋คํธ์ํฌ ์
๋ ฅ ์ฐ๋ ๋
ChatMsg m = (ChatMsg)oin.readObject();
System.out.println("์๋ฒ์ธก์์ ๋ณด๋ด์จ ๋ฉ์ธ์ง:"+m.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("ํด๋ผ์ด์ธํธ ์ข
๋ฃ");
}
//๋คํธ์ํฌ ์ถ๋ ฅ ๊ธฐ๋ฅ์ ๋ด๋นํ ํด๋ผ์ด์ธํธ ์ธก ์ฐ๋ ๋
static class ClientThread extends Thread {
Socket s;
ObjectOutputStream oos;
ObjectInputStream oin;
boolean loginpass;
ClientThread(Socket s,ObjectInputStream oin){
this.s=s;
this.oin=oin;
try {
ChatMsg msg = (ChatMsg)oin.readObject();
System.out.println("์๋ฒ์ธก์์ ๋ณด๋ด์จ ๋ฉ์ธ์ง:"+msg.getMessage());
if(msg.getMessage().equals("์ ์ ์ฑ๊ณต")) {
OutputStream os = s.getOutputStream();
this.oos = new ObjectOutputStream(os);
loginpass = login();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
Scanner kbd = new Scanner(System.in);
while(true) {
System.out.println("์
๋ ฅํ ๋ฉ์์ง:");
String line = kbd.nextLine().strip();
System.out.println("์์ ์ ๋ช
:");
String rec = kbd.nextLine().strip();
ChatMsg cmsg = new ChatMsg();
cmsg.setReceiver(rec);
cmsg.setMessage(line);
try {
oos.writeObject(cmsg);
oos.flush();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private boolean login() {
String loginresult = null;
Scanner kbd = new Scanner(System.in);
System.out.println("์์ด๋ ์
๋ ฅ:");
try {
String uid = kbd.nextLine().strip();
System.out.println("๋น๋ฐ๋ฒํธ ์
๋ ฅ:");
String upw = kbd.nextLine().strip();
ChatMsg msg = new ChatMsg(uid, upw);
oos.writeObject(msg);
oos.flush();
loginresult = ((ChatMsg)oin.readObject()).getMessage();
System.out.println("๋ก๊ทธ์ธ ๊ฒฐ๊ณผ:"+loginresult);
} catch (Exception e) {
e.printStackTrace();
}
if(loginresult.equals("๋ก๊ทธ์ธ ์ฑ๊ณต")) {
return true;
}else {
return false;
}
}
}
}
|
cs |
ํด๋ผ์ด์ธํธ๋ฅผ ๋๊ฐ ์คํ ์์ผ ๊ฐ๊ฐ ์์ด๋๋ฅผ ๋ค๋ฅด๊ฒ ์ฃผ์ด ์คํ ํ๋ค.
ํด๋ผ์ด์ธํธ 1 ์ฝ์๊ฐ:
ํด๋ผ์ด์ธํธ2 ์ฝ์ ๊ฐ:
ํด๋ผ์ด์ธํธ 1์์ ํด๋ผ์ด์ธํธ2๋ฅผ ์์ ์๋ก ์ ํด์ ๋ณด๋ธ ๋ด์ฉ์ด ํด๋ผ์ด์ธํธ1์์ ๋ณด์ด์ง ์๊ณ ํด๋ผ์ด์ธํธ 2์์๋ง ๋ณด์ธ๋ค.
์น ์๋ฒ ํ๋ก๊ทธ๋๋ฐํ๊ธฐ ์ํด์ Java, ํตํฉ๊ฐ๋ฐํ๊ฒฝ(Eclipse), Web browser(ed Chrome), WAS (Web Application Server, ex Tomcat) 4๊ฐ์ง๊ฐ ๊ฐ์ถฐ์ ธ์ผํ๋ค.
ํฐ์บฃ์ ๋ค์ด๋ฐ์ ์ค์ ํด์ค ํฌํธ๋ก ๋ค์ด๊ฐ๋ฉด ๋ง๊ฒ ๋์จ๊ฑธ ํ์ธํ ์ ์๋ค.
ํฐ์บฃ ํ์ผ์ ์ ํ ์คํธ ๋ฌธ์๋ฅผ ํ๋ ๋ง๋ค์ด์ html๋ฌธ์๋ก ๋ฐ๊พธ๊ธฐ
๋๋ฒ๊น ํด๋ณด๊ธฐ ์ํด์ ๋ฉ๋ชจ์ฅ์ ์งง์ html์์์ ๋ง์ถ ์ฝ๋๋ฅผ ์์ฑํ๋ค.
< / > ์ด๋ ๊ฒ ๋์ด ์๋ ๊ฒ๋ค์ ๋ฐ์ดํฐ๋ผ ๋ถ๋ฅด์ง ์๊ณ ํ๊ทธ๋ผ๊ณ ๋ถ๋ฅด๋ฉฐ ๋ฉํ ๋ฐ์ดํฐ์ด๋ค. ์ด๊ฒ๋ค์ ๋ฐ์ดํฐ๋ฅผ ์ํ ๋ฐ์ดํฐ์ด๋ค.
์์ฑ๋ html์ ์น ๋ธ๋ผ์ฐ์ ์์ ํ์ธํด ๋ณผ ์ ์๋ค.
๊ธ์์์ ๋ฐ๊พธ๋ css ์์ ๋ ํ ์ ์๋ค.
์์ RGB๊ฐ์ผ๋ก ๋ํ๋ด์ด ์ฝ๋ฉํ๋ค.
์คํฌ๋ฆฝํธ๋ ๋์ ์ธ ์นํ์ด์ง๋ฅผ ๋ง๋ค๋ ์ฐ๋๋ฐ ๋ฌด์ธ๊ฐ ํ๋์ ํ๋ฉด ์ด๋ฒคํธ๊ฐ ์ผ์ด๋๋ค.
๋ฒํผ์ ๋ง๋ค์ด ํด๋ฆญํ ๋ "์๋ ํ์ธ์~" ๋ฉ์ธ์ง๊ฐ ๋์ค๊ฒ ๋ง๋ค์๋ค.
๊ธฐ์กด๊น์ง๋ ์๋ฐ ์คํ ๋ค๋ ์๋์ ์ผ๋ก ์ฝ๋ฉํ์ง๋ง ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ, ์น ์๋ฒ ํ๋ก๊ทธ๋๋ฐ์ ํ ๋ ค๋ฉด ์๋ฐ ์ํฐํ๋ผ์ด์ฆ ์๋์ ์ผ๋ก ์ฝ๋ฉํด์ผํ๋ค.(Java EE)
์ดํด๋ฆฝ์ค Java EE ๋ชจ๋
๋ฉ๋ชจ์ฅ์์ ๋ง๋ค๋ html์์์ ์ดํด๋ฆฝ์ค์์ ์ง์ํด์ฃผ๋ ๊ฒ์ผ๋ก ์ฝ๊ฒ ๋ง๋ค ์ ์๋ค.
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
|
package com.ezen.web.hello;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/test")
public class TestServlet extends HttpServlet
{
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html; charset=utf-8");
PrintWriter pw = response.getWriter();
pw.println("<? doctype html>");
pw.println("<html>");
pw.println("<meta charset='utf-8'>");
pw.println("<head><title>์๋ธ๋ฆฟ์ผ๋ก ์๋ต ๋ง๋ค๊ธฐ</title>");
pw.println("<body>");
pw.println("<h1>์๋ธ๋ฆฟ์ผ๋ก html ์๋ตํ๊ธฐ</h1>");
pw.println("</body>");
pw.println("</head>");
pw.println("</html>");
pw.flush();
}
}
|
cs |
Servlet ํ์ผ ์๋ฒ์์ ๋์๊ฐ๋ ์์ ํ๋ก๊ทธ๋จ
Run on Server ์คํ ๊ฒฐ๊ณผ
์์ค ๋ณด๊ธฐ
๋๊ธ