Server
package socket;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
class MySession implements Runnable {
private Scanner sc;
private Socket so;
public MySession(Socket so) throws IOException {
this.so = so;
sc = new Scanner(so.getInputStream());
}
@Override
public void run() { // 접속한 클라이언트가 보낸 메시지를 내 컴퓨터에 출력하기
String msg = "";
while(true) {
msg = sc.nextLine(); // 클라이언트가 보낸 메시지를 읽어서
if("exit".equals(msg)) {
break;
}
System.out.println(msg); // 내 화면(서버)에 출력하기
}
sc.close();
try { so.close(); } catch(Exception e) {}
}
}
public class Ex03_Server {
public static void main(String[] args) throws Exception {
ServerSocket ss = null;
PrintWriter pw = null;
Socket so = null;
Scanner sc = new Scanner(System.in);
String msg = null;
ss = new ServerSocket(7777);
System.out.println("접속 대기중...");
so = ss.accept();
pw = new PrintWriter(so.getOutputStream());
MySession session = new MySession(so);
Thread th = new Thread(session);
th.start();
// 내가 입력한 메시지를 클라이언트에게 보내는 코드
while(true) {
System.out.print("보낼 메시지 입력 : ");
msg = sc.nextLine(); // 내가 키보드로 입력한 내용을
pw.println(msg); // pw는 so과 연결되어 있어서
pw.flush(); // 접속된 클라이언트에게 보내는 코드
if("exit".equals(msg)) {
break;
}
}
sc.close();
pw.close();
so.close();
ss.close();
}
}
Client
package socket;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
class ClientOutput implements Runnable {
private Socket so;
private PrintWriter pw;
private Scanner sc;
public ClientOutput(Socket so, Scanner sc) throws IOException {
this.so = so;
this.sc = sc;
pw = new PrintWriter(so.getOutputStream());
}
@Override
public void run() {
String msg = "";
while(true) {
System.out.print("보낼 메시지 입력 : ");
msg = sc.nextLine();
pw.println(msg);
pw.flush();
if("exit".equals(msg)) {
break;
}
}
pw.close();
try { so.close(); } catch(Exception e) {}
}
}
public class Ex03_Client {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
String host, msg;
int port = 7777;
Socket so;
Scanner server;
System.out.print("접속할 서버의 아이피 입력 : ");
host = sc.nextLine();
so = new Socket(host, port);
ClientOutput out = new ClientOutput(so, sc);
Thread th = new Thread(out);
th.start();
server = new Scanner(so.getInputStream());
while(true) {
msg = server.nextLine();
System.out.println(msg);
if(msg.equals("exit")) {
break;
}
}
server.close();
sc.close();
so.close();
}
}
'AWS CLOUD FRAMEWORK > Java' 카테고리의 다른 글
[Day19] multiChat (0) | 2023.04.08 |
---|---|
[Day18] Chat (0) | 2023.04.07 |
[Day18] Ex02 (0) | 2023.04.07 |
[Day18] Ex01 (0) | 2023.04.07 |
[Day18] Ex01_Server (0) | 2023.04.07 |