《第13章 基于socket的网络编程_70min》由会员分享,可在线阅读,更多相关《第13章 基于socket的网络编程_70min(25页珍藏版)》请在金锄头文库上搜索。
1、基于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, ASLE
2、EP, 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 netwo
3、rk 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 接口?支持支持 IP
4、v4 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?Conn
5、ect(), 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()
6、, 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
7、(“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 cont
8、aining 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
9、, 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 othe
10、r 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
11、 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_W
12、riteable(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;获得数据s
13、tatic 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 listenI
14、SOCKET_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);my
15、Socket = 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_WOULDBLOC
16、K = 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(hell
17、oworld)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_
18、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编程