반응형
Java Socket 메시지 주고받기
예시 01 / Example 01)
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
|
package server;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MainServer {
public static void main(String[] args) {
try {
ServerSocket s_socket = new ServerSocket(8888);
Socket c_socket = s_socket.accpet();
OutputStream output_data = c_socket.getOutputStream();
String sendDataString = "Welcome to My Server.";
output_data.write(sendDataString.getBytes());
s_socket.close();
c_socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
cs |
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
|
package client;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class MainClient {
public static void main(String[] args) {
try {
Socket c_socket = new Socket("192.168.0.2", 8888);
InputStream input_data = c_socket.getInputStream();
byte[] receiveBuffer = new byte[100];
input_data.read(receiveBuffer);
System.out.println(new String(receiveBuffer));
c_socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
cs |
실행 방법 / 결과
1. 서버 실행
2. 클라이언트 실행
==========================================================
예시 02 / Example 02)
반응형
'공부 > Object-Oriented Design Pattern' 카테고리의 다른 글
[Proxy Pattern] 프록시 패턴이란 (0) | 2021.04.15 |
---|---|
[OODP] Singleton Pattern (싱글톤 패턴)이란 (0) | 2021.04.04 |
Java Socket Test Example (1) 자바 소켓 예시 (0) | 2021.04.03 |
Java RMI (Remote Method Invocation) (3) (1) | 2021.04.03 |
Java RMI (Remote Method Invocation) (2) (0) | 2021.04.03 |
댓글