JAVA之旅(三十三)_第1頁(yè)
JAVA之旅(三十三)_第2頁(yè)
JAVA之旅(三十三)_第3頁(yè)
JAVA之旅(三十三)_第4頁(yè)
JAVA之旅(三十三)_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、JAVA之旅(三十三)一.TCP說完UDP,我們就來(lái)說下我們應(yīng)該重點(diǎn)掌握的TCP了TCP傳輸 Socket和ServiceSocket建立客戶端和服務(wù)端建立連接后,通過Socket中的IO流進(jìn)行數(shù)據(jù)的傳輸關(guān)閉Socket同樣的,我們的客戶端和服務(wù)端都是兩個(gè)獨(dú)立的應(yīng)用我們通過查閱API文檔發(fā)現(xiàn),該對(duì)象在建立的時(shí)候,就可以去連接指定主機(jī),因?yàn)閠cp是面向連接的,所以在建立socket服務(wù)時(shí),就要有服務(wù)存在,并成功連接,形成通路后,在該通道進(jìn)行數(shù)據(jù)傳輸所以我們用代碼來(lái)看下他的步驟客戶端package com.lgl.hellojava;import java.io.IOException;impor

2、t java.io.InputStream;import java.io.OutputStream;import .ServerSocket;import .Socket;import .UnknownHostException;public class TcpClient public static void main(String args) try /1.創(chuàng)建客戶端的服務(wù),傳地址和端口 Socket s = new Socket("192.168.1.102",10000); /2.為了發(fā)送數(shù)據(jù),應(yīng)該獲得socket流中的輸出流 OutputStream out =

3、s.getOutputStream(); out.write("你好".getBytes(); s.close(); catch (UnknownHostException e) / TODO Auto-generated catch block e.printStackTrace(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); 服務(wù)端package com.lgl.hellojava;import java.io.IOException;import java.

4、io.InputStream;import .ServerSocket;import .Socket;/* * 定義端點(diǎn)接收數(shù)據(jù)打印出來(lái) * 服務(wù)端: * 1.建立服務(wù)端的socket服務(wù),servicesocket,并監(jiān)聽一個(gè)端口 * 2.獲取連接過來(lái)的客戶端對(duì)象,通過accept方法,這個(gè)方法是阻塞的,沒有連接就會(huì)等 * 3.客戶端如果發(fā)過來(lái)數(shù)據(jù),那么服務(wù)端要使用對(duì)應(yīng)的客戶端對(duì)象,并獲取到該對(duì)象的讀取流 * 4.關(guān)閉服務(wù)端(可選操作) * author LGL * */public class TcpService public static void main(String args)

5、try /1.建立連接,監(jiān)聽端口 ServerSocket ss = new ServerSocket(10000); /2.連接客戶端對(duì)象 Socket accept = ss.accept(); /獲取ip String ip = accept.getInetAddress().getHostAddress(); /3.獲取客戶端發(fā)送過來(lái)的數(shù)據(jù) InputStream in = accept.getInputStream(); /4.開始讀取 byte buf = new byte1024; int len = in.read(buf); System.out.println(new St

6、ring(buf,0,len); /5.關(guān)閉 ss.close(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); 二.TCP互相傳輸我們?cè)趤?lái)寫一個(gè)實(shí)例去說明,他們的互訪動(dòng)作,這里為了寫起來(lái)方便,就寫在一個(gè)類中了package com.lgl.hellojava;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import .ServerSocket;import .Soc

