TCP傳輸:利用socket服務做一個文本轉換器,socket文本轉換器

來源:互聯網
上載者:User

TCP傳輸:利用socket服務做一個文本轉換器,socket文本轉換器

轉載請註明出處,謝謝:http://blog.csdn.net/harryweasley/article/details/45665291


最近看了一個教學視頻,學習socket編程,裡面有一個例子感覺寫的不錯,我就在此整理一下,協助我回憶,查看。


需求:文本轉換器,

用戶端給服務端發送一些字母,服務端會將文本轉換成大寫返回給用戶端。

並且用戶端可以無限制的進行文本轉換,當用戶端輸入“over”時,轉換結束。


用戶端:


服務端:



關於dos視窗,運行java,你可以看這個文章http://blog.csdn.net/harryweasley/article/details/45559129

分析:
用戶端:
既然是操作裝置上的資料,那麼就可以使用io技術,並按照io的操作規律來思考。
源:鍵盤錄入。
目的:網路裝置,網路輸出資料流。
而且操作的是文本資料。可以選擇字元流。


步驟
1,建立服務。
2,擷取鍵盤錄入。
3,將資料發給服務端。
4,擷取服務端返回的大寫資料。
5,結束,關資源。


都是文本資料,可以使用字元流進行操作,同時提高效率,加入緩衝。


下面是用戶端代碼:

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;public class TransClien {/** * @param args * @throws IOException * @throws UnknownHostException */public static void main(String[] args) throws Exception {//建立用戶端的socket,指定目的主機和連接埠Socket s = new Socket("192.168.1.48", 10005);//定義讀取鍵盤資料的流對象。BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));//定義目的,將資料寫入到socket輸出資料流。發給服務端。//BufferedWriter bufOut = //new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));PrintWriter out = new PrintWriter(s.getOutputStream(),true);//定義一個socket讀取流,讀取服務端返回的大寫資訊。BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));String line = null;while((line=bufr.readLine())!=null){if("over".equals(line))break;out.println(line);//bufOut.write(line);//bufOut.newLine();//bufOut.flush();String str =bufIn.readLine();System.out.println("server:"+str);}bufr.close();s.close();}}


服務端:
源:socket讀取流。
目的:socket輸出資料流。
都是文本,裝飾。


下面是服務端代碼:

import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;public class TransServer {/** * @param args */public static void main(String[] args) throws Exception {//建立socket服務,並監聽連接埠ServerSocket ss = new ServerSocket(10005);Socket s = ss.accept();String ip = s.getInetAddress().getHostAddress();System.out.println(ip + "....connected");// 讀取socket讀取流中的資料。BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));// 目的。socket輸出資料流。將大寫資料寫入到socket輸出資料流,並發送給用戶端。// BufferedWriter bufOut =// new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));PrintWriter out = new PrintWriter(s.getOutputStream(), true);String line = null;while ((line = bufIn.readLine()) != null) {System.out.println(line);out.println(line.toUpperCase());// bufOut.write(line.toUpperCase());// bufOut.newLine();// bufOut.flush();}s.close();ss.close();}}

注意:用戶端和服務端那裡注釋的4行代碼,是利用緩衝流做的,用PrintWriter步驟會少一些,這兩種方法都是可以的。


關於udp傳輸,你可以查看這篇文章http://blog.csdn.net/harryweasley/article/details/45665309


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.