?
本文檔使用 PHP中文網(wǎng)手冊 發(fā)布
LocalSocket
?版本:Android 4.0 r1
?
結(jié)構(gòu)
繼承關(guān)系
public class LocalSocket extends Object
????????
java.lang.Object
android.net.LocalSocket
?
類概述
在UNIX域名空間內(nèi)創(chuàng)建一個socket(非服務(wù)器),這種類型的socket完全不同于java.net.socket。
?
構(gòu)造函數(shù)
public LocalSocket ()
???????? 創(chuàng)建一個AF_LOCAL/UNIX 套接字。
?
公共方法
public void bind (LocalSocketAddress bindpoint)
綁定socket到一個端口。僅可由還沒有被綁定的實(shí)體對象來調(diào)用該方法。
參數(shù)
bindpoint??? 端口地址
異常
IOException
?
public void close ()
關(guān)閉socket
異常
IOException
?
public void connect (LocalSocketAddress endpoint)
???????? 將當(dāng)前socket連接到一個端口,只有當(dāng)終端地址還沒有被連接的情況下才能調(diào)用該函數(shù)。
???????? 參數(shù)
endpoint???? 端口地址
???????? 異常
?????????????????? IOException???? 如果socket?是無效的或地址不存在。
?
public void connect (LocalSocketAddress endpoint, int timeout)
將當(dāng)前socket連接到一個端口。
異常
IOException???? 如果socket?是無效的或地址不存在。
?
public FileDescriptor[] getAncillaryFileDescriptors()
獲取一組由對端通過附加消息傳送過來的文件描述符。這個方法會返回最近發(fā)送的文件描述符,并且返回空直到收到一個新的描述符。文件描述符僅可以通過常規(guī)數(shù)據(jù)傳送,此方法讀取一個描述符后僅能返回非空值。
???????? 返回值
空或者文件描符數(shù)組
???????? 異常
IOException
?
public FileDescriptor getFileDescriptor ()
返回文件描述符,如果文件沒有打開或已關(guān)閉則返回空。
返回值
文件描述符或空值
?
public InputStream getInputStream ()
?????????獲取輸入流。
???????? 返回值
input stream對象
???????? 異常
?????????????????若socket已經(jīng)關(guān)閉或者還沒建立則拋出IO異常
public LocalSocketAddress getLocalSocketAddress ()
若存在則返回綁定的socket。
???????? 返回值
?????????????????? 本地socket地址,若匿名則返回空。
?
public OutputStream getOutputStream ()
獲取輸出流
???????? 返回值
輸出流
???????? 異常
?????????????????? 若socket已經(jīng)關(guān)閉或者還沒建立則拋出IO異常
?
public Credentials getPeerCredentials ()
獲取socket對的認(rèn)證信息。僅對已連接的套接字有效。
返回值
非空; 認(rèn)證信息
???????? 異常
IOException
?
public int getReceiveBufferSize ()
(譯者注:獲取接受緩沖區(qū)大小)
???????? 異常
IOException
?
public LocalSocketAddress getRemoteSocketAddress()
(譯者注:獲取socket連接端地址)
?
public int getSendBufferSize ()
(譯者注:獲取發(fā)送緩沖區(qū)大?。?/span>
???????? 異常
IOException
?
public int getSoTimeout ()
(譯者注:得到遠(yuǎn)端超時設(shè)置)
???????? 異常
IOException
?
public synchronized boolean isBound ()
(譯者注:是否已經(jīng)綁定)
?
public boolean isClosed ()
(譯者注:是否已經(jīng)關(guān)閉連接)
?
public synchronized boolean isConnected ()
(譯者注:是否已連接)
?
public boolean isInputShutdown ()
(譯者注:輸入流是否已關(guān)閉)
?
public boolean isOutputShutdown ()
(譯者注:輸出流是否已關(guān)閉)
?
public void setFileDescriptorsForSend(FileDescriptor[] fds)
將一組文件描述符放入隊(duì)列準(zhǔn)備發(fā)送到對端(socket),這些文件描述符在下次發(fā)送普通數(shù)據(jù)時會作為單個輔助消息一同發(fā)送出去,請查看桌面linux 系統(tǒng)的“main 7 unix” 的SCM_RIGHT。
參數(shù)
Fds??????????? 非空,待發(fā)送的文件描述符數(shù)組。
?
public void setReceiveBufferSize (int size)
(譯者注:設(shè)置接受緩沖區(qū)大?。?/span>
???????? 異常
IOException
?
public void setSendBufferSize (int n)
(譯者注:設(shè)置發(fā)送緩沖區(qū)大小)
???????? 異常
IOException
?
public void setSoTimeout (int n)
(譯者注:設(shè)置遠(yuǎn)端超時設(shè)置)
???????? 異常
IOException
?
public void shutdownInput ()
關(guān)閉輸入端socket
???異常
???IOException
?
public void shutdownOutput ()
關(guān)閉輸出端socket
異常
IOException
?
?
補(bǔ)充?
文章精選
??? [推薦]OPhone OS的網(wǎng)絡(luò)層架構(gòu)介紹
示例代碼
??? Android LocalSocket / LocalServerSocket sample code
Platforms:?Android SDK 1.0 (Eclipse 3.4.1 + ADT 0.8.0)
main.xml
???<Button android:id="@+id/send_1_button"
??? ????????android:layout_width="fill_parent" android:layout_height="wrap_content"
???????????android:text="send 1
request"/>
?
java文件:
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
*
* @author Denis Migol
*
*/
public class DemoActivity extends Activity {
???
??? public static StringSOCKET_ADDRESS = "your.local.socket.address";
???
??? // background threads use this Handler
to post messages to
??? // the main application thread
??? private final Handlerhandler = new Handler();
???
??? public class NotificationRunnable implements Runnable {
??????? private Stringmessage = null;
???????
??????? public void run() {
???????????if (message != null &&message.length() > 0) {
???????????????
showNotification(message);
???????????}
??????? }
???????
??????? /**
??????? * @param message the
message to set
??????? */
??????? public voidsetMessage(String message) {
???????????this.message =message;
??????? }
??? }
???
??? // post this to the Handler when the
background thread notifies
??? private final NotificationRunnablenotificationRunnable = new NotificationRunnable();
???
??? public voidshowNotification(String message) {
??????? Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
??? }
???
??? class SocketListener extends Thread {
??????? private Handlerhandler = null;
??????? private NotificationRunnablerunnable = null;
???????
??????? public SocketListener(Handlerhandler, NotificationRunnablerunnable) {
???????????this.handler =handler;
???????????this.runnable =runnable;
???????????this.handler.post(this.runnable);
??????? }
???????
??????? /**
??????? * Show UI
notification.
??????? * @param message
??????? */
??????? private voidshowMessage(String message) {
???????????this.runnable.setMessage(message);
???????????this.handler.post(this.runnable);
?????? ?}
???????
??????? @Override
??????? public void run() {
???????????//showMessage("DEMO: SocketListener started!");
???????????try {
???????????????LocalServerSocket server = new LocalServerSocket(SOCKET_ADDRESS);
???????????????while (true) {
?????? ?????????????LocalSocketreceiver = server.accept();
???????????????????if (receiver != null) {
???????????????????????InputStream input =receiver.getInputStream();
???????????????????????
???????????????????????// simply for java.util.ArrayList
???? ???????????????????intreaded = input.read();
???????????????????????int size = 0;
???????????????????????int capacity = 0;
???????????????????????byte[] bytes = new byte[capacity];
???????????????????????
???????????????????????// reading
?????????? ?????????????while (readed!= -1) {
???????????????????????????// java.util.ArrayList.Add(E e);
???????????????????????????
capacity = (capacity * 3)/2 + 1;
???????????????????????????//bytes = Arrays.copyOf(bytes, capacity);
???????????????????????????byte[] copy = new byte[capacity];
???????????????????????????System.arraycopy(bytes, 0, copy, 0, bytes.length);
???????????????????????????
bytes = copy;
???????????????????????????
bytes[size++] = (byte)readed;
???????????????????????????
?????????????
??????????????//
read next byte
???????????????????????????
readed = input.read();
???????????????????????}
???????????????????????
???????????????????????
showMessage(new String(bytes, 0, size));
???????????????????}
???????????????}
???????????} catch (IOException e) {
???????????????Log.e(getClass().getName(), e.getMessage());
???????????}
??????? }
??? }
???
??? public static voidwriteSocket(String message) throws IOException {
??????? LocalSocketsender = new LocalSocket();
??????? sender.connect(new LocalSocketAddress(SOCKET_ADDRESS));
??????? sender.getOutputStream().write(message.getBytes());
??????? sender.getOutputStream().close();
??? }
???
??? @Override
??? public voidonCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main);
???????
??????? new SocketListener(this.handler, this.notificationRunnable).start();
???????
??????? Button send1= (Button)findViewById(R.id.send_1_button);
??????? send1.setOnClickListener(new OnClickListener() {
???????????
???????????@Override
???????????public void onClick(View v) {
???????????????try {
???????????????????
writeSocket("hello");
???????????????} catch (IOException e) {
???????????????????Log.e(getClass().getName(), e.getMessage());
???????????????}
???????????}
???????????
??????? });
??? }
}
????????
?