android 裡使用Socket進行發送訊息案例

來源:互聯網
上載者:User

猜拳 源碼
http://www.eoeandroid.com/thread-114907-1-1.html

自己實現的Android曲線圖
http://www.eoeandroid.com/thread-207218-1-1.html

android遊戲物理引擎源碼
http://www.eoeandroid.com/thread-207445-1-1.html

socketServer類

package com.socket.server; import java.io.*;import java.net.*; import android.content.Context;import android.content.Intent; public class SocketServer { ServerSocket sever;Context context; public SocketServer(Context context, int port) {try {//執行個體化ServerSocket傳入連接埠號碼sever = new ServerSocket(port);this.context = context;} catch (IOException e) {e.printStackTrace();}} public void ControlActionAnswerMsg(String str) {final Intent intent = new Intent();intent.setAction("SOCKET_ACTION");intent.putExtra("message", str);context.sendBroadcast(intent);} public void beginListen() {//將socket監聽放入線程中,防止影響主線程操作new Thread() {public void run() {//因為是不斷監聽的過程,所以採用死迴圈while (true) {try {//這裡是做監聽操作final Socket socket = sever.accept(); BufferedReader in;try {//監聽到訊息後這裡讀取流資訊in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));//將流資訊放入PrintWriter中,然後讀出來即可PrintWriter out = new PrintWriter(socket.getOutputStream());while (!socket.isClosed()) {String str;//讀出字串str = in.readLine();out.println("Hello!world!! " + str);out.flush();if (str == null || str.equals("end"))break;System.out.println(str);ControlActionAnswerMsg(str);}socket.close();} catch (IOException e) {e.printStackTrace();}} catch (IOException e) {e.printStackTrace();}} }}.start();}}

ClientSocket類

package com.socket.pc;import java.io.*;import java.net.*;public class ClientSocket { static Socket client; public ClientSocket(String site, int port) {  try {   //執行個體化socket傳入ip和連接埠號碼,兩邊連接埠號碼要統一   client = new Socket(site, port);   System.out.println("Client is created! site:" + site + " port:"     + port);  } catch (UnknownHostException e) {   e.printStackTrace();  } catch (IOException e) {   e.printStackTrace();  } } public String sendMsg(String msg) {  try {   // BufferedReader in = new BufferedReader(new InputStreamReader(   // client.getInputStream()));   //將字串轉成輸出資料流然後發送出去   PrintWriter out = new PrintWriter(client.getOutputStream());   out.println(msg);   out.flush();   // return in.readLine();  } catch (IOException e) {   e.printStackTrace();  }  return ""; } public void closeSocket() {  try {   client.close();  } catch (IOException e) {   e.printStackTrace();  } }}

服務端與用戶端源碼

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.