_151_Java_基於Socket的TCP程式設計
阿新 • • 發佈:2018-12-13
-----------------
-------------------
--------------------
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import org.junit.Test; //客戶端向伺服器傳送資訊,服務端輸出資訊到控制檯上,服務端同時向客戶端傳送資訊 public class TestTCP2 { //客戶端 @Test public void client() { OutputStream os=null; Socket socket=null; InputStream inputStream =null; //utf-8:中文字元儲存是三個位元組 try { socket=new Socket(InetAddress.getByName("127.0.0.1"), 8989); os = socket.getOutputStream(); os.write("as的d客戶端jjff".getBytes()); //shutdownOutput:顯示告訴服務端已經接受完畢 socket.shutdownOutput(); inputStream = socket.getInputStream(); //utf-8:中文字元是三個位元組 //GBK、GB2312收編的漢字佔2個位元組 //實用的 Unicode 版本對應於 UCS-2,使用16位的編碼空間。也就是每個字元佔用2個字 //UCS-2 就改名成了 UTF-16。 byte[]b=new byte[6]; int len; //此處做一個簡單的測試(這裡是UTF-8編碼) //每次儲存4個位元組,然後檢測是否有中文被拆開,有拆開則需要補位元組 while((len=inputStream.read(b,0,4))!=-1) { boolean flag=false; if(b[3]<0) { int num=0; for(int i=1;i<3;i++) { if(b[i]>0) { num++; } } inputStream.read(b,4,num); len+=num; flag=true; System.out.println(len); } if(b[0]<0) { inputStream.read(b,4,2); len+=2; flag=true; System.out.println(len+" 補位元組"); } String string = new String(b,0,len); System.out.println(string); for(int i=0;i<len;i++) { System.out.println(b[i]); } if(flag) { b[4]=0; b[5]=0; } } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(inputStream!=null) { try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(os!=null) { try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(socket!=null) { try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } //服務端 @Test public void server() { ServerSocket serverSocket=null; Socket socket=null; InputStream inputStream=null; OutputStream outputStream = null; try { serverSocket = new ServerSocket(8989); socket = serverSocket.accept(); inputStream = socket.getInputStream(); //utf-8:中文字元是三個位元組 //GBK、GB2312收編的漢字佔2個位元組 //實用的 Unicode 版本對應於 UCS-2,使用16位的編碼空間。也就是每個字元佔用2個字 //UCS-2 就改名成了 UTF-16。 byte[]b=new byte[6]; int size=b.length; int len; //此處做一個簡單的測試(這裡是UTF-8編碼) //每次儲存4個位元組,然後檢測是否有中文被拆開,有拆開則需要補位元組 while((len=inputStream.read(b,0,4))!=-1) { boolean flag=false; if(b[3]<0) { int num=0; for(int i=1;i<3;i++) { if(b[i]>0) { num++; } } inputStream.read(b,4,num); len+=num; flag=true; System.out.println(len); } if(b[0]<0) { inputStream.read(b,4,2); len+=2; flag=true; System.out.println(len+" fjghjg"); } String string = new String(b,0,len); System.out.println(string); for(int i=0;i<len;i++) { System.out.println(b[i]); } if(flag) { b[4]=0; b[5]=0; } } outputStream = socket.getOutputStream(); outputStream.write("as的d客戶端jjff".getBytes()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(inputStream!=null) { try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(socket!=null) { try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(serverSocket!=null) { try { serverSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }