日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

TCP的网络编程

發布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TCP的网络编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package Internet;import org.junit.Test;import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket;/* 實現TCP網絡編程*/ public class TCPTest {// 客戶端@Testpublic void client() throws IOException {Socket socket = null;OutputStream os = null;try {InetAddress inet = InetAddress.getByName("192.168.1.104");socket = new Socket(inet,8899);os = socket.getOutputStream();os.write("你好呀我是客戶端gg".getBytes());} catch (IOException e) {e.printStackTrace();} finally {// 資源關閉if (os != null){os.close();}if (socket != null){socket.close();}}}// 服務端@Testpublic void server() throws IOException {ServerSocket ss = null;Socket socket = null;InputStream is = null;ByteArrayOutputStream baos = null;try {ss = new ServerSocket(8899);socket = ss.accept();is = socket.getInputStream();// 若構建的數組太小,可能會有亂碼 // byte[] buffer = new byte[1024]; // int len; // while ((len=is.read(buffer))!=-1){ // String str = new String(buffer,0,len); // System.out.println(str); // }// 在 baos中有一個可擴容的數組,會自動存取所有字符baos = new ByteArrayOutputStream();byte[] buffer = new byte[5];int len ;while ((len = is.read(buffer))!=-1){baos.write(buffer,0,len);}System.out.println(baos.toString());} catch (IOException e) {e.printStackTrace();} finally {if(baos!=null){baos.close();}if(is!=null){is.close();}if(socket!=null){socket.close();}if(ss!=null){ss.close();}}} }

二:

package Internet;import org.junit.Test;import java.io.*; import java.net.ServerSocket; import java.net.Socket;public class TCPTest2 {@Testpublic void client() throws IOException { // 1.Socket socket = new Socket("127.0.0.1",8080); // 2.OutputStream os = socket.getOutputStream(); // 3.FileInputStream fis = new FileInputStream(new File("1.jpg")); // 4.byte[] buffer = new byte[5];int len ;while ((len = fis.read(buffer))!=-1){os.write(buffer,0,len);} // 5.fis.close();os.close();socket.close();}@Testpublic void server() throws IOException {ServerSocket ss = new ServerSocket(8080);Socket socket = ss.accept();InputStream is = socket.getInputStream();FileOutputStream fos = new FileOutputStream(new File("2.jpg"));byte[] buffer = new byte[5];int len ;while ((len = is.read(buffer))!=-1){fos.write(buffer,0,len);}fos.close();is.close();socket.close();ss.close();} }

三:

package Internet;import org.junit.Test;import java.io.*; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket;/* 從客戶端發送文件給服務端,服務端保存到本地。并返回”發送成功“給客戶端*/ public class TCPTest3 {@Testpublic void client() throws IOException { // 1.Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),8080); // 2.OutputStream os = socket.getOutputStream(); // 3.FileInputStream fis = new FileInputStream(new File("1.jpg")); // 4.byte[] buffer = new byte[5];int len ;while ((len = fis.read(buffer))!=-1){os.write(buffer,0,len);} // 圖片傳完了不在輸出數據socket.shutdownOutput();// 5.接受服務器端的數據并顯示到控制臺上InputStream is = socket.getInputStream();ByteArrayOutputStream baos = new ByteArrayOutputStream();byte[] buffer1 = new byte[5];int len1 ;while ((len = is.read(buffer1))!=-1){baos.write(buffer1,0,len);}System.out.println(baos.toString());// 6,fis.close();os.close();socket.close();is.close();baos.close();}@Testpublic void server() throws IOException { // 1.ServerSocket ss = new ServerSocket(8080); // 2.Socket socket = ss.accept(); // 3.InputStream is = socket.getInputStream(); // 4.FileOutputStream fos = new FileOutputStream(new File("3.jpg")); // 5.byte[] buffer = new byte[5];int len ; // 由于read時阻塞式的方法,沒有明確告訴就不會結束循環。因此需要明確的指示結束(29行)while ((len = is.read(buffer))!=-1){fos.write(buffer,0,len);}// 6.服務器端給與客戶端反饋OutputStream os = socket.getOutputStream();// 字符需要轉換成字節流os.write("照片已收到!很漂亮!".getBytes());// 7.fos.close();is.close();socket.close();ss.close();} }

總結

以上是生活随笔為你收集整理的TCP的网络编程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。