====== Ch.19 네트워크 ======
=====네트워크 개요=====
=====네트워크 관련 클래스 =====
package chapter19;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressEx {
public static void main(String[] args) {
try {
// getByName메서드로 InetAddress 객체 생성
InetAddress ip = InetAddress.getByName("www.google.co.kr");
System.out.println("hostname:" + ip.getHostName());
System.out.println("ip :" + ip.getHostAddress());
// getAllByName메서드로 InetAddress 객체배열 생성
InetAddress[] ips =
InetAddress.getAllByName("www.google.co.kr");
for (InetAddress i : ips) {
System.out.println("ip 주소 :" + i);
}
// ip 주소값을 byte[] 배열로 리턴
byte[] ipAddr = ip.getAddress();
// byte 자료형의 표현 범위 : 128 ~ 127
// 127 이상의 값은 음수로 표현됨
for (byte b : ipAddr) {
System.out.print(((b < 0) ? b + 256 : b) + ".");
}
System.out.println();
// getLocalHost 메서드로 InetAddress 객체 생성 호출
InetAddress local = InetAddress.getLocalHost();
System.out.println("내컴퓨터 IP:" + local);
// getByAddress 메서드로 InetAddress 객체 생성 호출
InetAddress ip2 = InetAddress.getByAddress(ips[0].getAddress());
System.out.println(ips[0].getHostAddress() + " 주소 :" + ip2);
} catch (UnknownHostException e) {
System.out.println(e.getMessage());
}
}
}
package chapter19;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
public class URLEx {
public static void main(String[] args) {
try {
URL url = null;
url = new URL("https://www.egovframe.go.kr/EgovIntro.jsp?menu=1&submenu=1");
System.out.println("authority : " + url.getAuthority());
System.out.println("content : " + url.getContent());
System.out.println("protocol : " + url.getProtocol());
System.out .println("host : " + url.getHost());
System.out.println("port : " + url.getPort());
System.out.println("path : " + url.getPath());
System.out.println("file : " + url.getFile());
System.out.println("query : " + url.getQuery());
// URL 을 통해서 정보 받기
int data = 0;
try {
Reader is = new InputStreamReader(url.openStream());
while ((data = is.read()) != -1) {
System.out.print((char) data);
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
package chapter19;
import java.net.URL;
import java.net.URLConnection;
public class URLConnectionEx {
public static void main(String[] args) {
URL url = null;
String address =
"https://www.egovframe.go.kr/EgovIntro.jsp?menu=1&submenu=1";
try {
url = new URL(address);
URLConnection conn = url.openConnection();
System.out.println("conn.toString():" + conn);
System.out.println("getAllowUserInteraction():" + conn.getAllowUserInteraction());
System.out.println("getConnectTimeout():" + conn.getConnectTimeout());
System.out.println("getContent():" + conn.getContent());
System.out.println("getContentEncoding():" + conn.getContentEncoding());
System.out.println("getContentLengt h():" + conn.getContentLength());
System.out.println("getContentType():" + conn.getContentType());
System.out.println("getDate():" + conn.getDate());
System.out.println("getDefaultAllowUserInteraction():" +
conn.getDefaultAllowUserInteraction());
System.out.println("getDefaultUseCaches():" + conn.getDefaultUseCaches());
System.out.println("getDoInput():" + conn.getDoInput());
System.out.println("getDoOutput():" + conn.getDoOutput());
System.out.println("getExpiration():" + conn.getExpiration());
System.out.println("getHeaderFields():" + conn.getHeaderFields());
System.out.println("getIfModifiedSince():" + conn.getIfModifiedSince());
System.out.println("getLastModified():" + conn.getLastModified());
System.out.println("getReadTimeout():" + conn.getReadTimeout());
System.out.println("getURL():" + conn.getURL());
System.out.println("getUseCaches():" + conn.getUseCaches());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
package chapter19;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class URLConnectionEx2 {
public static void main(String[] args) {
URL url = null;
String address =
"https://www.egovframe.go.kr/EgovIntro.jsp?menu=1&submenu=1";
BufferedReader br = null;
String readline = "";
try {
url = new URL(address);
br = new BufferedReader(
new InputStreamReader(url.openStream()));
while ((readline = br.readLine()) != null) {
System.out.println(readline);
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try { br.close(); }catch(Exception e) {}
}
}
}
=====TCP 소켓 프로그래밍=====
package chapter19;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerEx {
public static void main(String[] args) {
// 소켓 생성
ServerSocket server = null;
try {
server = new ServerSocket(9999);
// 무한 반복 (클라이언트 접속 대기)
while(true) {
System.out.println("클라이언트 접속 대기");
Socket client = server.accept();
System.out.println("스레드 생성");
HttpThread ht = new HttpThread(client);
ht.start();
}
}catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
server.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
class HttpThread extends Thread {
private Socket client;
BufferedReader br;
PrintWriter pw;
HttpThread(Socket client) {
this.client = client;
try {
br = new BufferedReader (new InputStreamReader(client.getInputStream()));
pw = new PrintWriter(client.getOutputStream());
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
public void run() {
BufferedReader fbr = null;
DataOutputStream outToClient = null;
try {
String line = br.readLine();
//line : GET / HTTP/1.1
System.out.println("Http Header :"+line);
int start = line.indexOf("/") + 1;
int end = line.lastIndexOf("HTTP") - 1;
String fileName=line.substring(start,end);
if(fileName.equals("")) {
fileName="index.html";
}
System.out.println("사용자 요청 파일 :"+fileName);
fbr = new BufferedReader (new FileReader(fileName));
String fileLine = null;
pw.println("HTTP/1.0 200 Document Follows \r\n");
while((fileLine = fbr.readLine())!=null){
pw.println(fileLine);
pw.flush();
}
} catch(IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if(br != null) br.close();
if(pw != null) pw.close();
if(client != null) client.close();
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
}
}
=====UDP 소켓 프로그래밍=====
package chapter19;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerEx {
public static void main(String[] args) {
// 소켓 생성
ServerSocket server = null;
try {
server = new ServerSocket(9999);
// 무한 반복 (클라이언트 접속 대기)
while(true) {
System.out.println("클라이언트 접속 대기");
Socket client = server.accept();
System.out.println("스레드 생성");
HttpThread ht = new HttpThread(client);
ht.start();
}
}catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
server.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
class HttpThread extends Thread {
private Socket client;
BufferedReader br;
PrintWriter pw;
HttpThread(Socket client) {
this.client = client;
try {
br = new BufferedReader (new InputStreamReader(client.getInputStream()));
pw = new PrintWriter(client.getOutputStream());
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
public void run() {
BufferedReader fbr = null;
DataOutputStream outToClient = null;
try {
String line = br.readLine();
//line : GET / HTTP/1.1
System.out.println("Http Header :"+line);
int start = line.indexOf("/") + 1;
int end = line.lastIndexOf("HTTP") - 1;
String fileName=line.substring(start,end);
if(fileName.equals("")) {
fileName="index.html";
}
System.out.println("사용자 요청 파일 :"+fileName);
fbr = new BufferedReader (new FileReader(fileName));
String fileLine = null;
pw.println("HTTP/1.0 200 Document Follows \r\n");
while((fileLine = fbr.readLine())!=null){
pw.println(fileLine);
pw.flush();
}
} catch(IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if(br != null) br.close();
if(pw != null) pw.close();
if(client != null) client.close();
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
}
}
package chapter19;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerEx {
public static void main(String[] args) {
// 소켓 생성
ServerSocket server = null;
try {
server = new ServerSocket(9999);
// 무한 반복 (클라이언트 접속 대기)
while(true) {
System.out.println("클라이언트 접속 대기");
Socket client = server.accept();
System.out.println("스레드 생성");
HttpThread ht = new HttpThread(client);
ht.start();
}
}catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
server.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
class HttpThread extends Thread {
private Socket client;
BufferedReader br;
PrintWriter pw;
HttpThread(Socket client) {
this.client = client;
try {
br = new BufferedReader (new InputStreamReader(client.getInputStream()));
pw = new PrintWriter(client.getOutputStream());
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
public void run() {
BufferedReader fbr = null;
DataOutputStream outToClient = null;
try {
String line = br.readLine();
//line : GET / HTTP/1.1
System.out.println("Http Header :"+line);
int start = line.indexOf("/") + 1;
int end = line.lastIndexOf("HTTP") - 1;
String fileName=line.substring(start,end);
if(fileName.equals("")) {
fileName="index.html";
}
System.out.println("사용자 요청 파일 :"+fileName);
fbr = new BufferedReader (new FileReader(fileName));
String fileLine = null;
pw.println("HTTP/1.0 200 Document Follows \r\n");
while((fileLine = fbr.readLine())!=null){
pw.println(fileLine);
pw.flush();
}
} catch(IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if(br != null) br.close();
if(pw != null) pw.close();
if(client != null) client.close();
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
}
}