socket android php,LocalSocket

15914.htmlLocalSocket

版本:Android 4.0 r1

结构

继承关系

public class LocalSocket extends Object

java.lang.Object

android.net.LocalSocket

类概述

在UNIX域名空间内创建一个socket(非服务器),这种类型的socket完全不同于java.net.socket。

构造函数

public LocalSocket ()

创建一个AF_LOCAL/UNIX 套接字。

公共方法

public void bind (LocalSocketAddress

bindpoint)

绑定socket到一个端口。仅可由还没有被绑定的实体对象来调用该方法。

参数

bindpoint端口地址

异常

IOException

public void close ()

关闭socket

异常

IOException

public void connect (LocalSocketAddress

endpoint)

将当前socket连接到一个端口,只有当终端地址还没有被连接的情况下才能调用该函数。

参数

endpoint端口地址

异常

IOException     如果socket 是无效的或地址不存在。

public void connect (LocalSocketAddress

endpoint, int timeout)

将当前socket连接到一个端口。

异常

IOException     如果socket 是无效的或地址不存在。

public FileDescriptor[] getAncillaryFileDescriptors()

获取一组由对端通过附加消息传送过来的文件描述符。这个方法会返回最近发送的文件描述符,并且返回空直到收到一个新的描述符。文件描述符仅可以通过常规数据传送,此方法读取一个描述符后仅能返回非空值。

返回值

空或者文件描符数组

异常

IOException

public FileDescriptor getFileDescriptor ()

返回文件描述符,如果文件没有打开或已关闭则返回空。

返回值

文件描述符或空值

public InputStream getInputStream ()

获取输入流。

返回值

input stream对象

异常

若socket已经关闭或者还没建立则抛出IO异常

public LocalSocketAddress getLocalSocketAddress ()

若存在则返回绑定的socket。

返回值

本地socket地址,若匿名则返回空。

public OutputStream getOutputStream ()

获取输出流

返回值

输出流

异常

若socket已经关闭或者还没建立则抛出IO异常

public Credentials getPeerCredentials ()

获取socket对的认证信息。仅对已连接的套接字有效。

返回值

非空; 认证信息

异常

IOException

public int getReceiveBufferSize ()

(译者注:获取接受缓冲区大小)

异常

IOException

public LocalSocketAddress getRemoteSocketAddress()

(译者注:获取socket连接端地址)

public int getSendBufferSize ()

(译者注:获取发送缓冲区大小)

异常

IOException

public int getSoTimeout ()

(译者注:得到远端超时设置)

异常

IOException

public synchronized boolean isBound ()

(译者注:是否已经绑定)

public boolean isClosed ()

(译者注:是否已经关闭连接)

public synchronized boolean isConnected ()

(译者注:是否已连接)

public boolean isInputShutdown ()

(译者注:输入流是否已关闭)

public boolean isOutputShutdown ()

(译者注:输出流是否已关闭)

public void setFileDescriptorsForSend(FileDescriptor[] fds)

将一组文件描述符放入队列准备发送到对端(socket),这些文件描述符在下次发送普通数据时会作为单个辅助消息一同发送出去,请查看桌面linux 系统的“main 7 unix” 的SCM_RIGHT。

参数

Fds            非空,待发送的文件描述符数组。

public void setReceiveBufferSize (int size)

(译者注:设置接受缓冲区大小)

异常

IOException

public void setSendBufferSize (int n)

(译者注:设置发送缓冲区大小)

异常

IOException

public void setSoTimeout (int n)

(译者注:设置远端超时设置)

异常

IOException

public void shutdownInput ()

关闭输入端socket

异常

IOException

public void shutdownOutput ()

关闭输出端socket

异常

IOException

补充

文章精选

示例代码

Platforms:Android SDK1.0 (Eclipse 3.4.1 + ADT

0.8.0)

main.xml

request"/>

java文件:

importjava.io.IOException;importjava.io.InputStream;importandroid.app.Activity;importandroid.net.LocalServerSocket;importandroid.net.LocalSocket;importandroid.net.LocalSocketAddress;importandroid.os.Bundle;importandroid.os.Handler;importandroid.util.Log;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.Toast;/**** @author Denis Migol**/publicclassDemoActivityextendsActivity{publicstaticStringSOCKET_ADDRESS="your.local.socket.address";// background threads use this Handler

to post messages to// the main application threadprivatefinalHandlerhandler=newHandler();publicclassNotificationRunnableimplementsRunnable{privateStringmessage=null;publicvoidrun(){if(message!=null&&message.length()>0){showNotification(message);}}/*** @param message the

message to set*/publicvoidsetMessage(Stringmessage){this.message=message;}}// post this to the Handler when the

background thread notifiesprivatefinalNotificationRunnablenotificationRunnable=newNotificationRunnable();publicvoidshowNotification(Stringmessage){Toast.makeText(this,message,Toast.LENGTH_SHORT).show();}classSocketListenerextendsThread{privateHandlerhandler=null;privateNotificationRunnablerunnable=null;publicSocketListener(Handlerhandler,NotificationRunnablerunnable){this.handler=handler;this.runnable=runnable;this.handler.post(this.runnable);}/*** Show UI

notification.* @param message*/privatevoidshowMessage(Stringmessage){this.runnable.setMessage(message);this.handler.post(this.runnable);}@Overridepublicvoidrun(){//showMessage("DEMO: SocketListener started!");try{LocalServerSocketserver=newLocalServerSocket(SOCKET_ADDRESS);while(true){LocalSocketreceiver=server.accept();if(receiver!=null){InputStreaminput=receiver.getInputStream();// simply for java.util.ArrayListintreaded=input.read();intsize=0;intcapacity=0;byte[]bytes=newbyte[capacity];// readingwhile(readed!=-1){// java.util.ArrayList.Add(E e);capacity=(capacity*3)/2+1;//bytes = Arrays.copyOf(bytes, capacity);byte[]copy=newbyte[capacity];System.arraycopy(bytes,0,copy,0,bytes.length);bytes=copy;bytes[size++]=(byte)readed;//

read next bytereaded=input.read();}showMessage(newString(bytes,0,size));}}}catch(IOExceptione){Log.e(getClass().getName(),e.getMessage());}}}publicstaticvoidwriteSocket(Stringmessage)throwsIOException{LocalSocketsender=newLocalSocket();sender.connect(newLocalSocketAddress(SOCKET_ADDRESS));sender.getOutputStream().write(message.getBytes());sender.getOutputStream().close();}@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);newSocketListener(this.handler,this.notificationRunnable).start();Buttonsend1=(Button)findViewById(R.id.send_1_button);send1.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewv){try{writeSocket("hello");}catch(IOExceptione){Log.e(getClass().getName(),e.getMessage());}}});}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值