채팅만들기
다대다 양방향 통신
* ChatProtocol
package chatV4;
//메서드는 몸체가 필요없음. 추상메서드 -> public abstract 생략되어 있음.
//변수가 String name -> public static final 생략되어 있음.
public interface ChatProtocol {
public static final String ALL = "ALL";
String MSG = "MSG";
}
* SocketServer
package chatV4;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;
import chatV4.ChatProtocol;
public class SocketServer {
ServerSocket serverSocket;
Vector<SocketThread> vc;
public SocketServer() {
try {
serverSocket = new ServerSocket(20000);
vc = new Vector<>();
// 메인 스레드는 소켓을 accept() 하고 vector에 담는 역할을 한다.
while (true) {
System.out.println("요청 대기");
Socket socket = serverSocket.accept(); // 클라이언트의 요청을 받는다.
System.out.println("요청 받음");
SocketThread st = new SocketThread(socket);
st.start();
vc.add(st);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 소켓정보 + 타겟 (run) + 식별자 (id)
class SocketThread extends Thread {
Socket socket;
String id;
BufferedReader reader;
PrintWriter writer;
public SocketThread(Socket socket) {
this.socket = socket;
}
// List
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
writer = new PrintWriter(socket.getOutputStream(), true);
writer.println("please Sign in");
id = reader.readLine();
System.out.println("이거실행됨");
System.out.println(id);
String line = null;
while ((line = reader.readLine()) != null) {
router(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void router(String line) {
String[] gubun = line.split(":"); // ALL : 안녕, MSG:white:안녕
String protocol = gubun[0];
if (protocol.equals(chatV4.ChatProtocol.ALL)) {
String msg = gubun[1];
allChat(msg);
} else if (protocol.equals(chatV4.ChatProtocol.MSG)) {
String otherId = gubun[1];
String msg = gubun[2];
privateChat(otherId, msg);
}
}
public void privateChat(String otherId, String msg) {
System.out.println(id + " : " + msg + " ip : " + socket.getInetAddress());
for (SocketThread socketThread : vc) {
if (socketThread.id.equals(otherId)) {
socketThread.writer.println(id + " : " + msg);
} else {
writer.println("아이디를 찾을 수 없습니다.");
}
}
}
}
public static void main(String[] args) {
new SocketServer();
}
public void allChat(String msg) {
// TODO Auto-generated method stub
}
}
* SocketClient
package chatV4;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import chatV4.SocketServer.SocketThread;
public class SocketClient {
Socket socket;
BufferedReader reader;
PrintWriter writer;
Scanner sc;
public SocketClient() {
try {
// accept() 호출되고, 스레드만들어지고, 벡터에 추가된다.
socket = new Socket("localhost", 20000);
SocketThread st = new SocketThread();
st.start();
writer = new PrintWriter(socket.getOutputStream(), true);
sc = new Scanner(System.in);
while(true) {
String line = sc.nextLine();
// ALL : 안녕
// MSG : white : 안녕
writer.println();
}
}catch(Exception e) {
e.printStackTrace();
}
}
class SocketThread extends Thread {
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while((line = reader.readLine()) != null) {
System.out.println("from server : " + line);
}
}catch(Exception e) {
}
}
}
public static void main(String[] args) {
new SocketClient();
}
}
'Java programing > 프로그래밍 언어 활용' 카테고리의 다른 글
Java - Object 클래스 (0) | 2020.04.17 |
---|---|
Java - SDK, REST API, FDMA, TDMA (0) | 2020.04.17 |
Java - 채팅만들기, 클론코딩 (0) | 2020.04.14 |
Java - 소켓통신, 내부클래스, Finally (0) | 2020.04.14 |
Java - 스레드1, 익명클래스 (0) | 2020.04.13 |