1.開篇簡介
Socket本質上就是Java封裝了傳輸層上的TCP協議(註:UDP用的是DatagramSocket類)。要實現Socket的傳輸,需要構建用戶端和伺服器端。另外,傳輸的資料可以是字串和位元組。字串傳輸主要用於簡單的應用,比較複雜的應用(比如Java和C++進行通訊),往往需要構建自己的應用程式層規則(類似於應用程式層協議),並用位元組來傳輸。
2.基於字串傳輸的Socket案例
1)伺服器端代碼(基於控制台的應用程式,類比)
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;import java.util.List;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class Main { private static final int PORT = 9999; private List<Socket> mList = new ArrayList<Socket>(); private ServerSocket server = null; private ExecutorService mExecutorService = null; //thread pool public static void main(String[] args) { new Main(); } public Main() { try { server = new ServerSocket(PORT); mExecutorService = Executors.newCachedThreadPool(); //create a thread pool System.out.println("伺服器已啟動..."); Socket client = null; while(true) { client = server.accept(); //把用戶端放入用戶端集合中 mList.add(client); mExecutorService.execute(new Service(client)); //start a new thread to handle the connection } }catch (Exception e) { e.printStackTrace(); } } class Service implements Runnable { private Socket socket; private BufferedReader in = null; private String msg = ""; public Service(Socket socket) { this.socket = socket; try { in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //用戶端只要一連到伺服器,便向用戶端發送下面的資訊。 msg = "伺服器位址:" +this.socket.getInetAddress() + "come toal:" +mList.size()+"(伺服器發送)"; this.sendmsg(); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { try { while(true) { if((msg = in.readLine())!= null) { //當用戶端發送的資訊為:exit時,關閉串連 if(msg.equals("exit")) { System.out.println("ssssssss"); mList.remove(socket); in.close(); msg = "user:" + socket.getInetAddress() + "exit total:" + mList.size(); socket.close(); this.sendmsg(); break; //接收用戶端發過來的資訊msg,然後發送給用戶端。 } else { msg = socket.getInetAddress() + ":" + msg+"(伺服器發送)"; this.sendmsg(); } } } } catch (Exception e) { e.printStackTrace(); } } /** * 迴圈遍曆用戶端集合,給每個用戶端都發送資訊。 */ public void sendmsg() { System.out.println(msg); int num =mList.size(); for (int index = 0; index < num; index ++) { Socket mSocket = mList.get(index); PrintWriter pout = null; try { pout = new PrintWriter(new BufferedWriter( new OutputStreamWriter(mSocket.getOutputStream())),true); pout.println(msg); }catch (IOException e) { e.printStackTrace(); } } } } }
2)Android用戶端代碼
package com.amaker.socket;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.Socket;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class SocketDemo extends Activity implements Runnable { private TextView tv_msg = null; private EditText ed_msg = null; private Button btn_send = null; // private Button btn_login = null; private static final String HOST = "10.0.2.2"; private static final int PORT = 9999; private Socket socket = null; private BufferedReader in = null; private PrintWriter out = null; private String content = ""; //接收線程發送過來資訊,並用TextView顯示 public Handler mHandler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); tv_msg.setText(content); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv_msg = (TextView) findViewById(R.id.TextView); ed_msg = (EditText) findViewById(R.id.EditText01); btn_send = (Button) findViewById(R.id.Button02); try { socket = new Socket(HOST, PORT); in = new BufferedReader(new InputStreamReader(socket .getInputStream())); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter( socket.getOutputStream())), true); } catch (IOException ex) { ex.printStackTrace(); ShowDialog("login exception" + ex.getMessage()); } btn_send.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String msg = ed_msg.getText().toString(); if (socket.isConnected()) { if (!socket.isOutputShutdown()) { out.println(msg); } } } }); //啟動線程,接收伺服器發送過來的資料 new Thread(SocketDemo.this).start(); } /** * 如果串連出現異常,彈出AlertDialog! */ public void ShowDialog(String msg) { new AlertDialog.Builder(this).setTitle("notification").setMessage(msg) .setPositiveButton("ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); } /** * 讀取伺服器發來的資訊,並通過Handler發給UI線程 */ public void run() { try { while (true) { if (!socket.isClosed()) { if (socket.isConnected()) { if (!socket.isInputShutdown()) { if ((content = in.readLine()) != null) { content += "\n"; mHandler.sendMessage(mHandler.obtainMessage()); } else { } } } } } } catch (Exception e) { e.printStackTrace(); } }}
解析:除了isClose方法,Socket類還有一個isConnected方法來判斷Socket對象是否串連成功。 看到這個名字,也許讀者會產生誤解。 其實isConnected方法所判斷的並不是Socket對象的當前串連狀態, 而是Socket對象是否曾經串連成功過,如果成功串連過,即使現在isClose返回true, isConnected仍然返回true。因此,要判斷當前的Socket對象是否處於串連狀態, 必須同時使用isClose和isConnected方法, 即只有當isClose返回false,isConnected返回true的時候Socket對象才處於串連狀態。 雖然在大多數的時候可以直接使用Socket類或輸入輸出資料流的close方法關閉網路連接,但有時我們只希望關閉OutputStream或InputStream,而在關閉輸入輸出資料流的同時,並不關閉網路連接。這就需要用到Socket類的另外兩個方法:shutdownInput和shutdownOutput,這兩個方法只關閉相應的輸入、輸出資料流,而它們並沒有同時關閉網路連接的功能。和isClosed、isConnected方法一樣,Socket類也提供了兩個方法來判斷Socket對象的輸入、輸出資料流是否被關閉,這兩個方法是isInputShutdown()和isOutputShutdown()。 shutdownInput和shutdownOutput並不影響Socket對象的狀態。
2.基於位元組的傳輸
基於位元組傳輸的時候,只需要把相應的字串和整數等類型轉換為對應的網路位元組進行傳輸即可。具體關於如何把其轉換為網路位元組,請參《網路搜集:java整型數與網路位元組序的 byte[] 數群組轉換關係》。
PS:歡迎有志之士加入Android之家群:272022717. 這裡是技術交流、支援人員、思想匯聚、項目交流之地。