Blackberry手机客户端应用侦听代码

Blackberry主推推送技术,但何为推送技术,客户端对推送技术是怎样支持的?

 

推送的过程有应用服务发起,由BES服务器执行,数据通过BB专线,无线网最终达到黑莓手机终端,数据推送的目标终端以email地址或PIN决定。BES服务器与应用服务器或email服务器之间有轮询交互,BES服务器轮循应用服务器查看数据更新,如有更新,应用服务器会发起推送请求。

 

对于客户端应用来说,不存在轮循的操作。客户端应用只需侦听指定的端口,当数据到达时,客户端应用会被通知来处理到达的数据。以下是客户端实现侦听的范例代码:

 

class ListenerThread extends Thread {

 

        LISTEN_URL = "http://:911";

        public void run() {

 

            StreamConnectionNotifier notify = null;
            StreamConnection stream = null;
            InputStream input = null;
            try{//wait for the device to load
                sleep(1000);
           }
           catch(Exception e){}
            for(;;) {//in case an exception is thrown, re try to get the data
                    try {

                        notify = (StreamConnectionNotifier)Connector.open(LISTEN_URL);

                        for(;;){

                            //NOTE: the following will block until data is received
                            stream = notify.acceptAndOpen();
                            input = stream.openInputStream();

                            //Extract the data from the input stream
                            StringBuffer sb = new StringBuffer();
                            int datum = -1;
                            while ( -1 != (datum = input.read()) )
                            {
                                    sb.append((char)datum);
                            }
                            stream.close();
                            stream = null;

                            String contactData = sb.toString();
                            DataStore dataStore = new DataStore();
                            dataStore.saveData(contactData);

                            System.err.println("Push message received...("+contactData.length()+" bytes)/n");
                            System.err.println(contactData);

                            //triggering a notification event since data has been received
                            NotificationsManager.triggerImmediateEvent(ID_1,0,this,null);//notifing the device of new content being received
                        }

                    }
                    catch (IOException e){
                            //likely the stream was closed
                            System.err.println(e.toString());
                    }
                    finally {
                            try {
                                    if (stream != null){
                                            stream.close();
                                    }
                            }
                            catch (Exception ex){}
                            try{
                                    if (notify != null){
                                            notify.close();
                                    }
                            }
                            catch (Exception ex){}
                    }//end finally
            }//end for
        }//end run
    }//end listenerthread
}//end pushdatalistener

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用C#创建基于TCP协议的服务器和客户端的示例代码: 服务器端代码: ```csharp using System; using System.Net; using System.Net.Sockets; using System.Text; class Server { static void Main(string[] args) { TcpListener listener = new TcpListener(IPAddress.Any, 8888); // 创建TCP侦听器,监听本地8888端口 listener.Start(); // 启动侦听器 Console.WriteLine("等待客户端连接..."); while (true) { TcpClient client = listener.AcceptTcpClient(); // 接受客户端连接请求 Console.WriteLine("客户端连接成功"); NetworkStream stream = client.GetStream(); // 获取网络流 byte[] buffer = new byte[1024]; int length = stream.Read(buffer, 0, buffer.Length); // 读取客户端发送的数据 string message = Encoding.ASCII.GetString(buffer, 0, length); // 将字节数组转换为字符串 Console.WriteLine("接收到消息:" + message); byte[] data = Encoding.ASCII.GetBytes("服务端收到消息:" + message); // 将字符串转换为字节数组 stream.Write(data, 0, data.Length); // 向客户端发送数据 client.Close(); // 关闭客户端连接 } } } ``` 客户端代码: ```csharp using System; using System.Net.Sockets; using System.Text; class Client { static void Main(string[] args) { TcpClient client = new TcpClient("127.0.0.1", 8888); // 创建TCP客户端,连接本地8888端口 NetworkStream stream = client.GetStream(); // 获取网络流 string message = "Hello, server!"; // 要发送的消息 byte[] data = Encoding.ASCII.GetBytes(message); // 将字符串转换为字节数组 stream.Write(data, 0, data.Length); // 向服务端发送数据 byte[] buffer = new byte[1024]; int length = stream.Read(buffer, 0, buffer.Length); // 读取服务端发送的数据 string response = Encoding.ASCII.GetString(buffer, 0, length); // 将字节数组转换为字符串 Console.WriteLine("接收到响应:" + response); client.Close(); // 关闭客户端连接 } } ``` 这里只是一个简单的示例,实际应用中还需要进行错误处理、多客户端连接处理等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值