๐ป
Java ํ๋ก๊ทธ๋๋ฐ - ์ฑํ ํ๋ก๊ทธ๋จ ๋ณธ๋ฌธ
์ฑํ ํ๋ก๊ทธ๋จ
ServerReceiver.java
package network06.Chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;
public class ServerReceiver extends Thread{
private Socket socket;
public ServerReceiver(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()), 512);
while(true) {
String inputMsg = br.readLine();
if(inputMsg.equalsIgnoreCase("Q")) {
System.out.println("์ฑํ
๋ฐฉ์ ๋๊ฐ์
จ์ต๋๋ค.");
break;
}
System.out.println(inputMsg);
}
} catch (NullPointerException | SocketException e) {
System.out.println("์ฑํ
์ด ์ข
๋ฃ๋์์ต๋๋ค."); //ํด๋ผ์ด์ธํธ์์ socket close null ๋ฐํ
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(br!=null) br.close();
if(socket!=null) socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
ServerEx.java
package network06.Chat;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerEx {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(8989);
System.out.println("์๋ฒ ์ฑํ
์ค๋น์ค...");
Socket socket = serverSocket.accept();
ServerReceiver receiver = new ServerReceiver(socket);
receiver.start();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(serverSocket!=null) serverSocket.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
ClientSender.java
package network06.Chat;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class ClientSender extends Thread{
private Socket socket;
private String nickName;
public ClientSender(Socket socket, String nickName) {
this.socket = socket;
this.nickName = nickName;
}
@Override
public void run() {
BufferedWriter bw = null;
PrintWriter pw = null;
Scanner sc = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()), 512);
pw = new PrintWriter(bw);
sc = new Scanner(System.in);
while(true) {
String outputMsg = sc.nextLine();
if(outputMsg.equalsIgnoreCase("q")) {
pw.println(outputMsg);
pw.flush();
break;
}
pw.println(nickName + ">>>" + outputMsg);
pw.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(bw!=null) bw.close();
if(pw!=null) bw.close();
if(sc!=null) bw.close();
if(socket!=null) socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
ClientEx.java
package network06.Chat;
import java.net.Socket;
public class ClientEx {
public static void main(String[] args) {
if(args.length!=1) {
System.out.print("๋๋ค์ ํ๋๋ง ์
๋ ฅํ์ธ์:");
return;
}
try {
String nickName = args[0];
Socket socket = new Socket("๋ด ์์ดํผ ์ฃผ์", 8989);
ClientSender sender = new ClientSender(socket, nickName);
sender.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
network > chat ํด๋์ ์์ฑํ ์๋ฐํ์ผ์ ๋ณต์ฌํด์ ๋ถ์ฌ๋ฃ๊ธฐํ๋ค.
๋ฉ๋ชจ์ฅ์ผ๋ก ์ด๊ณ ๋ ํ ํจํค์ง๋ช ์ ์ง์ฐ๊ณ , ๋ค๋ฅธ์ด๋ฆ์ผ๋ก ์ ์ฅ > ansi๋ก ํ๊ณ ๋ชจ๋ ํ์ผ
C:\sohyunkim\java\network\chat
๋จ์ฒด ์ฑํ ํ๋ก๊ทธ๋จ
ClientEx.java
package network07.ChatMulti;
import java.net.Socket;
public class ClientEx {
public static void main(String[] args) {
try {
Socket socket = new Socket("192.168.0.23", 7575);
ClientSender sender = new ClientSender(socket, "haha");
sender.start();
ClientReceiver receiver = new ClientReceiver(socket);
receiver.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
ClientReceiver.java
package network07.ChatMulti;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;
public class ClientReceiver extends Thread{
private Socket socket;
public ClientReceiver(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()), 512);
while(true) {
String inputMsg = br.readLine();
if(inputMsg.equalsIgnoreCase("Q")) {
System.out.println("์ฑํ
๋ฐฉ์ ๋๊ฐ์
จ์ต๋๋ค.");
break;
}
System.out.println(inputMsg);
}
} catch (NullPointerException | SocketException e) {
System.out.println("์ฑํ
์ด ์ข
๋ฃ๋์์ต๋๋ค.");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(br!=null) br.close();
if(socket!=null) socket.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
ClientSender.java
package network07.ChatMulti;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class ClientSender extends Thread{
private Socket socket;
private String nickName;
public ClientSender(Socket socket, String nickName) {
this.socket = socket;
this.nickName = nickName;
System.out.println("์ฑํ
์์์
๋๋ค.");
}
@Override
public void run() {
BufferedWriter bw = null;
PrintWriter pw = null;
Scanner sc = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()), 512);
pw = new PrintWriter(bw);
sc = new Scanner(System.in);
while(true) {
String outputMsg = sc.nextLine();
if(outputMsg.equalsIgnoreCase("q")) {
pw.println(outputMsg);
pw.flush();
break;
}
pw.println(outputMsg);
pw.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(bw!=null) bw.close();
if(pw!=null) pw.close();
if(sc!=null) sc.close();
if(socket!=null) socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
ServerArr.java
package network07.ChatMulti;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ServerArr extends Thread{
private Socket socket;
public static List<PrintWriter> list = Collections.synchronizedList(new ArrayList<PrintWriter>());
private PrintWriter pw;
public ServerArr(Socket socket) { //[0]pw ํ๊ธธ๋/ [1]pw ๊น๊ธธ๋
this.socket = socket;
sendArr();
}
public void sendArr() {
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()), 152);
pw = new PrintWriter(bw);
list.add(pw);
} catch(Exception e) {
e.printStackTrace();
}
}
public void sendPrintAll(String str) {
for(PrintWriter pw:list) {
pw.println(str);
pw.flush();
}
}
@Override
public void run() {
BufferedReader br = null;
String nickName = null;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()), 512);
nickName = br.readLine();
sendPrintAll("#" + nickName + "๋์ด ๋ค์ด์ค์
จ์ต๋๋ค.");
while(true) {
String inputMsg = br.readLine();
if(inputMsg.equalsIgnoreCase("Q")) {
sendPrintAll("#" + nickName + "๋์ด ์ฑํ
๋ฐฉ์ ๋๊ฐ์ต๋๋ค.");
break;
}
sendPrintAll(nickName + ">>" + inputMsg);
}
} catch(SocketException | NullPointerException e) {
sendPrintAll("#" + nickName + "๋์ด ์ฑํ
๋ฐฉ์ ๋๊ฐ์ต๋๋ค.");
} catch (Exception e) {
}
}
}
ServerEx.java
package network07.ChatMulti;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerEx {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(7575);
System.out.println("์๋ฒ ์ฑํ
์ค๋น์ค...");
while(true) {
Socket socket = serverSocket.accept();
ServerArr arr = new ServerArr(socket);
arr.start();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(serverSocket!=null) serverSocket.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
cmd ์ฐฝ์์
javac ServerArr.java, javac ServerEx.java, java ServerEx(์๋ฒ์ธํ )
์ฌ๋ฌ๊ฐ์ cmd์ฐฝ์์
javac ClientReceiver.java , javac ClientSender.java, javac ClientEx.java, java ClienEx
๋ฐ์ํ
'KITRI > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Java ํ๋ก๊ทธ๋๋ฐ - jdbc ์ฐ๊ฒฐ (0) | 2020.05.25 |
---|---|
Java ํ๋ก๊ทธ๋๋ฐ - ๋คํธ์ํฌ ํต์ (0) | 2020.05.25 |
Java ํ๋ก๊ทธ๋๋ฐ - ์ค๋ ๋(Thread) (0) | 2020.05.20 |
Java ํ๋ก๊ทธ๋๋ฐ - byte ์ ์ถ๋ ฅ, char์ ์ถ๋ ฅ (0) | 2020.05.20 |
Java ํ๋ก๊ทธ๋๋ฐ - ์์ธ์ฒ๋ฆฌ(Exception) (0) | 2020.05.18 |
Comments