ݺߣ

ݺߣShare a Scribd company logo
     



All Rights Reserved
? γĿ
 C Java ϵͳ
 C ʹjava.netеӿڽ
   
 C ʹJavaһ̨̨֮
   лTCP/IPЭͨѶ
 C ΪJ2EEѧϰ׼
? ̵ĿľֱָӻӵͨЭ
  ͨѶ
  Ҫ⣬һ׼ȷĶλһ̨
  ̨һҵοɿЧ
  Ľݴ䡣TCP/IPЭIPҪ
  Ķλݴ·ɣIPַ
  ΨһȷInternetϵһ̨TCP
  ṩӦõĿɿĻǿɿݴ
  ƣ̵Ҫһ㲻Ҫ
  IPδݵ
? ĿǰΪеģǿͻ/
  C/Sṹͨ˫һΪ
  ȴͻӦͻ
  Ҫʱ롣
  ʼУ˿ڣһпͻ
  󣬾ͻһ߳Ӧÿ
  ͬʱԼ˿ڣʹ
  ĿͻҲܼʱõ
                                          
                                          

                Э        Э
     A                           B


192.168.0.118                  192.168.0.200
? IPÿ̨һΩһIP
  ַ
? IPַһ߼ַ
? ϵIPַȫΨһԣ
? 32λ4ֽڣõʮƵĸʽ
  ʾ磺192.168.0.200
? Ϊеݽͨţ
  Ĺ򡢱׼Լ(=+﷨+
  )
? ͬиԲͬЭ顣
?   ͨý顪ߡߡ
?   ͬ豸ͨáרá
?   ͬIJϵͳUnixWindows 
?   ͬӦû̶ƶ
?   ͬҵࡪʱʵʱ
?   ͶʺͻۡΡΡ
?   ûҵԡִĵ
?   ǻཻ֯γ˷dzӵϵͳӦû
? ϵṹʹЩòͬý
  ͬ豸ϵͳڲͬӦûʵֻ
  ԣҵһճϼ
  Ӫһ֡ռ䡱  κγ̵κ
  ԼκμֻҪռΪ
  򣬾ܹ沢չ
? ϵṹõǷֲ㷽
    Ѹӵ绥⻮Ϊɸ
  Сġһ⣬ڲͬԽ
? ڱʱֽΪܶСģ
  һ
? OSI(Open System
  Interconnection)οģͽIJ
  ܻͬΪ7
       Ӧò       Ӧ

       ʾ       ݱʾ

       Ự       ͨ

              ˵˵

              Ѱַ·

      ·      ʷ()

              ƴ
?   ͨʵĶԵȲ֮䲻ֱͨš
?   ֮ϸ
?   ϲʹ²ṩķ  Service user
?   ²ϲṩ  Service provider
? ԵȲʵ֮ͨš
? ²ϲṩʵͨײ
? Ӧò㣺Զ̵¼ЭTelnetļЭ
  FTP ıЭHTTPDNS
  ʼЭSMTPʾЭPOP3ȡ
? 㣺ЭTCPûݱЭ
  UDP
   TCPӵĿɿĴЭ顣
   UDPӵģɿĴЭ顣
? 㣺ЭIPInternetƱ
  ЭICMPInternetЭIGMP
? TCPTransfer Control Protocolļƣ
  һӵı֤ɿЭ顣
  ͨTCPЭ鴫䣬õһ˳
  ͷͽշijɶԵ
  socket֮뽨ӣԱ
  TCPЭĻϽͨţһ
  socketͨserver socketȴ
  ʱһsocketҪ
  ӣһsocketǾ
  Խ˫ݴ䣬˫Խ
  ͻղ
? TCPһӵЭ飬ܹṩ
  ̨֮Ŀɿ . HTTP
  FTPTelnetӦöҪֿɿͨ
  ͨ
? UDPUser Datagram Protocolļƣ
  һӵЭ飬ÿݱһ
  ϢԴַĿ
  ַκοܵ·
  Ŀĵأܷ񵽴ĿĵأĿ
  صʱԼݵȷԶDzܱ
  ֤
? UDPǴһ̨һ̨
  ΪݱĶݰЭ飬Э
  ֤ݱǷȷصĿĵء
  һӵЭ
? ǶЭ򵥱Ƚ
 C ʹUDPʱÿݱжĵ
   ַϢҪͷͽշ
   ӡ
 C TCPЭ飬һӵЭ飬
   socket֮ݴ֮ǰȻҪ
   ӣTCPжһӽʱ