7、ket;import .UnknownHostException;/* * 客戶端發(fā)送信息,服務(wù)端收到,反饋信息 * * author LGL * */public class Tcp public static void main(String args) try Socket s = new Socket("192.168.1.102", 10005); OutputStream out = s.getOutputStream(); out.write("我是客戶端".getBytes(); InputStream in = s.getInputSt

8、ream(); byte buf = new byte1024; int len = in.read(buf); System.out.println(new String(buf, 0, len); s.close(); catch (UnknownHostException e) / TODO Auto-generated catch block e.printStackTrace(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); /* * 服務(wù)端 * author LGL * *

9、/class Server public static void main(String args) try ServerSocket ss = new ServerSocket(10005); Socket s = ss.accept(); InputStream in = s.getInputStream(); byte buf = new byte1024; int len = in.read(buf); System.out.println(new String(buf, 0, len); OutputStream out = s.getOutputStream(); out.writ

10、e("收到后反饋".getBytes(); s.close(); ss.close(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); 三.復(fù)制文件同樣的這里也是使用的流,我們具體來(lái)看下怎么去操作,我們同樣的,寫在一個(gè)類中package com.lgl.socket;import java.io.BufferedReader;import java.io.FileReader;import java.io.FileWriter;import java.io.IOExc

11、eption;import java.io.InputStreamReader;import java.io.PrintWriter;import .ServerSocket;import .Socket;import .UnknownHostException;public class FileClient public static void main(String args) try Socket s = new Socket("192.168.1.102", 10006); BufferedReader bufr = new BufferedReader(new F

12、ileReader("test.txt"); PrintWriter pw = new PrintWriter(s.getOutputStream(), true); String line = null; while (line = bufr.readLine() != null) pw.println(line); pw.print("over"); BufferedReader bufIn = new BufferedReader(new InputStreamReader( s.getInputStream(); String str = buf

13、In.readLine(); System.out.println(str); bufr.close(); s.close(); catch (UnknownHostException e) / TODO Auto-generated catch block e.printStackTrace(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); class FileServer public static void main(String args) try ServerSocket s

14、s = new ServerSocket(10006); Socket s = ss.accept(); BufferedReader bufIn = new BufferedReader(new InputStreamReader( s.getInputStream(); PrintWriter out = new PrintWriter(new FileWriter("test1.txt"), true); String line = null; while (line = bufIn.readLine() != null) if ("over".e

15、quals(line) break; out.println(line); PrintWriter pw = new PrintWriter(s.getOutputStream(), true); pw.println("上傳成功"); out.close(); s.close(); ss.close(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); 四.上傳圖片我們?cè)賮?lái)看下圖片是怎么上傳的,我們先來(lái)分析下步驟客戶端1.服務(wù)端點(diǎn)2.讀取客戶端已有的圖片數(shù)據(jù)3.通過s

16、ocket,發(fā)送給服務(wù)端4.讀取服務(wù)端反饋的信息5.關(guān)閉資源* * 客戶端 * * author LGL * */public class PicClient public static void main(String args) try Socket s = new Socket("192.168.1.102", 10009); FileInputStream fis = new FileInputStream("1.png"); OutputStream out = s.getOutputStream(); byte buf = new byte1

17、024; int len = 0; while (len = fis.read(buf) != -1) out.write(buf, 0, len); /告訴服務(wù)端數(shù)據(jù)寫完 s.shutdownInput(); InputStream in = s.getInputStream(); byte bufn = new byte1024; int num = in.read(bufn); System.out.println(new String(bufn, 0, num); fis.close(); s.close(); catch (UnknownHostException e) / TODO

18、 Auto-generated catch block e.printStackTrace(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); 服務(wù)端直接看代碼/* * 服務(wù)端 * author LGL * */class PicServer public static void main(String args) try ServerSocket ss = new ServerSocket(10009); Socket s = cept(); InputStream in = s.ge

19、tInputStream(); FileOutputStream fos = new FileOutputStream("2.png"); byte buf = new byte1024; int len = 0; while (len = in.read(buf) != -1) fos.write(buf, 0, len); OutputStream out = s.getOutputStream(); out.write("上傳成功".getBytes(); fos.close(); s.close(); ss.close(); catch (IOE

20、xception e) / TODO Auto-generated catch block e.printStackTrace(); 其實(shí)跟I/O區(qū)別真不大,但是概念一定要了解清楚五.多并發(fā)上傳多并發(fā)這個(gè)概念就是多人互動(dòng)了,這對(duì)服務(wù)器的負(fù)荷還是有考究的,這里呢,我們就模擬一下,多人上傳圖片的場(chǎng)景,我們是怎么做的?我們還是在上傳圖片的那份代碼上更改首先我們可以確定的是,這是服務(wù)端的代碼這個(gè)服務(wù)端有個(gè)局限性,當(dāng)A客戶端連接之后,被服務(wù)端獲取到,服務(wù)端就在執(zhí)行代碼了,這個(gè)時(shí)候如果B客戶端連接只有等待,這就是我們需要多并發(fā)的原因了,為了讓多個(gè)客戶端同時(shí)連接,服務(wù)端最好就是講每個(gè)客戶端封裝到一個(gè)單獨(dú)的線

21、程中,這樣就可以同時(shí)處理多個(gè)客戶端請(qǐng)求如何定義線程?只要明確了每個(gè)客戶端要在服務(wù)端執(zhí)行的代碼即可/* * 服務(wù)端 * * author LGL * */class PicServer public static void main(String args) try ServerSocket ss = new ServerSocket(10009); while (true) Socket s = ss.accept(); new Thread(new PicThread(s).start(); catch (IOException e) / TODO Auto-generated catch

22、block e.printStackTrace(); /* * 并發(fā)線程 * author LGL * */class PicThread implements Runnable private Socket s; public PicThread(Socket s) this.s = s; Override public void run() try String ip = s.getInetAddress().getHostAddress(); System.out.println("ip:" + ip); long millis = System.currentTim

23、eMillis(); File file = new File(millis + ".png"); InputStream in = s.getInputStream(); FileOutputStream fos = new FileOutputStream(file); byte buf = new byte1024; int len = 0; while (len = in.read(buf) != -1) fos.write(buf, 0, len); OutputStream out = s.getOutputStream(); out.write("上

24、傳成功".getBytes(); fos.close(); s.close(); catch (Exception e) throw new RuntimeException("上傳失敗"); 其實(shí)我寫的代碼還是有點(diǎn)爛的,但是思想在就好,我們得先把思想學(xué)會(huì)了六.多并發(fā)登錄上面說的多并發(fā)的上傳,實(shí)在服務(wù)端端,現(xiàn)在我們來(lái)說下登錄,是作用在客戶端package com.lgl.socket;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import j

25、ava.io.InputStreamReader;import java.io.PrintWriter;import .ServerSocket;import .Socket;import .UnknownHostException;public class LoginClient public static void main(String args) try Socket s = new Socket("192.168.1.102", 10008); BufferedReader bufr = new BufferedReader(new InputStreamRead

26、er( System.in); PrintWriter out = new PrintWriter(s.getOutputStream(), true); BufferedReader bufIn = new BufferedReader(new InputStreamReader( s.getInputStream(); for (int i = 0; i < 3; i+) String line = bufr.readLine(); if (line = null) break; out.println(line); String info = bufIn.readLine(); S

27、ystem.out.println("info:" + info); if (info.contains("歡迎") break; bufr.close(); s.close(); catch (UnknownHostException e) / TODO Auto-generated catch block e.printStackTrace(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); /* * 服務(wù)端 * * author LGL * */class LoginServer public static void main(String args) try ServerSocket ss = new ServerSocket(10008); while (true) Socket s = ss.accept(); new Thread(new UserThread(s).start(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); /*

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論