관리 메뉴

nkdk의 세상

자바 30차 내용 network 본문

My Programing/JAVA

자바 30차 내용 network

nkdk 2008. 3. 8. 01:25
네트워크를 시작하게 됐네요 한 1~2주 정도는 하겠죠?

tcp/udp 프로토콜에 대해서 배웠습니다.
tcp는 연결형 udp는 비연결형입니다.

유일한 ip를 가지고 있어야 한다. 내부 네트워크를 이요하여 특정
영역내에서만 수행될 수 있도록 한다.

Stream을 이용하면 하드웨어의 지식이 없더라도 자바의 제공하는
클래스로 얼마든지 프로그램을 짜는데 지장이 없다.

자바는 네트워크에 비춰서 만들어 주었기 때문에 수 많은 네트워크가
사용된다.

TCP(URL, RMI)에 대하여 UDP(Multicast) 에 대하여 배우겠습니다.

java.net 에 들어가서 Inet4Address 등의 클래스를 확인합니다.

import java.net.*;

public class NetTest1 {

public static void main(String[] args) {
InetAddress ia; // 주소를 얻기 위한 클래스
InetAddress []ia2; // 여러개의 주소를 얻기 위함
try {
ia = InetAddress.getByName("www.daum.net");
System.out.println(ia);
System.out.println(ia.getHostAddress());
System.out.println(ia.getHostName());

ia=InetAddress.getLocalHost(); // 나의 호스트 정보를 받아온다.
System.out.println("------------------------");
System.out.println(ia);
System.out.println(ia.getHostAddress());
System.out.println(ia.getHostName());
System.out.println("------------------------");

ia2=InetAddress.getAllByName("www.daum.net");
for(InetAddress aa:ia2){
System.out.println(aa.getHostAddress());
System.out.println(aa.getHostName());
}
} catch (UnknownHostException e) {
// TODO: handle exception
}

}

}

import java.net.*;
import java.io.*;

public class NetTest2 {

public static void main(String[] args) {
// get http://www.daum.net/index.html HTTP1/0
InetAddress ia;
PrintWriter out; // 보낼 때는 out
BufferedReader in; // 받을 때는 IN을 사용
Socket soc;
try {
ia = InetAddress.getByName("www.daum.net");
soc = new Socket(ia, 80);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(soc
.getOutputStream())));
out.println("get http://www.daum.net");
out.flush(); // out객체를 깨끗히 비운다.

// 서버에서 응답한 자료 읽기
in = new BufferedReader(new InputStreamReader(soc.getInputStream()));

while (true) {
String str = in.readLine();
if (str == null) break;
System.out.println(str);
}

in.close(); out.close(); soc.close();

} catch (Exception e) {
// TODO: handle exception
}
}
}

------------------------

import java.io.*;
import java.net.*;

public class NetTest3 {

public static void main(String[] args) {
ServerSocket ss;
//사용 중인 port 확인
for(int i=1;i<=65535;i++) {
try {
ss = new ServerSocket(i);
ss.close();
} catch (IOException e) {
System.out.println(i+ "포트 열린상태");
}
}
System.out.println("끝");
}
}

// 이렇게 3개를했네요 오츠까레