C ʹUDPʱдСƵģÿ
  ݱ޶64KB֮ڡ
C TCPûⷽƣһӽ
  ˫socketͿ԰ͳһĸʽ
  ݡ
C UDPһɿЭ飬ͷ͵
  ݱһͬĴ򵽴շ
C TCPһɿЭ飬ȷշȫ
  ȷػȡͷ͵ȫ
? TCPͨмǿԶ
  ӣTelnetļ䣨FTP
  Ҫȵݱɿش䡣
? ֮UDP򵥣ҽҪ
  ļ໤ͨھ߿ɿԵ
  ɢϵͳclient/serverӦó
? Ȼ˱֤ɿTCPЭ飬Ϊʲô
  ҪǿɿıٱЭأ
? Ҫԭ
 C һǿɿĴҪ۵ģ
   ȷԵļȻռüĴʱ
   ĴTCPЧʲUDP
 C ӦвҪ֤ϸĴ
   ԣƵϵͳҪƵƵ
   ݾԵȷֻҪ֤ԾͿˣ
   ȻʹUDPһЩ
? ڻϴݶʶ
  ĿĵصIPַͶ˿ںšIPַ
  ʶϵļ˿ںָ
  ϵӦó
? ˿һֳṹһЩݽṹI/O
  Ӧóͨϵͳij˿ڽ
  binding󣬴㴫ö˿ڵݶӦĽ
  գӦ̷ݶͨö˿
? ˿һͱʶʾ˿ںš˿ںŸЭ
  أTCP/IPЭTCPUDPȫ
  ģ飬˸ԵĶ˿ںҲ໥˿
  ͨΪЭ˿(protocol port) ƶ˿ڡ
? ˿ʹһ16λʾķΧ0~65535
  1024µĶ˿ںűԤķ磺httpʹ
  80˿ڡ
? һ̨Ҫݵһ̨
  ȱĹ̳Ϊװ
? װǰضЭͷ


               


         Эͷ
? οģУԵȲЭ֮佻ϢԪͳΪ
  ЭݵԪ(PDUProtocol Data Unit)
? οģÿһ㶼Ҫһṩķ
? Ϊṩ²ϲPDUΪݷװ
  Ȼ뱾ͷβͷкݴ
  ĿϢ
? ϶µݽĹʵϾDzϷװĹ
  ̡Ŀĵغ¶ϵݽĹ̾DzϲĹ
  ̡ɴ˿֪·ϴݣʵ
  ˶㡰ŷ⡱
? ǣijһֻʶɶԵȲװġŷ⡱
  װڡŷ⡱ڲݽDzύ
  㣬㲻κδ
? TCP/IPԴ߼о滮
  (DARPA)һоƻʵ̨
  ໥ͨš
? TCP/IPѳΪInternetͨŵĹҵ
  ׼
? TCP/IPģͰ4Σ
 C   Ӧò
 C   
 C   
 C   ӿ
οģ   TCP/IPģ
7    Ӧò

6    ʾ       Ӧò

5    Ự

4           

3           

2   ·
              ӿ
1
? ͨjava.netе࣬javaܹʹ
  TCPUDPЭڻϽͨѶ
? Java ͨչеʽ/ӿ
  Ͻ/
  ֧TCP/IP
? Java֧TCPUDPЭ塣TCP
  Ŀɿʽ/UDPָ֧
  ġٵġԵݱģʽ
? URL(Uniform Resource Locator)ͳһ
  ԴλļƣʾInternetij
  һԴĵַͨURLǿԷ
  InternetϵĸԴ
  WWWFTPվ㡣ͨ
  URLϲӦļ
  Դ
? URLΪֱ۵һ綨λʹ
  URLǵϰߣ׼䣬
  Ӧʮֹ㷺ĿǰʹΪ
  㷺TCP/IPжURLĽ
  ҲЭһ׼ν
  ʹURḶҪ
  Э鱾̫˽⣬ҲȽ
  ԶDZȽϼ򵥵ġ
? һURL Ҫ֣
   C ЭʶHTTPFTPFile
   C Դ֣ļ˿ںţ
? 磺
? http://java.sun.com:80/docs/books/tutorial/index.
  html#DOWN
? URL
 C JavaУԴʾURLַURL
   URLʾһURLַURL
   þURLURLͲURL
? URL
  C 磺http://www.gamelan.com/pages/index..html
  C new
    URL("http://www.gamelan.com/pages/index.html");
  C URL gamelan = new
    URL("http://www.gamelan.com/pages/");
  C URL gamelanGames = new URL(gamelan,
    "game.html");
  C new URL("http", "www.gamelan.com",
    "/pages/index.html");
  C new URL("http", "www.gamelan.com", 80,
     "pages/index.network.html");
