java中TCP傳輸協議


class TcpClient { public static void main(String[] args) throws Exception { //創建client的socket服務,指定目的主機和port Socket s = new Socket( "192.168.1.10", 10003); //為了發送數據。獲取socket流中的輸出流 java.io.OutputStream out = s.getOutputStream(); out.write( "hello tcp".getBytes()); s.close(); } } class TcpServer { public static void main(String[] args) throws Exception { //建立服務端socket服務,並監聽一個port ServerSocket ss = new ServerSocket( 10003); //通過accept方法獲取連接過來的client對象 Socket s = ss.accept(); String ip = s.getInetAddress().getHostAddress(); //獲取client發送過來的數據。使用client對象的讀取流來讀取數據 InputStream in = s.getInputStream(); byte[] buf = new byte[ 1024]; int len = in.read(buf); System. out.println( new String(buf, 0,len)); //關閉client s.close(); } } class TcpClient2 { public static void main(String[] args) throws Exception { //建立socket服務。指定連接的主機和port Socket s = new Socket( "192.168.1.10", 10004 ); //獲取socket流中的輸出流。將數據寫入該流,通過網絡傳送給服務端 OutputStream out = (OutputStream) s.getOutputStream(); out.write( "hello Tcp".getBytes()); //獲取socket流中的輸入流。將服務端反饋的數據獲取到,並打印 InputStream in = s.getInputStream(); byte[] buf = new byte[ 1024]; int len = in.read(buf); System. out.println( new String(buf, 0,len)); s.close(); } } class TcpServer2 { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket( 10004); Socket s = ss.accept(); //服務端收信息 InputStream in = s.getInputStream(); String ip = s.getInetAddress().getHostAddress(); byte[] buf = new byte[ 1024]; int len = in.read(buf); System. out.println( new String(buf, 0,len)); //服務端發信息 OutputStream out = (OutputStream) s.getOutputStream(); out.write( "have receive".getBytes()); s.close(); ss.close(); } } /*需求:建立一個文本轉換服務器 client給服務端發送文本,服務端會將文本轉成大寫再返還給client*/ class TcpClient3 { public static void main(String[] args) throws Exception { Socket s = new Socket( "192.168.1.4", 10005); //定義讀取鍵盤數據的流對象,讀取鍵盤錄入文本 BufferedReader bufr = new BufferedReader( new InputStreamReader(System. in)); //獲取socket流中的輸出流。通過網絡傳送到服務端 BufferedWriter bufOut = new BufferedWriter( new OutputStreamWriter(s.getOutputStream())); //定義一個socket讀取流,讀取服務端返回的大寫信息 BufferedReader bufIn = new BufferedReader( new InputStreamReader(s.getInputStream())); String line = null; while((line=bufr.readLine())!= null) { if( "over".equals(line)) break; bufOut.write(line); bufOut.newLine(); //結束標記 bufOut.flush(); String str = bufIn.readLine(); System. out.println( "server:"+str); } bufr.close(); s.close(); } } class TcpServer3 { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket( 10005); Socket s = ss.accept(); //讀取socket讀取流中的數據 BufferedReader bufIn = new BufferedReader( new InputStreamReader(s.getInputStream())); //socket輸出流,將大寫文本寫入到socket輸出流。並發送給client BufferedWriter bufOut = new BufferedWriter( new OutputStreamWriter(s.getOutputStream())); String line = null; while((line=bufIn.readLine())!= null) { bufOut.write(line.toUpperCase()); bufOut.newLine(); //結束標記 bufOut.flush(); } s.close(); ss.close(); } }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM