资源预览内容
第1页 / 共25页
第2页 / 共25页
第3页 / 共25页
第4页 / 共25页
第5页 / 共25页
第6页 / 共25页
第7页 / 共25页
第8页 / 共25页
第9页 / 共25页
第10页 / 共25页
亲,该文档总共25页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
基于socket的网络编程天翼博路技术部2009.2主要内容?BREW所支持的socket编程?UDP socket编程?TCP socket编程BREWTMDeveloper Training BREW中的socket编程特点?非阻塞异步通信模式 - 基于回调函数的通知方式?支持TCP和UDP?C网手机至少同时支持3个TCP,4个UDP?BREW提供的通用API?INetMgr?ISocketINetMgr?INetMgr 对象的创建?ISHELL_CreateInstance(piShell, AEECLSID_NET, &piNet)?网络属性?PPP state (OPEN, ASLEEP, CLOSED)?Single IPv4 address?所有操作完成以后释放INetMgr 的函数?OpenSocket()?Creates a STREAM or DGRAM ISocket?GetHostByName()?Name Service (resolver)?Retrieves “A”record via IDNS?Caches results for performance?NetStatus()?Retrieve instantaneous network state?OnEvent()?Register callback for asynchronous network state changesINetMgr 的函数?GetMyIPAddr()?Local IP address ?Zero if PPP down?SetLinger()?Linger timer?Defaults to 30 seconds?Max of all INetMgr instances?Close PPP lingerseconds after last socket activity?SetOpt() / GetOpt()?Generic Control?RLP3 settings?PPP credentials?DNS serversISocket 接口?支持支持 IPv4 socket?STREAM or DGRAM?INetMgr 管理网络行为管理网络行为?Opens PPP only when needed?Local IP address needed for Bind(), Connect(), or SendTo()?Loopback 支持支持?INADDR_BREW_LOOPBACK?INADDR_LOOPBACKISocket的函数?Bind() / GetSockName()?Request / retrieve local socket address?Listen() / Accept()?TCP server sockets?Connect(), GetPeerName()?Request, retrieve remote TCP socket address?Read(), ReadV(), Write(), WriteV()?TCP data stream?Shutdown(), Close()?Graceful TCP terminationISocket的函数?SendTo(), RecvFrom()?UDP datagrams?GetLastError()?Specific error code of last operation (e.g. ECONNRESET)?Readable(), Writeable(), Cancel()?Non-blocking I/O?SetOpt() / GetOpt()?Socket options (TCP keep-alive, receive buffer size, etc.)?IOCtl()?Generic control, extensionsBREWTMDeveloper Training BREW UDP socket 编程创建与释放?CreationISocket *mySocket;mySocket = INETMGR_OpenSocket(pMe-pINetMgr, AEE_SOCK_DGRAM);if(!mySocket) DBGPRINTF(“Error value: %x”, INETMGR_GetLastError(pMe-pINetMgr);?Releaseif(mySocket) ISOCKET_Release(mySocket);mySocket = NULL; 数据报的发送准备ISOCKET_SendTo ( ISocket * pISocket, byte * pBuff, uint16 wBytes,uint16 wflags, INAddr a, INPort wPort )pISocket: Pointer to the ISocket Interface object. pBuff: Buffer containing data to be sent. wBytes: Size of the buffer, in terms of number of bytes. wflags: Not used and must be set to 0. a: IP Address.wPort: Port.转换成网络字节?Port?netPort = HTONS(myPort);?IP Address?netIP = HTONL(myIP);数据发送Void SendPacket(App* pMe) retVal = ISOCKET_SendTo(pMe-pISocket, (byte*)HelloWorld, sizeof(HelloWorld), 0, pMe-inAddr, HTONS(pMe-port);switch(retVal) case AEE_NET_WOULDBLOCK:ISOCKET_Writeable(pMe-pISocket, (PFNNOTIFY)SendPacket, pMe);return;/ Some sort of network errorcase AEE_NET_ERROR:DBGPRINTF(Network Error);break;default:if(retVal = sizeof(“HelloWorld”)/Successelse/ Some other error端口绑定?ISOCKET_Bind()?int ISOCKET_Bind(ISocket * pISocket, INAddra, uint16 wPort)?a = AEE_INADDR_ANY?Explicitly requests that the socket be assigned to any local address. ?a = AEE_INADDR_LOOPBACK?Allows for local connections. Sent data is routed back to sending device. ?不支持绑定本地地址接收数据static void SetUpNetwork(CVocApp* pMe) int retVal;/ Bind to the correct port and ip addressretVal = ISOCKET_Bind(pMe-pIRecvSocket, HTONL(AEE_BREW_LOOPBACK), HTONS(PORT);/ Check the return value of ISOCKET_Bind() and display necessary messageswitch(retVal) / Socket not ready yetcase AEE_NET_WOULDBLOCK:ISOCKET_Writeable(pMe-pIRecvSocket, (PFNNOTIFY)SetUpNetwork, pMe);break;/ Succesfully connectedcase AEE_NET_SUCCESS:ISOCKET_Readable(pMe-pIRecvSocket, (PFNNOTIFY)ReadInData, pMe);break;/ Something went wrong, print error to loggerdefault:DBGPRINTF(retVal %d, ISOCKET_GetLastError(pMe-pIRecvSocket);break;获得数据static void ReadInData(App* pMe) int ret;/ When data is ready, receive dataret = ISOCKET_RecvFrom(pMe-pIRecvSocket, (void*)&pMe-indata, sizeof(pMe-indata), ISOCKET_FLAG_SENDTO_WAKEUP | ISOCKET_FLAG_SENDTO_URGENT,NULL, NULL);/ Data integrity checksif(ret pIRecvSocket);/ Have socket continue to listenISOCKET_Readable(pMe-pIRecvSocket, (PFNNOTIFY)ReadInData, pMe);BREW TCP socket 编程创建和释放?CreationISocket *mySocket;mySocket = INETMGR_OpenSocket(pMe-pINetMgr, AEE_SOCK_STREAM);if(!mySocket) DBGPRINTF(“Error value: %x”, INETMGR_GetLastError(pMe-pINetMgr);?Releaseif(mySocket) ISOCKET_Release(mySocket);mySocket = NULL;建立连接ISOCKET_Connect(pMe-pISocket, HTONL(myIP), HTONS(myPort), (PFNCONNECTCB)ConnectionMade, pMe);static void ConnectionMade(myApp* pMe, int error) /Check error codeswitch(error) case AEE_NET_ETIMEDOUT:/ Connection timed outbreak;case AEE_NET_SUCCESS:/ Send Some dataif(AEE_NET_WOULDBLOCK = ISOCKET_Write(pMe-pISocket,(byte*)helloworld, sizeof(helloworld) ISOCKET_Writeable(pMe-pISocket, (PFNNOTIFY) SendDataCB, pMe);break;default:/ Some other Network errorbreak;是否可写的回调函数static void SendDataCB(myApp* pMe)if(AEE_NET_WOULDBLOCK = ISOCKET_Write(pMe-pISocket,(byte*)helloworld, sizeof(helloworld)ISOCKET_Writeable(pMe-pISocket, (PFNNOTIFY) SendDataCB, pMe);接收数据static void ReadInData(myApp* pMe) int ret;/ When data is ready, receive dataret = ISOCKET_Read(pMe-pISocket, (void*)&pMe-indata, sizeof(pMe-indata); / Check for errorif(ret pIRecvSocket);/ Have socket continue to listenISOCKET_Readable(pMe-pIRecvSocket, (PFNNOTIFY)ReadInData, pMe);大数据块读写int32 ISOCKET_ReadV ( ISocket * pISocket, SockIOBlock iov, uint16 iovcount ) int32 ISOCKET_WriteV ( ISocket * pISocket, SockIOBlock iov, uint16 wiovcount ) typedef structure byte * pbBuffer; uint16 wLen; SockIOBlock; 本章关键点?Socket是什么?BREW UDP/TCP socket编程
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号