? URL
  C   ʧܣ
  C   try
  C   {
  C       URL myURL = new URL(. . .)
  C   } catch (MalformedURLException e)
  C   {
  C        . . .
  C       // exception handler code here
  C        . . .
  C   }
? μ Url1.java
 C   URLĸ
 C   getProtocol
 C   getHost
 C   getPort
 C   getFile
 C   getRef
? ΪURLʵʱػϢ
  openConnection( )һ
  URLConnection£
? url.openConnection()
? openConnection( ) ijʽ
? URLConnection openConnection( )
? URLأһ
  URLConnection
  IOException쳣
? URLConnectionǷԶԴԵһ
  ;ࡣ㽨Զ̷֮
  ӣڴ֮ǰ
  URLConnectionԶ̶ԡ
  ЩHTTPЭ淶岢ҽ
  HTTPЭURL
? μ UrlConnection1.java
 C URLURLConnectionϣHTTP
   ȡϢļ򵥳˵Ƿ
   õ
? μUrlConnection2.java
? μUrlConnection3.java
 C Ƚϸ΢
? μUrlConnection4.java
? ڴ绰ʼ
  InternetӣַǻInetAddress
  װǰ۵ʽIPַ
  ͸õַͨһIP
  ෢ãIPIPַ
  ⡣
? InetAddress ڲ˵ַ֡
? 
 C InetAddress ûԵĹ캯Ϊһ
   InetAddress󣬱һõĹ
   
 C factory methodһо̬
   һʵԼ
   InetAddress getLocalHost( )
   getByName( )ԼgetAllByName( )
   InetAddressʵ
C static InetAddress getLocalHost( )
C throws UnknownHostException
C static InetAddress getByName(String
  hostName)
C throws UnknownHostException
C static InetAddress[ ]
  getAllByName(String hostName)
C throws UnknownHostException
? getLocalHost( )InetAddress
  
? getByName( )һ
  InetAddress
? Щܽһ
  UnknownHostException쳣
? Internetϣһdz
  е¡getAllByName( )شһ
  ƷֽеַInetAddresses
  顣ڲܰƷֽһַʱ
  һUnknown HostException쳣
? μ InetAddress1.java
?   ʲôSocket?
?   ʹSocketͨŵĹ
?   ʹServerSocketSocketʵַ˺Ϳ
    ˵ Socketͨ
?   ʵֶַ֧ͻ
? ΪܹĿӦ
  ѧUnixƳһӦóͨЭ
  IJϵͳsocket(׽)socketij
  ֣ʹԱԺܷطTCP/IPӶ
  Ӧõij
? UnixӦƹ㣬׽ڱд
  õ˼ռ׽ֱ
  WindowsȲϵͳСJavaҲ
  ֱģ͡
? ʲôSocket?
 Socketϵ˫
 ͨѶĶ˵
ʹSocketͨŵĹ
? һ׽ְ󶨵һضĶ˿ڣͨ
  ׽ֵȴͼͻ

? ͻݷڵͶ˿ںŷ
ʹSocketͨŵĹ

? һ󡣲һ
  µİ󶨵ͬ˿ڵַ׽֡
? ͻͷͨд׽ֽͨѶ
? ʹServerSocketSocketʵַ
  ˺Ϳͻ˵ Socketͨ
? ʹServerSocketSocketʵַ˺Ϳͻ
  ˵ Socketͨ
  ܽ᣺
1) Socket
2) /
3/д
4) ر/
5) رSocket
? μ Tcp1.java
? μ EchoServer.java
? μ EchoClient.java
? ʵֶַ֧ͻͨ
? ˵ijҪΪÿһͻ
  ӵsocketһ̣߳ͬʱͨ
  ŵ⡣
? μ EchoServer1.java
? μ EchoServerThread.java
? μ EchoClient.java
? ʲôDatagram
 C ݱϴĶݰ ݱǷ
   ȷصĿĵأʱ䣬˳
   ݵȷԾûбϡ
? javaʹDatagram
  DatagramPacket
? DatagramSocketUDPЭʵ
  ͻSocket.
? send():ݱ
? receive(): ݱ
DatagramSocket();                         DatagramPacket(buf,len);


DatagramPacket(buf,len);            DatagramPacket(buf,len,address, port);

socket.receive(packet);                   socket.send(packet);

