package space2;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
class SampleClient implements Runnable {
private InetAddress destination; // 목적지 정보
private int port; // 통신하기 위해 나가는 "개찰구 번호"
public void SampleClinet(InetAddress dest, int port) {
this.destination = dest;
this.port = port;
}
public void run() {
try {
Socket link = new Socket(destination, port);
System.out.println("Client : Connected!");
} catch (IOException e) {
// ConnectionException일 경우, 재시도를 하도록 만들기도 함
System.out.println(e);
}
}
}
class SampleServer implements Runnable {
private int port; // 나아게 통신을 시도한 컴퓨터가 사용하는 "개찰구 번호"를 기록하기 위한 목적
public void SampleServer(int port) {
this.port = port;
}
public void run() {
try {
ServerSocket link = new ServerSocket(port);
Socket linkToClient = link.accept();
System.out.println("Server : Connected!");
} catch (IOException e) {
System.out.println(e);
}
}
}
public class EX1 {
public static void main(String[] args) {
try {
Thread client = new Thread(new SampleClient(InetAddress.getLocalHost(), 8080));
Thread server = new Thread(new SampleServer(8080));
server.start();
client.start();
} catch (UnknownHostException e) {
System.out.println(e);
}
}
}