일단 1:1 소켓 통신 예제에 대해 살펴보도록 하자.
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
public class ClientExample {
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket();
System.out.println("[연결 요청]");
socket.connect(new InetSocketAddress("localhost", 5001));
System.out.println("[연결 성공]");
} catch (IOException e) {
e.printStackTrace();
}
if (!socket.isClosed()) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerExample {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress("localhost", 5001));
while (true) {
System.out.println("[연결 기다림]");
Socket socket = serverSocket.accept();
InetSocketAddress isa = (InetSocketAddress) socket.getRemoteSocketAddress();
System.out.println("[연결 수락함] " + isa.getHostName());
}
} catch (IOException e) {
e.printStackTrace();
}
if (!serverSocket.isClosed()) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
public class ClientExample {
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket();
System.out.println("[연결 요청]");
socket.connect(new InetSocketAddress("localhost", 5001));
System.out.println("[연결 성공]");
byte[] bytes = null;
String message = null;
OutputStream os = socket.getOutputStream();
message = "Hello Server";
bytes = message.getBytes("UTF-8");
os.write(bytes);
os.flush();
System.out.println("[데이터 보내기 성공]");
InputStream is = socket.getInputStream();
bytes = new byte[100];
int readByteCount = is.read(bytes);
message = new String(bytes, 0, readByteCount, "UTF-8");
System.out.println("[데이터 받기 성공] " + message);
os.close(); is.close();
} catch (IOException e) {
e.printStackTrace();
}
if (!socket.isClosed()) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class ServerExample {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress("localhost", 5001));
while (true) {
System.out.println("[연결 기다림]");
Socket socket = serverSocket.accept();
InetSocketAddress isa = (InetSocketAddress) socket.getRemoteSocketAddress();
System.out.println("[연결 수락함] " + isa.getHostName());
byte[] bytes = null;
String message = null;
InputStream is = socket.getInputStream();
bytes = new byte[100];
int readByteCount = is.read(bytes); // blocking
message = new String(bytes, 0, readByteCount, "UTF-8");
System.out.println("[데이터 받기 성공] " + message);
OutputStream os = socket.getOutputStream();
message = "Hello Client";
bytes = message.getBytes("UTF-8");
os.write(bytes);
os.flush();
System.out.println("[데이터 보내기 성공]");
is.close(); os.close(); socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (!serverSocket.isClosed()) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
다음시간에는 멀티쓰레드의 개념에 대해 복습하고, 멀티쓰레드를 활용해 Java TCP 소켓 프로그래밍을 통한 간단한 채팅프로그램을 만들어보자 !
사실 만드는건 아니고 예제 분석하기 ㅎㅎ
둔한 붓이 총명함을 이긴다
이건 어떤 의미인가요 ?