728x90
SMALL
- 클라이언트
- ClientApplication
/*
* 클라이언트
* */
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class ClientApplication {
private static final String IP = "localhost";
private static final int PORT = 12345;
public static void main(String[] args) {
try {
final Socket socket = new Socket(IP, PORT);
// 외부에서 수신받은 데이터를 화면에 출력하는 스레드
final ListenerThread listener = new ListenerThread(socket.getInputStream());
listener.start();
while(true) {
writeMessage(socket.getOutputStream());
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void writeMessage(OutputStream outputStream) throws IOException {
final Scanner scanner = new Scanner(System.in);
System.out.print("입력 > ");
final String messageToSend = scanner.nextLine();
final byte[] raw = messageToSend.getBytes(StandardCharsets.UTF_8);
outputStream.write(raw);
}
}
- ListenerThread
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class ListenerThread extends Thread {
private InputStream in;
public ListenerThread(InputStream in) {
this.in = in;
}
@Override
public void run() {
System.out.println("리스너 시작됨");
while (true) {
try {
byte[] bytes = new byte[4096];
int readCount;
while ((readCount = in.read(bytes)) != -1) {
final String message = new String(bytes, 0, readCount, StandardCharsets.UTF_8);
System.out.println(message);
}
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
}
- ServerApplication
/*
* 서버 애플리케이션
* */
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class ServerApplication {
private static final int PORT = 12345;
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("접속 대기 중");
final Socket socket = serverSocket.accept();
System.out.println("시작!");
final ListenerThread listener = new ListenerThread(socket.getInputStream());
listener.start();
// 블로킹
while (true) {
writeMessage(socket.getOutputStream());
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void writeMessage(OutputStream outputStream) throws IOException {
final Scanner scanner = new Scanner(System.in);
System.out.print("입력 > ");
final String messageToSend = scanner.nextLine();
final byte[] raw = messageToSend.getBytes(StandardCharsets.UTF_8);
outputStream.write(raw);
}
}
- 서버
- Application
import java.io.IOException;
import java.io.OutputStream;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
/**
* 서버 애플리케이션
*/
public class Application {
private static final int PORT = 12345;
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("접속 대기 중");
final Socket socket = serverSocket.accept();
System.out.println("시작!");
final ListenerThread listener = new ListenerThread(socket.getInputStream());
listener.start();
while (true) {
writeMessage(socket.getOutputStream());
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void writeMessage(OutputStream outputStream) throws IOException {
final Scanner scanner = new Scanner(System.in);
System.out.print("입력 > ");
final String messageToSend = scanner.next();
final byte[] raw = messageToSend.getBytes(StandardCharsets.UTF_8);
outputStream.write(raw);
}
}
- ListenerThread
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class ListenerThread extends Thread {
private InputStream in;
public ListenerThread(InputStream in) {
this.in = in;
}
@Override
public void run() {
System.out.println("리스너 시작됨");
while (true) {
try {
byte[] bytes = new byte[4096];
int readCount;
while ((readCount = in.read(bytes)) != -1) {
final String message = new String(bytes, 0, readCount, StandardCharsets.UTF_8);
System.out.println(message);
}
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
}
728x90
LIST
'JAVA > Java' 카테고리의 다른 글
20201123 _스프링 톰캣 맛보기 (0) | 2020.11.23 |
---|---|
20201120 이미지읽기 (0) | 2020.11.20 |
201106_32 상속 (0) | 2020.11.06 |
20201105_29 jdbc문제 및 해결 (0) | 2020.11.05 |
20201104_28 thread (0) | 2020.11.04 |
댓글