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();} }總結
- 上一篇: python封装DM达梦数据库-增删改查
- 下一篇: LaTeX数学符号表