国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當前位置: 首頁 > news >正文

深圳市建筑工程佛山seo外包平臺

深圳市建筑工程,佛山seo外包平臺,求職網(wǎng)站怎么做,wordpress首頁導航欄4. 綜合練習 練習一:多發(fā)多收 需求: 客戶端:多次發(fā)送數(shù)據(jù) 服務器:接收多次接收數(shù)據(jù),并打印 代碼示例: public class Client {public static void main(String[] args) throws IOException {//客戶端&…

4. 綜合練習

練習一:多發(fā)多收

需求:

客戶端:多次發(fā)送數(shù)據(jù)

服務器:接收多次接收數(shù)據(jù),并打印

代碼示例:

public class Client {public static void main(String[] args) throws IOException {//客戶端:多次發(fā)送數(shù)據(jù)//服務器:接收多次接收數(shù)據(jù),并打印
?//1. 創(chuàng)建Socket對象并連接服務端Socket socket = new Socket("127.0.0.1",10000);
?//2.寫出數(shù)據(jù)Scanner sc = new Scanner(System.in);OutputStream os = socket.getOutputStream();
?while (true) {System.out.println("請輸入您要發(fā)送的信息");String str = sc.nextLine();if("886".equals(str)){break;}os.write(str.getBytes());}//3.釋放資源socket.close();}
}
public class Server {public static void main(String[] args) throws IOException {//客戶端:多次發(fā)送數(shù)據(jù)//服務器:接收多次接收數(shù)據(jù),并打印
?//1.創(chuàng)建對象綁定10000端口ServerSocket ss = new ServerSocket(10000);
?//2.等待客戶端來連接Socket socket = ss.accept();
?//3.讀取數(shù)據(jù)InputStreamReader isr = new InputStreamReader(socket.getInputStream());int b;while ((b = isr.read()) != -1){System.out.print((char)b);}
?//4.釋放資源socket.close();ss.close();}
}

練習二:接收并反饋

  • 案例需求

    客戶端:發(fā)送數(shù)據(jù),接受服務器反饋

    服務器:收到消息后給出反饋

  • 案例分析

    • 客戶端創(chuàng)建對象,使用輸出流輸出數(shù)據(jù)

    • 服務端創(chuàng)建對象,使用輸入流接受數(shù)據(jù)

    • 服務端使用輸出流給出反饋數(shù)據(jù)

    • 客戶端使用輸入流接受反饋數(shù)據(jù)

  • 代碼實現(xiàn)

    // 客戶端
    public class ClientDemo {public static void main(String[] args) throws IOException {Socket socket = new Socket("127.0.0.1",10000);
    ?OutputStream os = socket.getOutputStream();os.write("hello".getBytes());// os.close();如果在這里關流,會導致整個socket都無法使用socket.shutdownOutput();//僅僅關閉輸出流.并寫一個結束標記,對socket沒有任何影響B(tài)ufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));String line;while((line = br.readLine())!=null){System.out.println(line);}br.close();os.close();socket.close();}
    }
    // 服務器
    public class ServerDemo {public static void main(String[] args) throws IOException {ServerSocket ss = new ServerSocket(10000);
    ?Socket accept = ss.accept();
    ?InputStream is = accept.getInputStream();int b;while((b = is.read())!=-1){System.out.println((char) b);}
    ?System.out.println("看看我執(zhí)行了嗎?");
    ?BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));bw.write("你誰啊?");bw.newLine();bw.flush();
    ?bw.close();is.close();accept.close();ss.close();}
    }

練習三:上傳練習(TCP協(xié)議)

  • 案例需求

    客戶端:數(shù)據(jù)來自于本地文件,接收服務器反饋

    服務器:接收到的數(shù)據(jù)寫入本地文件,給出反饋

  • 案例分析

    • 創(chuàng)建客戶端對象,創(chuàng)建輸入流對象指向文件,每讀一次數(shù)據(jù)就給服務器輸出一次數(shù)據(jù),輸出結束后使用shutdownOutput()方法告知服務端傳輸結束

    • 創(chuàng)建服務器對象,創(chuàng)建輸出流對象指向文件,每接受一次數(shù)據(jù)就使用輸出流輸出到文件中,傳輸結束后。使用輸出流給客戶端反饋信息

    • 客戶端接受服務端的回饋信息

  • 相關方法

    方法名說明
    void shutdownInput()將此套接字的輸入流放置在“流的末尾”
    void shutdownOutput()禁止用此套接字的輸出流
  • 代碼實現(xiàn)

    public class Client {public static void main(String[] args) throws IOException {//客戶端:將本地文件上傳到服務器。接收服務器的反饋。//服務器:接收客戶端上傳的文件,上傳完畢之后給出反饋。
    ?
    ?//1. 創(chuàng)建Socket對象,并連接服務器Socket socket = new Socket("127.0.0.1",10000);
    ?//2.讀取本地文件中的數(shù)據(jù),并寫到服務器當中BufferedInputStream bis = new BufferedInputStream(new FileInputStream("mysocketnet\\clientdir\\a.jpg"));BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());byte[] bytes = new byte[1024];int len;while ((len = bis.read(bytes)) != -1){bos.write(bytes,0,len);}
    ?//往服務器寫出結束標記socket.shutdownOutput();
    ?
    ?//3.接收服務器的回寫數(shù)據(jù)BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));String line = br.readLine();System.out.println(line);
    ?
    ?//4.釋放資源socket.close();
    ?}
    }
    public class Server {public static void main(String[] args) throws IOException {//客戶端:將本地文件上傳到服務器。接收服務器的反饋。//服務器:接收客戶端上傳的文件,上傳完畢之后給出反饋。
    ?
    ?//1.創(chuàng)建對象并綁定端口ServerSocket ss = new ServerSocket(10000);
    ?//2.等待客戶端來連接Socket socket = ss.accept();
    ?//3.讀取數(shù)據(jù)并保存到本地文件中BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("mysocketnet\\serverdir\\a.jpg"));int len;byte[] bytes = new byte[1024];while ((len = bis.read(bytes)) != -1){bos.write(bytes,0,len);}bos.close();//4.回寫數(shù)據(jù)BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));bw.write("上傳成功");bw.newLine();bw.flush();
    ?//5.釋放資源socket.close();ss.close();}
    }

練習四:文件名重復

 ```java
public class UUIDTest { public static void main(String[] args) { String str = UUID.randomUUID().toString().replace("-", ""); System.out.println(str);//9f15b8c356c54f55bfcb0ee3023fce8a } } ```public class Client {public static void main(String[] args) throws IOException {//客戶端:將本地文件上傳到服務器。接收服務器的反饋。//服務器:接收客戶端上傳的文件,上傳完畢之后給出反饋。
?
?//1. 創(chuàng)建Socket對象,并連接服務器Socket socket = new Socket("127.0.0.1",10000);
?//2.讀取本地文件中的數(shù)據(jù),并寫到服務器當中BufferedInputStream bis = new BufferedInputStream(new FileInputStream("mysocketnet\\clientdir\\a.jpg"));BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());byte[] bytes = new byte[1024];int len;while ((len = bis.read(bytes)) != -1){bos.write(bytes,0,len);}
?//往服務器寫出結束標記socket.shutdownOutput();
?
?//3.接收服務器的回寫數(shù)據(jù)BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));String line = br.readLine();System.out.println(line);
?
?//4.釋放資源socket.close();
?}
}
public class Server {public static void main(String[] args) throws IOException {//客戶端:將本地文件上傳到服務器。接收服務器的反饋。//服務器:接收客戶端上傳的文件,上傳完畢之后給出反饋。
?
?//1.創(chuàng)建對象并綁定端口ServerSocket ss = new ServerSocket(10000);
?//2.等待客戶端來連接Socket socket = ss.accept();
?//3.讀取數(shù)據(jù)并保存到本地文件中BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());String name = UUID.randomUUID().toString().replace("-", "");BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("mysocketnet\\serverdir\\" + name + ".jpg"));int len;byte[] bytes = new byte[1024];while ((len = bis.read(bytes)) != -1) {bos.write(bytes, 0, len);}bos.close();//4.回寫數(shù)據(jù)BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));bw.write("上傳成功");bw.newLine();bw.flush();
?//5.釋放資源socket.close();ss.close();}

http://m.aloenet.com.cn/news/41070.html

相關文章:

  • 網(wǎng)站開發(fā) 參考文獻seo網(wǎng)絡優(yōu)化招聘信息
  • wordpress 屏蔽白云百度seo公司
  • 微信怎么制作微電影網(wǎng)站深圳seo優(yōu)化seo優(yōu)化
  • 2018年網(wǎng)站建設免費拓客軟件
  • 滄州網(wǎng)站域名注冊服務公司seo網(wǎng)絡排名優(yōu)化技巧
  • 邢臺手機網(wǎng)站建設公司seo排名點擊軟件推薦
  • 視頻網(wǎng)站開發(fā)背景手機網(wǎng)站排名優(yōu)化
  • 基礎微網(wǎng)站開發(fā)口碑好seo基礎入門
  • 信譽好的武漢網(wǎng)站建設seo課培訓
  • 怎么使用vs2017做網(wǎng)站關鍵詞排名怎么快速上去
  • 百度做的網(wǎng)站字體侵權百度一下百度主頁官網(wǎng)
  • 爐石做任務抽獎網(wǎng)站windows優(yōu)化大師下載安裝
  • WordPress潮流媒體主題sem推廣和seo的區(qū)別
  • 溫州市手機網(wǎng)站制作班級優(yōu)化大師官方免費下載
  • wordpress欄目圖片seo上排名
  • wordpress獨立博客免費seo視頻教程
  • 哲學專業(yè)特色建設網(wǎng)站谷歌搜索廣告優(yōu)化
  • 畢業(yè)論文做cad圖的網(wǎng)站江蘇網(wǎng)頁設計
  • 網(wǎng)站投訴平臺寧波seo快速排名
  • WordPress可以做社交網(wǎng)站嘛網(wǎng)絡平臺怎么創(chuàng)建
  • 溫州做網(wǎng)站設計網(wǎng)絡營銷類型有哪些
  • 滑動 手機網(wǎng)站 代碼優(yōu)化人員配置
  • wordpress添加友情練級濟南seo網(wǎng)絡優(yōu)化公司
  • wordpress視頻站主題百度一下你就知道下載
  • 明年做哪個網(wǎng)站致富站長工具seo綜合查詢降級
  • 抖音短視頻代運營公司太原seo網(wǎng)絡優(yōu)化招聘網(wǎng)
  • 鄭州市熱點新聞優(yōu)化游戲卡頓的軟件
  • 公司網(wǎng)站制作 步驟seo優(yōu)化師是什么
  • 網(wǎng)站備案值得嗎常見的搜索引擎
  • 做機械設計的要知道哪些網(wǎng)站產(chǎn)品優(yōu)化是什么意思