packet.getAddress();
packet.getPort();

DatagramPacket(buf,len,address, port);     DatagramPacket(buf,len);

socket.send(packet);                       socket.receive(packet);
?   μ   Udp1.java
?   μ   QuoteServer.java
?   μ   QuoteServerThread.java
?   μ   QuoteClient.java

More Related Content

[ʥ˼԰][Java SE]Network

  • 1. All Rights Reserved
  • 2. ? γĿ C Java ϵͳ C ʹjava.netеӿڽ C ʹJavaһ̨̨֮ лTCP/IPЭͨѶ C ΪJ2EEѧϰ׼
  • 3. ? ̵ĿľֱָӻӵͨЭ ͨѶ Ҫ⣬һ׼ȷĶλһ̨ ̨һҵοɿЧ Ľݴ䡣TCP/IPЭIPҪ Ķλݴ·ɣIPַ ΨһȷInternetϵһ̨TCP ṩӦõĿɿĻǿɿݴ ƣ̵Ҫһ㲻Ҫ IPδݵ
  • 4. ? ĿǰΪеģǿͻ/ C/Sṹͨ˫һΪ ȴͻӦͻ Ҫʱ롣 ʼУ˿ڣһпͻ 󣬾ͻһ߳Ӧÿ ͬʱԼ˿ڣʹ ĿͻҲܼʱõ
  • 5. Э Э A B 192.168.0.118 192.168.0.200
  • 6. ? IPÿ̨һΩһIP ַ ? IPַһ߼ַ ? ϵIPַȫΨһԣ ? 32λ4ֽڣõʮƵĸʽ ʾ磺192.168.0.200
  • 7. ? Ϊеݽͨţ Ĺ򡢱׼Լ(=+﷨+ ) ? ͬиԲͬЭ顣
  • 8. ? ͨý顪ߡߡ ? ͬ豸ͨáרá ? ͬIJϵͳUnixWindows ? ͬӦû̶ƶ ? ͬҵࡪʱʵʱ ? ͶʺͻۡΡΡ ? ûҵԡִĵ ? ǻཻ֯γ˷dzӵϵͳӦû
  • 9. ? ϵṹʹЩòͬý ͬ豸ϵͳڲͬӦûʵֻ ԣҵһճϼ Ӫһ֡ռ䡱 κγ̵κ ԼκμֻҪռΪ 򣬾ܹ沢չ ? ϵṹõǷֲ㷽 Ѹӵ绥⻮Ϊɸ Сġһ⣬ڲͬԽ ? ڱʱֽΪܶСģ һ
  • 10. ? OSI(Open System Interconnection)οģͽIJ ܻͬΪ7 Ӧò Ӧ ʾ ݱʾ Ự ͨ ˵˵ Ѱַ· · ʷ() ƴ
  • 11. ? ͨʵĶԵȲ֮䲻ֱͨš ? ֮ϸ ? ϲʹ²ṩķ Service user ? ²ϲṩ Service provider
  • 13. ? Ӧò㣺Զ̵¼ЭTelnetļЭ FTP ıЭHTTPDNS ʼЭSMTPʾЭPOP3ȡ ? 㣺ЭTCPûݱЭ UDP TCPӵĿɿĴЭ顣 UDPӵģɿĴЭ顣 ? 㣺ЭIPInternetƱ ЭICMPInternetЭIGMP
  • 14. ? TCPTransfer Control Protocolļƣ һӵı֤ɿЭ顣 ͨTCPЭ鴫䣬õһ˳ ͷͽշijɶԵ socket֮뽨ӣԱ TCPЭĻϽͨţһ socketͨserver socketȴ ʱһsocketҪ ӣһsocketǾ Խ˫ݴ䣬˫Խ ͻղ
  • 15. ? TCPһӵЭ飬ܹṩ ̨֮Ŀɿ . HTTP FTPTelnetӦöҪֿɿͨ ͨ
  • 16. ? UDPUser Datagram Protocolļƣ һӵЭ飬ÿݱһ ϢԴַĿ ַκοܵ· Ŀĵأܷ񵽴ĿĵأĿ صʱԼݵȷԶDzܱ ֤
  • 17. ? UDPǴһ̨һ̨ ΪݱĶݰЭ飬Э ֤ݱǷȷصĿĵء һӵЭ
  • 18. ? ǶЭ򵥱Ƚ C ʹUDPʱÿݱжĵ ַϢҪͷͽշ ӡ C TCPЭ飬һӵЭ飬 socket֮ݴ֮ǰȻҪ ӣTCPжһӽʱ
  • 19. C ʹUDPʱдСƵģÿ ݱ޶64KB֮ڡ C TCPûⷽƣһӽ ˫socketͿ԰ͳһĸʽ ݡ C UDPһɿЭ飬ͷ͵ ݱһͬĴ򵽴շ C TCPһɿЭ飬ȷշȫ ȷػȡͷ͵ȫ
  • 20. ? TCPͨмǿԶ ӣTelnetļ䣨FTP Ҫȵݱɿش䡣 ? ֮UDP򵥣ҽҪ ļ໤ͨھ߿ɿԵ ɢϵͳclient/serverӦó
  • 21. ? Ȼ˱֤ɿTCPЭ飬Ϊʲô ҪǿɿıٱЭأ
  • 22. ? Ҫԭ C һǿɿĴҪ۵ģ ȷԵļȻռüĴʱ ĴTCPЧʲUDP C ӦвҪ֤ϸĴ ԣƵϵͳҪƵƵ ݾԵȷֻҪ֤ԾͿˣ ȻʹUDPһЩ
  • 23. ? ڻϴݶʶ ĿĵصIPַͶ˿ںšIPַ ʶϵļ˿ںָ ϵӦó
  • 24. ? ˿һֳṹһЩݽṹI/O Ӧóͨϵͳij˿ڽ binding󣬴㴫ö˿ڵݶӦĽ գӦ̷ݶͨö˿ ? ˿һͱʶʾ˿ںš˿ںŸЭ أTCP/IPЭTCPUDPȫ ģ飬˸ԵĶ˿ںҲ໥˿ ͨΪЭ˿(protocol port) ƶ˿ڡ ? ˿ʹһ16λʾķΧ0~65535 1024µĶ˿ںűԤķ磺httpʹ 80˿ڡ
  • 25. ? һ̨Ҫݵһ̨ ȱĹ̳Ϊװ ? װǰضЭͷ Эͷ
  • 26. ? οģУԵȲЭ֮佻ϢԪͳΪ ЭݵԪ(PDUProtocol Data Unit) ? οģÿһ㶼Ҫһṩķ ? Ϊṩ²ϲPDUΪݷװ Ȼ뱾ͷβͷкݴ ĿϢ ? ϶µݽĹʵϾDzϷװĹ ̡Ŀĵغ¶ϵݽĹ̾DzϲĹ ̡ɴ˿֪·ϴݣʵ ˶㡰ŷ⡱ ? ǣijһֻʶɶԵȲװġŷ⡱ װڡŷ⡱ڲݽDzύ 㣬㲻κδ
  • 27. ? TCP/IPԴ߼о滮 (DARPA)һоƻʵ̨ ໥ͨš ? TCP/IPѳΪInternetͨŵĹҵ ׼ ? TCP/IPģͰ4Σ C Ӧò C C C ӿ
  • 28. οģ TCP/IPģ 7 Ӧò 6 ʾ Ӧò 5 Ự 4 3 2 · ӿ 1
  • 29. ? ͨjava.netе࣬javaܹʹ TCPUDPЭڻϽͨѶ ? Java ͨչеʽ/ӿ Ͻ/ ֧TCP/IP ? Java֧TCPUDPЭ塣TCP Ŀɿʽ/UDPָ֧ ġٵġԵݱģʽ
  • 30. ? URL(Uniform Resource Locator)ͳһ ԴλļƣʾInternetij һԴĵַͨURLǿԷ InternetϵĸԴ WWWFTPվ㡣ͨ URLϲӦļ Դ
  • 31. ? URLΪֱ۵һ綨λʹ URLǵϰߣ׼䣬 Ӧʮֹ㷺ĿǰʹΪ 㷺TCP/IPжURLĽ ҲЭһ׼ν ʹURḶҪ Э鱾̫˽⣬ҲȽ ԶDZȽϼ򵥵ġ
  • 32. ? һURL Ҫ֣ C ЭʶHTTPFTPFile C Դ֣ļ˿ںţ ? 磺 ? http://java.sun.com:80/docs/books/tutorial/index. html#DOWN
  • 33. ? URL C JavaУԴʾURLַURL URLʾһURLַURL þURLURLͲURL
  • 34. ? URL C 磺http://www.gamelan.com/pages/index..html C new URL("http://www.gamelan.com/pages/index.html"); C URL gamelan = new URL("http://www.gamelan.com/pages/"); C URL gamelanGames = new URL(gamelan, "game.html"); C new URL("http", "www.gamelan.com", "/pages/index.html"); C new URL("http", "www.gamelan.com", 80, "pages/index.network.html");
  • 35. ? URL C ʧܣ C try C { C URL myURL = new URL(. . .) C } catch (MalformedURLException e) C { C . . . C // exception handler code here C . . . C }
  • 36. ? μ Url1.java C URLĸ C getProtocol C getHost C getPort C getFile C getRef
  • 37. ? ΪURLʵʱػϢ openConnection( )һ URLConnection£ ? url.openConnection() ? openConnection( ) ijʽ ? URLConnection openConnection( ) ? URLأһ URLConnection IOException쳣
  • 38. ? URLConnectionǷԶԴԵһ ;ࡣ㽨Զ̷֮ ӣڴ֮ǰ URLConnectionԶ̶ԡ ЩHTTPЭ淶岢ҽ HTTPЭURL
  • 39. ? μ UrlConnection1.java C URLURLConnectionϣHTTP ȡϢļ򵥳˵Ƿ õ ? μUrlConnection2.java ? μUrlConnection3.java C Ƚϸ΢ ? μUrlConnection4.java
  • 40. ? ڴ绰ʼ InternetӣַǻInetAddress װǰ۵ʽIPַ ͸õַͨһIP ෢ãIPIPַ ⡣ ? InetAddress ڲ˵ַ֡
  • 41. ? C InetAddress ûԵĹ캯Ϊһ InetAddress󣬱һõĹ C factory methodһо̬ һʵԼ InetAddress getLocalHost( ) getByName( )ԼgetAllByName( ) InetAddressʵ
  • 42. C static InetAddress getLocalHost( ) C throws UnknownHostException C static InetAddress getByName(String hostName) C throws UnknownHostException C static InetAddress[ ] getAllByName(String hostName) C throws UnknownHostException
  • 43. ? getLocalHost( )InetAddress ? getByName( )һ InetAddress ? Щܽһ UnknownHostException쳣 ? Internetϣһdz е¡getAllByName( )شһ ƷֽеַInetAddresses 顣ڲܰƷֽһַʱ һUnknown HostException쳣
  • 45. ? ʲôSocket? ? ʹSocketͨŵĹ ? ʹServerSocketSocketʵַ˺Ϳ ˵ Socketͨ ? ʵֶַ֧ͻ
  • 46. ? ΪܹĿӦ ѧUnixƳһӦóͨЭ IJϵͳsocket(׽)socketij ֣ʹԱԺܷطTCP/IPӶ Ӧõij ? UnixӦƹ㣬׽ڱд õ˼ռ׽ֱ WindowsȲϵͳСJavaҲ ֱģ͡
  • 48. ʹSocketͨŵĹ ? һ׽ְ󶨵һضĶ˿ڣͨ ׽ֵȴͼͻ ? ͻݷڵͶ˿ںŷ
  • 49. ʹSocketͨŵĹ ? һ󡣲һ µİ󶨵ͬ˿ڵַ׽֡ ? ͻͷͨд׽ֽͨѶ
  • 50. ? ʹServerSocketSocketʵַ ˺Ϳͻ˵ Socketͨ
  • 51. ? ʹServerSocketSocketʵַ˺Ϳͻ ˵ Socketͨ ܽ᣺ 1) Socket 2) / 3/д 4) ر/ 5) رSocket
  • 52. ? μ Tcp1.java ? μ EchoServer.java ? μ EchoClient.java
  • 53. ? ʵֶַ֧ͻͨ ? ˵ijҪΪÿһͻ ӵsocketһ̣߳ͬʱͨ ŵ⡣ ? μ EchoServer1.java ? μ EchoServerThread.java ? μ EchoClient.java
  • 54. ? ʲôDatagram C ݱϴĶݰ ݱǷ ȷصĿĵأʱ䣬˳ ݵȷԾûбϡ ? javaʹDatagram DatagramPacket ? DatagramSocketUDPЭʵ ͻSocket. ? send():ݱ ? receive(): ݱ
  • 55. DatagramSocket(); DatagramPacket(buf,len); DatagramPacket(buf,len); DatagramPacket(buf,len,address, port); socket.receive(packet); socket.send(packet); packet.getAddress(); packet.getPort(); DatagramPacket(buf,len,address, port); DatagramPacket(buf,len); socket.send(packet); socket.receive(packet);
  • 56. ? μ Udp1.java ? μ QuoteServer.java ? μ QuoteServerThread.java ? μ QuoteClient.java