C h a p t e r 11
java.net.,,., (TCP/IP) (UDP/IP).,. 1 ISO OSI 7 1977 (ISO, International Standards Organization) (OSI, Open Systems Interconnection). 6 1983 X.200. OSI 7 [ 11-1] 7. 1 (Physical Layer), 2 (Data Link Layer), 3 (Network Layer), 4 (Transport Layer), 5 (Session Layer), 6 (Presentation Layer), 7 (Application Layer). 1, 2, 3, 4, 5, 6, 7., ( ) (path),, ( ). 472
2 (Internet) (Network of Networks). TCP/IP UDP/IP. TCP/IP,,. [ 11-2] 4.,,,. OSI 7 1, 2, 3, 4, 5, 6, 7., FDDI,, X.25, IP, ICMP, IGMP, ARP, RARP, TCP, UDP. HTTP, FTP, E-mail, Telnet, SMTP, DNS. (Client & Server) P2P(Peer-to-Peer).,. P2P. 473
[ 11-3]. ( ). (Header). (1+), (2) (1+) (2+1+). ( )., (Encapsulation). ( ). ( ),. (Decapsulation). ( ) ( ). 474
3 (Internet Adress), IP,. IP,. IP IP. IP 4 10.(), 32. 10 0 255, 256 8. 32.,., 211.102.111.012. 475
(Port). ( ),., HTTP (80), FTP (21), Telnet (23), SMTP tjqltm(25), ECHO (7). TCP/IP 16, 1~65,535. 0~1,024, 1025~65,535. (Socket),.,.., MS-Windows 3.1 MS-Windows 98/NT/XP.,.. TCP(Transmission Control Protocol) UDP(User Datagram Protocol), java.net.. [ 11-4]. 476
4 (End Hosts). TCP(Transmission Control Protocol) UDP(User Datagram Protocol). TCP (Connection-Oriented). UDP (Connectionless). 477
5 [ 11-1] java.net. 478
1 [ 11-2]. 2 TCP [ 11-5]. java.net Serversocket. accept( ). 479
accept( ),.. OutputStream( ), InputStream( ).. OutputStream( ), InputStream( ).,.,. TCP [ 11-6]. 480
(TCP/IP) (UDP/IP). ().... < >I love JEJUDO!(client server)< >,! < >I love JEJUDO!(client server)< >., Welcom to connect to TCP Server!(server client).. 481
01 import java.net.; 02 import java.io.; 03 04 class Jv_11_1_TCPServer { 05 public static void main(string args[]) { 06 ServerSocket s1 = null; 07 Socket s2; 08 OutputStream os1; 09 DataOutputStream os2; 10 InputStream is1; 11 DataInputStream is2; 12 13 try { 14 s1 = new ServerSocket(5432); 15 } catch (IOException e) {e.printstacktrace();} 16 while(true) { 17 try { 18 System.out.println("..."); 19 s2 = s1.accept(); 20 System.out.println("!"); 482
21 22 os1 = s2.getoutputstream(); 23 os2 = new DataOutputStream(os1); 24 os2.writeutf("welcom to connect to TCP Server!(server -> client)"); 25 26 is1 = s2.getinputstream(); 27 is2 = new DataInputStream(is1); 28 String st = new String(is2.readUTF()); 29 System.out.println(st); 30 31 is1.close(); 32 is2.close(); 33 os1.close(); 34 os2.close(); 35 s2.close(); 36 } catch(ioexception e) {e.printstacktrace();} 37 } 38 39 } 40 } 483
01 import java.net.; 02 import java.io.; 03 04 class Jv_11_2_TCPClient { 05 public static void main(string args[]) { 06 try { 07 Socket s1; 08 InputStream is1; 09 DataInputStream is2; 10 OutputStream os1; 11 DataOutputStream os2; 484
12 13 String sendstring = "I love JEJUDO!(client -> server)"; 14 s1 = new Socket("127.0.0.1", 5432); 15 is1 = s1.getinputstream(); 16 is2 = new DataInputStream(is1); 17 String st = new String(is2.readUTF()); 18 System.out.println(st); 19 20 os1 = s1.getoutputstream(); 21 os2 = new DataOutputStream(os1); 22 os2.writeutf("< >" + sendstring + "< >"); 23 24 os2.close(); 25 os1.close(); 26 is2.close(); 27 is2.close(); 28 s1.close(); 29 } catch(connectexception connexc) { 30 System.err.println(" "); 31 } 32 catch(ioexception e) {e.printstacktrace();} 33 34 } 35 } 485
486
1 [ 11-3]. 2 UDP [ 11-11]. java.net DatagramSocket, DatagramPacket. TCP accept( ). java.net DatagramSocket, DatagramPacket. 487
send( ), receive( ). send( ), receive( ).. ()... 488
01 import java.net.; 02 import java.io.; 03 import java.lang.; 04 05 public class Jv_11_3_UDPServer { 06 public static void main(string args[]) { 07 try { 08 DatagramSocket ds = new DatagramSocket(9999); 09 byte[] bf = new byte[30]; 10 DatagramPacket dp = new DatagramPacket(bf, bf.length); 11 12 while(true) { 13 try { 489
14 ds.receive(dp); 15 String rs1 = new String(dp.getData()); 16 String rs2 = new String(rs1.trim()); 17 System.out.println(":" + dp.getaddress() + " :" + dp.getport()); 18 System.out.println(" (Client->Server): " + rs2); 19 } catch(ioexception e){} 20 } 21 } catch(ioexception e){} 22 } 23 } 490
01 import java.net.; 02 import java.io.; 03 public class Jv_11_4_UDPClient { 04 public static void main(string args[]) { 05 try { 06 DatagramSocket ds = new DatagramSocket(); 07 InetAddress ia = InetAddress.getByName("localhost"); 08 byte[] bf = args[0].getbytes(); 09 DatagramPacket dp = new DatagramPacket(bf, bf.length, ia, 9999); 10 11 ds.send(dp); 12 13 } catch(exception e){ 14 System.out.println(e); 15 } 16 } 17 } 491
1 InetAddress [ 11-4]. 2 URL [ 11-5], URL [ 11-6]. 492
3 URLConnection [ 11-7], URLConnection [ 11-8]. 493
URL, URLConnection, InetAddress ().., C:\source. 01 import java.net.; 02 03 class Jv_11_5 { 04 public static void main(string args[]) 05 throws Exception, MalformedURLException { 06 URL u = new URL(args[0]); 07 System.out.println("=== URL ==="); 08 System.out.println(":" + u.getprotocol()); 09 System.out.println(": " + u.getport()); 10 System.out.println(": " + u.gethost()); 11 System.out.println("(): " + u.getfile()); 12 System.out.println("URL: " + u.toexternalform()); 13 14 System.out.println("\n=== URLConnection ==="); 494
15 URLConnection uc = u.openconnection(); 16 int len = uc.getcontentlength(); 17 System.out.println(" : " + len + ""); 18 URL uu = uc.geturl(); 19 System.out.println("URL: " + uu); 20 21 System.out.println("\n=== InetAddress ==="); 22 InetAddress addr = null; 23 addr = InetAddress.getByName("www.bu.ac.kr"); 24 System.out.println(" IP"); 25 System.out.println(addr); 26 InetAddress Na[] = InetAddress.getAllByName("www.yonsei.ac.kr"); 27 System.out.println("www.yonsei.ac.kr "); 28 for (int i=0; i<na.length; i++) 29 System.out.println(Na[i]); 30 31 addr = InetAddress.getLocalHost(); 32 System.out.println(" : " + addr.gethostname()); 33 byte ip[] = addr.getaddress(); 34 System.out.println(" IP : "); 35 for (int i=0; i<ip.length; i++) { 36 if ( i > 0) { 37 System.out.print("."); 38 } 39 System.out.print(ip[i] & 0xff); 40 } 41 System.out.println("\n IP : " + addr.gethost Address()); 42 } 43 } 495
496
1 2 3 4 5 6 497
498
1 // file name: Jv_11_b1.java import java.io.; import java.util.; import public class Jv_11_b1 { public static void main(string[] args) { URL u = null; URLConnection uc = null; try { u = new URL("http://www.bu.ac.kr"); uc = } } System.out.println(" : " + uc.getcontenttype()); System.out.println(" : " + + " byte"); System.out.println(" : " + uc.getcontent Encoding ()); System.out.println(" : " + new Date( ); System.out.println(" : " + new Date (uc. getlastmodified())); }catch(malformedurlexception e) { System.out.println(e); }catch(ioexception e) { System.out.println(e); } 499
2 "" // file name: Jv_11_b2.java import java.net.; import java.io.; public class Jv_11_b2 { public static void main(string[] args) { URL u = null; try { u = new URL("http://java.sun.com:80"); }catch(malformedurlexception e) { System.out.println(e); System.exit(0); } System.out.println(" : " + ); System.out.println(" : " + ); System.out.println(" : " + ); System.out.println(" : " + u.getfile()); for(int i=15 ; i<25 ; i++) { try { Socket s = new ("localhost", i); 500
} System.out.println(" :" + i); s.close(); }catch(ioexception e) { System.out.println(" : " + i); } } } 3 501
4 502