(十)网络编程学习总结

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

om om
ww ww
PDF-XChange
! Click to buy NOW w.docu-track.c
ds.receive(dp);//↔ᯩ⌅䱫ຎˈ⭘ᶕ᧕᭦þؑ’ byte[] sndbuf = new Date().toString().getBytes(); DatagramPacket snddp = new DatagramPacket(sndbuf,sndbuf.length,
dp.getAddress(),dp.getPort());//ᔪ・аሱᖰཆਁⲴþؑ’ ds.send(snddp); //ਁþؑ’ //⴨ᖃҾྲл˖ //dp.setData(new Date().toString.getBytes()); //ds.send(dp); } catch(Exception e) { e.printStackTrace(); } finally { if(ds != null)
System.out.println("Receive ok. . ."); System.out.println(new String(recdp.getData(),0,recdp.getLength())); //⴨ᖃҾྲл˖ //ds.receive(dp); //System.out.println(new String(dp.getDate(),0,dp.getLength())); Thread.sleep(1000); } } catch (Exception e) { e.printStackTrace(); } finally { if(ds != null)
PDF-XChange
! Click to buy NOW w.docu-track.c
om om
ww ww
PDF-XChange
! Click to buy NOW w.docu-track.c
PDF-XChange !
Click to buy NOW w.docu-track.c
ds.close(); } } } ഐѪˈUDP ᱟ䶎䶒ੁ䘎᧕Ⲵˈнਟ䶐Ⲵˈ⇿а⅑Ⲵਁ䘱ᡰ䎠Ҷ䐟⭡䜭ਟ㜭наṧˈҏਟ㜭ᡀ࣏ˈҏ ਟ㜭ཡ䍕DŽ
᧕лᶕˈᡁԜᶕᙫ㔃ал UDP Ⲵ㖁㔌㕆〻DŽ สҾ UDP Ⲵ㖁㔌㕆〻 ㊫˖DatagramSocket ањ∄௫ˈ⴨ᖃҾþ䛞ㆂÿⲴ࣏㜭 ᯩ⌅˖receive(DatagramPacket dp) ㊫˖DatagramPacket ањ∄௫ˈ⴨ᖃҾþؑÿⲴ䀂㢢 þ䛞ㆂÿާᴹ᭦ਁþؑÿⲴ࣏㜭ˈሩҾ᧕᭦ᶕ䈤ˈਚ䴰㾱ᨀ‫׋‬þؑ㓨ÿ˄byte[]˅઼䮯ᓖ˄length˅ˈ ᶕ᧕᭦޵ᇩ˗㘼㾱ਁࠪˈ䲔Ҷ䘉єṧཆˈ䘈ᗵ䴰㾱⸕䚃ሩᯩⲴþൠ൰ÿ˄IP˅઼þ䛞㕆ÿ˄PORT˅˗ 䇙ᡁԜᶕⴻањֻᆀ˖ Coding: Server ㄟ public class UPDDayTimeDemo {
om om
ww ww
PDF-XChange
! Click to buy NOW w.docu-track.c
PDF-XChange !
Click to buy NOW w.docu-track.c
JAVA 㖁㔌㕆〻(TCP/UDP)˖
‫ݸ‬ᶕҶ䀓ал TCP/UDP Ⲵ⴨‫ޣ‬⢩⛩DŽ TCP˖Transfer Control Protocol:Ր䗃᧗ࡦॿ䇞ˈᱟ䶒ੁ䘎᧕ˈਟ䶐ˈᆹ‫Ⲵޘ‬䘎᧕DŽ UDP˖User Datagram Protocol: ⭘ᡧᮠᦞᣕॿ䇞ˈᱟ䶎䶒ੁ䘎᧕Ⲵˈнਟ䶐Ⲵˈнᆹ‫Ⲵޘ‬䘎᧕DŽа 㡜⭘൘ྲ˖㿶仁Ր䗃ˈ㖁㔌⭥䈍ㅹDŽ ᡁԜ࠶ᔰᶕ䇘䇪алˈ䘉є⿽ॿ䇞DŽᖃ❦ˈᡁԜѫ㾱ᱟ‫ڊ‬ал JAVA Ⲵ TCP Ⲵ䇘䇪. аˊสҾ TCP ॿ䇞Ⲵ㖁㔌㕆〻DŽ ㊫˖ Socket ᆳᱟа⿽ᣭ䊑Ⲵ㖁㔌᮷ԦˈJAVA ሱ㻵Ҷ䘉њ㊫ˈቿ㭭ҶᓅቲⲴ㓶㢲᫽֌DŽᆳᨀ‫׋‬Ҷ єњᖸᗇ㾱Ⲵᯩ⌅˖
BufferedReader br = new BufferedReader(new InputStreamReader(inStream)); PrintWriter pw = new PrintWriter(outStream); String str = br.readLine();
om om
Байду номын сангаас
ww ww
PDF-XChange
! Click to buy NOW w.docu-track.c
Pw.println(str.toUpperCase()); Pw.flush(); }| }
PDF-XChange
! Click to buy NOW w.docu-track.c
CLIENT˖ Public class Client { Public static void main(String[] args) { Socket c = new Socket(“127.0.0.1”,9050) ; InputStream is = c.getInputStream(); OutputStream os = c.getOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); PrintWriter pw = new PrintWriter(os); Pw.println(“Hello”); Pw.flush(); //␵オ㕃ߢ४ String str = br.readLine(); System.out.println(str); } }
InetAddress.getLocalHost(),7631); while(true) {
ds.send(dp); System.out.println("Client send OK . . ."); byte[] recbuf = new byte[128]; DatagramPacket recdp = new DatagramPacket(recbuf,recbuf.length); ds.receive(recdp);
Socket s = ss.accept(); //䘋‫ޕ‬䱫ຎˈㅹᖵᇒᡧㄟ䘎᧕DŽ CLIENT˖ Socket c = new Socket(“127.0.0.1”,port); //⌘˖ ㄟਓਧаᇊ㾱о SERVER а㠤DŽ о ᴽ࣑ಘᔪ・Ҷ䘎᧕ˈ䙊䗷 IP ઼ PORT л䶒ˈᡁԜᶕ‫ڊ‬ањֻᆀᶕᦼᨑ JAVA Ⲵ TCP 㖁㔌㕆〻 ‫ڊ‬ањᴽ࣑ಘˈ᧕᭦ᇒᡧㄟਁࠪⲴᆇㅖѢˈ䖜ᦒᡀབྷ߉ѻਾ޽䘄എ㔉ᇒᡧㄟˈᒦ൘ᇒᡧ㓸ㄟкᱮ皮DŽ Coding: SERVER ㄟ Public class Server {
public static void main(String[] args) { // TODO Auto-generated method stub DatagramSocket ds = null; while(true) { try { ds = new DatagramSocket(7631);//‫ݸ‬ᔪ・ањþ䛞ㆂ’ byte[] buf = new byte[1];//⭘ᶕ᧕᭦ؑⲴ޵ᇩ DatagramPacket dp = new DatagramPacket(buf,buf.length);//ࡋᔪаሱÿؑ‘
䈳⭘ leaveGroup(group) ᯩ⌅ᶕ⿫ᔰཊ᫝㓴; ⧠൘ˈᡁԜᶕ‫ڊ‬ањㆰঅֻᆀ˖ public class MulticastSocketDemo {
public static void main(String[] args) throws IOException { MulticastSocket ms = new MulticastSocket(7789);//֯⭘ PORT ᶕࡋᔪањཊ᫝㓴 InetAddress ip = InetAddress.getByName("228.5.6.7");//ཊ᫝㓴Ⲵ IP ൘к䶒ᡰ䇢Ⲵ㤳ത޵DŽ ms.joinGroup(ip);//࣐‫ޕ‬ཊ᫝㓴 String msg = "Hello"; DatagramPacket dp = new DatagramPacket(msg.getBytes(),msg.length(),ip,7789); ms.send(dp);
ds.close(); } } } } Coding˖ Client ㄟ public class UDPClient { public static void main(String[] args) { DatagramSocket ds = null; try { ds = new DatagramSocket(); byte[] buf = new byte[1]; DatagramPacket dp = new DatagramPacket(buf,buf.length,
} } Class MyThread extends Thread {
Private Socket s; Public MyThread(Socket s) {
This.s = s; } Public void run() {
InputStream inStream = s.getInputStream(); OutputStream outStream = s.getOutputStream();
⧠൘ˈᡁԜ޽ⴻалਖཆєњ㊫˖ аˊMulticastSocket ㊫ˈ ᆳ㔗᢯Ҿ DatagramSocket ㊫DŽ
ཊ᫝ᮠᦞᣕ྇᧕ᆇ㊫⭘Ҿਁ䘱઼᧕᭦ IP ཊ᫝वDŽ MulticastSocket ᱟа⿽ (UDP) DatagramSocketˈᆳާᴹ࣐‫ ޕ‬Internet кަԆཊ᫝ѫᵪⲴ“㓴”Ⲵ䱴࣐࣏㜭DŽ ཊ᫝㓴䙊䗷 D ㊫ IP ൠ൰઼ḷ߶ UDP ㄟਓਧᤷᇊDŽD ㊫ IP ൠ൰൘ 224.0.0.0 ઼ 239.255.255.255 Ⲵ㤳ത޵˄वᤜє㘵˅DŽൠ൰ 224.0.0.0 㻛‫ˈ⮉؍‬нᓄ֯⭘DŽ ਟԕ䙊䗷俆‫⭘֯ݸ‬ᡰ䴰ㄟਓࡋᔪ MulticastSocketˈ❦ਾ䈳⭘ joinGroup(InetAddress groupAddr) ᯩ⌅ᶕ࣐‫ޕ‬ཊ᫝㓴˖
getInputStream():˖ᶕ㧧ᗇ㖁㔌᮷ԦѝⲴ䗃‫ޕ‬⍱ InputStream ሩ䊑 getOutputStream()˖ᶕ㧧ᗇ㖁㔌᮷ԦѝⲴ䗃ࠪ⍱ OutputStream ሩ䊑 ᴽ࣑ಘㄟ˖ ㊫˖ServerSocket ⭘ PORT ਧᶕᔪ・ањ ServetSocket ሩ‫ۿ‬DŽ ᯩ⌅˖ accept(); 䘄എањ Socket 㔉ᇒᡧㄟDŽᆳᱟањ䱫ຎᯩ⌅ˈⴤࡠᴹᇒᡧㄟ֯⭘ Socket оѻ ᔪ・Ҷ䘎᧕DŽ ྲ˖SERVER˖ ServerSocket ss = new ServerSocket(port);
⭡к䶒ਟԕⴻࠪˈJAVA ⲴสҾ TCP Ⲵ㖁㔌㕆〻䶎ᑨㆰঅॆˈн޽䴰㾱ᡁԜ৫㘳㲁ᓅቲⲴ㖁㔌ˈ TCP/IP ॿ䇞ㅹDŽਚ䴰㾱᤹➗䙊䗷 SOCKET 㧧ᗇ䗃‫ޕ‬䗃ࠪ⍱ሩ‫⭘޽ˈۿ‬ᡁԜᡰᆖⲴ I/O ⍱Ⲵ⸕䇶৫ ᫽֌ቡ OK ҶDŽਖཆˈᴽ࣑ㄟ㾱‫ڊ‬ᡀཊ㓯〻Ⲵˈቡ޽֯⭘ཊ㓯〻Ⲵ⴨‫⸕ޣ‬䇶৫ᆼᡀˈᡰԕ䈤ˈJAVA Ⲵ㖁㔌㕆〻ᵜ䓛нᱟᖸഠ䳮ˈնަ࣐кཊ㓯〻ˈI/O ⍱ˈ⴨ӂѻ䰤ቡՊᴹаᇊⲴ༽ᵲᓖҶDŽ ަᆳⲴֻᆀቡн޽ཊѮҶʽʽʽ
Public static void main(String[] args) { ServerSocket ss = new ServerScoket(9050);//ᤷᇊㄟਓਧ While(true) { Socket s = ss.accept();//ㅹᖵᇒᡧㄟ䘎᧕DŽ (new MyThread(s)).start(); }
//㧧ᗇഎᓄ byte[] buf = new byte[1024]; DatagramPacket recDP = new DatagramPacket(buf,buf.length); ms.receive(recDP);//᧕᭦ System.out.println(new String(recDP.getData(),0,recDP.getLength())); ms.leaveGroup(ip);//⿫ᔰཊ᫝㓴 } }
相关文档
最新文档