일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- relative path
- html code
- git flow
- #다차원포인터
- #CallByAddress
- #3차원배열
- html5 new tag
- docker example
- html charset
- #C++ 연산자함수오버로딩
- html video
- #bubbleSort
- html id
- 토큰경제
- html plug-in
- html multimedia
- #1차원배열
- #JAVASCRIPT
- #성적관리프로그램
- 하이퍼레저패브릭
- #android activity
- #C++ has~a
- html object
- #2차원배열
- #binary
- mac terminal command
- border-box
- html youtube
- hyperledger transaction
- #자바상속#자바이즈어#is~a
- Today
- Total
A sentimental robot
Multi Chatting 본문
package Network;
import java.util.*;
import java.io.*;
import java.net.*;
public class Multi {
HashMap<String, DataOutputStream> client;
Multi() {
client = new HashMap();
Collections.synchronizedMap(client); // wrapping for concurrent access to the map
}
public static void main(String[] args) {
new Multi().start();
} // main
public void start() {
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(10001);
System.out.println("서버가 시작되었습니다.");
while (true) {
socket = serverSocket.accept(); // 클라이언트소켓를 서버소켓과 연결한 후,
System.out.println("[" + socket.getInetAddress() + ":" + socket.getPort() + "]" + "에서 접속하였습니다.");
ServerReceiver thread = new ServerReceiver(socket); // 연결된 클라이언트와 1:1 매칭
thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
} // main. start()
class ServerReceiver extends Thread {
Socket socket;
DataInputStream in;
DataOutputStream out;
public ServerReceiver(Socket socket) {
this.socket = socket;
try {
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
String name = "";
try {
name = in.readUTF();
sendToAll("#" + name + "님이 들어오셨습니다.");
// 처음 한 명이 채팅에 접속했을 땐 client map에 아무도 없기 때문에 안뜸
client.put(name, out);
System.out.println("현재 서버접속사 수는 " + client.size() + "입니다.");
while (in != null) {
sendToAll(in.readUTF());
}
} catch (IOException e) {
// 클라이언트가 종료되어 입력스트림(in)이 null이 되면 while문을 빠져 나와 client 목록에서 해당 클라이언트를 제거
//e.printStackTrace();
} finally {
sendToAll("#" + name + "님이 나가셨습니다."); // 접속이 끊긴 본인 창에는 안뜸 in=null이기 때문에
client.remove(name);
System.out.println("[" + socket.getInetAddress() + ":" + socket.getPort() + "]" + "에서 접속을 종료하였습니다.");
System.out.println("현재 서버접속자 수는" + client.size() + "입니다.");
} // try block
} // run()
} // inner class serverReceiver
public void sendToAll(String msg) {
Iterator it = client.keySet().iterator();
while (it.hasNext()) {
try {
DataOutputStream out = client.get(it.next());
out.writeUTF(msg);
} catch (IOException e) {
e.printStackTrace();
}
} // while
} // sendToAll()
} // Multi class
------------------------------------------------------------------------------------------------------------------
package Network;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class MultiClient {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String serverIp = "127.0.0.1";
String id;
try {
Socket socket = new Socket(serverIp, 10001);
System.out.println("서버에 연결되었습니다.");
System.out.print("ID를 입력하세요 :");
id = sc.next();
ClientSender cs = new ClientSender(socket, id);
// Thread cs = new Thread(new ClientSender(socket, args[0]));
Thread r = new Thread(new ClientReceiver(socket));
cs.start();
r.start();
} catch (ConnectException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} // try
} // main
} // MultiClient class
class ClientSender extends Thread {
Socket socket;
DataOutputStream out;
String name;
ClientSender(Socket socket, String name) {
this.socket = socket;
this.name = name;
try {
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
} // ClientSender constructor
public void run() {
Scanner sc = new Scanner(System.in);
try {
if (out != null)
out.writeUTF(name);
while (out != null)
out.writeUTF("[" + name + "]" + sc.next());
} catch (IOException e) {
e.printStackTrace();
}
} // run()
} // Clientsender class
class ClientReceiver extends Thread {
Socket socket;
DataInputStream in;
ClientReceiver(Socket socket) {
this.socket = socket;
try {
in = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (in != null) {
try {
System.out.println(in.readUTF());
} catch (IOException e) {
e.printStackTrace();
}
}
} // run()
} // ClientReceiver class
'Java' 카테고리의 다른 글
다시 한번 Has~a (0) | 2018.10.03 |
---|---|
1:1 채팅 (0) | 2018.09.06 |
NullPointerException (0) | 2018.06.30 |
Platform.runLater( ) (0) | 2018.02.07 |
Chatting UI (0) | 2018.02.07 |