티스토리 뷰
TCP 1 : 1 채팅
Class TcpChatServer
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 | import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class TcpChatServer { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; try { serverSocket = new ServerSocket(5000); socket = serverSocket.accept(); TcpSendThread sender = new TcpSendThread(socket); TcpReceiverThread receiver = new TcpReceiverThread(socket); Thread t1 = new Thread(sender); Thread t2 = new Thread(receiver); t1.start(); t2.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } | cs |
Class TcpChatClient
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 | import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class TcpChatClient { public static void main(String[] args) { Socket socket = null; String ip; String name; Scanner scan = new Scanner(System.in); System.out.println("ip를 입력하세요."); ip = scan.nextLine(); try { socket = new Socket(ip, 5000); TcpSendThread sender = new TcpSendThread(socket); TcpReceiverThread receiver = new TcpReceiverThread(socket); Thread t1 = new Thread(sender); Thread t2 = new Thread(receiver); t1.start(); t2.start(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } | cs |
Class TcpSenderThread
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 | import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.Scanner; public class TcpSendThread implements Runnable { Socket socket; BufferedWriter bw = null; String msg; String name; Scanner scan = new Scanner(System.in); public TcpSendThread(Socket socket) { this.socket = socket; } public void setName(String name) { this.name = name; } @Override public void run() { // TODO Auto-generated method stub try { bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); // System.out.println("이름을 입력하세요"); // name = scan.nextLine(); while (true) { msg = scan.nextLine(); bw.write(name + " : " + msg + "\n"); bw.flush(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } | cs |
Class TcpReceiverThread
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; public class TcpReceiverThread implements Runnable { Socket socket; BufferedReader br = null; String msg; String name; public TcpReceiverThread(Socket socket) { this.socket = socket; } @Override public void run() { // TODO Auto-generated method stub try { br = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (true) { msg = br.readLine(); System.out.println(msg); if (msg.equals("quit")) break; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } | cs |
'Programming > JAVA' 카테고리의 다른 글
Java Stream API 사용해보기 (407) | 2021.05.25 |
---|---|
Java NIO ByteBuffer 사용해보기 (0) | 2021.04.22 |
Immutable Object(불변 객체) (405) | 2021.04.06 |
String, StringBuffer, StringBuilder의 차이점이 뭘까? (0) | 2020.08.07 |
네이버 책 검색 OPEN API 활용하기 (820) | 2016.06.23 |
댓글