반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- web 사진
- tensorflow
- bccard
- pycharm
- 자료구조
- web
- cudnn
- Keras
- KNeighborsClassifier
- 재귀함수
- CES 2O21 참여
- vscode
- web 용어
- 머신러닝
- 결합전문기관
- 대이터
- mglearn
- 데이터전문기관
- classification
- postorder
- java역사
- html
- CES 2O21 참가
- 웹 용어
- web 개발
- paragraph
- inorder
- broscoding
- C언어
- discrete_scatter
Archives
- Today
- Total
bro's coding
java.Network.EchoProgram(1:1) 본문
반응형
package step4;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServer {
public void go() throws IOException {
// 서버에 연결하여 통신하기 위해 Socket을 생성
ServerSocket serverSocket = null;
PrintWriter pw = null;
BufferedReader br = null;
String clientIp = null;
Socket socket = null;
try {
// socket 연결
serverSocket = new ServerSocket(7777);
System.out.println("**EchoServer 실행**");
// accept();
socket = serverSocket.accept();
// client IP
clientIp = socket.getInetAddress().toString();
// stream 생성
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream(), true);// true : auto flush(쓰자마자 바로 쏨)
while (true) {
String message = br.readLine();
if (message.equals("종료종료")) {
System.out.println("Echo프로그램을 종료합니다.");
break;
}
System.out.println(clientIp + " : " + message);
pw.println(message);
System.out.println(message + "를 " + clientIp + "로 보냄");
}
} finally {
if (socket != null) {
socket.close();
}
if (pw != null) {
pw.close();
}
if (br != null) {
br.close();
}
}
}
public static void main(String[] args) {
try {
new EchoServer().go();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package step4;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import step1.IP;
public class EchoClient {
public void go() throws IOException {
// 서버에 연결하여 통신하기 위해 Socket을 생성
Socket socket = null;
PrintWriter pw = null;
Scanner scanner = null;
BufferedReader br = null;
try {
scanner = new Scanner(System.in);
socket = new Socket(IP.LOCAL, 7777);
System.out.println("**EchoServer접속완료**");
// in/out put stream 생성
pw = new PrintWriter(socket.getOutputStream(), true);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
// 메세지 입력 받음
System.out.print("입력하거라: ");
String message = scanner.nextLine();
// 서버에 메세지 보냄
pw.println(message);
if (message.equals("종료종료")) {
System.out.println("Echo프로그램을 종료합니다.");
break;
}
System.out.println(message + "를 서버에게 보냄");
// 서버에게 돌려 받은 에코 메세지를 출력
System.out.println(br.readLine() + "from server");
}
} finally {
if (socket != null) {
socket.close();
}
if (pw != null) {
pw.close();
}
if (scanner != null) {
scanner.close();
}
if (br != null) {
br.close();
}
}
}
public static void main(String[] args) {
try {
new EchoClient().go();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package step1;
public interface IP {
//public static final 상수 : IP.LOCAL 로 접근
//나의 IP
String LOCAL="127.0.0.1";
//서버의 IP
String INST="221.150.136.4";
}
반응형
'[IT] > java' 카테고리의 다른 글
java.web.model1 (0) | 2021.04.12 |
---|---|
java.Synchronizaation(동기화) (0) | 2021.03.23 |
java.project.chatting program (0) | 2021.03.23 |
java.Network.Inner class/Nested class (0) | 2021.03.22 |
java.Network (0) | 2021.03.18 |
java.currentTimeMillis(유닉스 시간) (0) | 2021.03.18 |
java.Deamon Thread (0) | 2021.03.17 |
java.Tread.scheduling (0) | 2021.03.17 |
Comments