(十)网络编程学习总结
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 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);//ᔰཊ㓴 } }