For UDP-based network communication, you must first create an isocket interface,
Isocket * mysocket;
Mysocket = inetmgr_opensocket (PME-> pinetmgr, aee_sock_stream );
If (! Mysocket ){
Dbuplintf ("error value: % x", inetmgr_getlasterror (PME-> pinetmgr ));
}
Then, you can send data packets.
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 error
Case aee_net_error:
Dbuplintf ("network error ");
Break;
Default:
If (retval = sizeof ("helloworld "))
// Success
Else
// Some other error
}
}
Before receiving data, bind the client port.
Static void setupnetwork (cvocapp * PME ){
Int retval;
// Bind to the correct port and IP address
Retval = isocket_bind (PME-> pirecvsocket, htonl (aee_brew_loopback), htons (port ));
// Check the return value of isocket_bind () and display necessary message
Switch (retval ){
// Socket not ready yet
Case aee_net_wouldblock:
Isocket_writeable (PME-> pirecvsocket, (pfnnotify) setupnetwork, PME );
Break;
// Succesfully connected
Case aee_net_success:
Isocket_readable (PME-> pirecvsocket, (pfnnotify) readindata, PME );
Break;
// Something went wrong, print error to Logger
Default:
Dbuplintf ("retval % d", isocket_getlasterror (PME-> pirecvsocket ));
Break;
}
}
Then, you can receive data through UDP,
Static void readindata (APP * PME ){
Int ret;
// When data is ready, receive data
Ret = isocket_recvfrom (PME-> pirecvsocket, (void *) & PME-> indata, sizeof (PME-> indata), bytes | isocket_flag_sendto_urgent, null, null );
// Data Integrity Checks
If (Ret <0 ){
Dbuplintf ("error % d", isocket_getlasterror (PME-> pirecvsocket ));
}
// Have socket continue to listen
Isocket_readable (PME-> pirecvsocket, (pfnnotify) readindata, PME );
}
After the communication is complete, you need to use isocket_release () to release the interface object.
For TCP-based network communication, you must first create an isocket interface,
Isocket * mysocket;
Mysocket = inetmgr_opensocket (PME-> pinetmgr, aee_sock_stream );
If (! Mysocket ){
Dbuplintf ("error value: % x", inetmgr_getlasterror (PME-> pinetmgr ));
}
Then, establish a data connection and monitor the network status of the connection. If the connection is successful, data is sent.
Isocket_connect (PME-> pisocket, htonl (myip), htons (myport), (pfnconnectcb) connectionmade, PME );
Static void connectionmade (MyApp * PME, int error ){
// Check error code
Switch (error ){
Case aee_net_etimedout:
// Connection timed out
Break;
Case aee_net_success:
// Send some data
If (aee_net_wouldblock = isocket_write (PME-> pisocket, (byte *) "helloworld", sizeof ("helloworld ")){
Isocket_writeable (PME-> pisocket, (pfnnotify) senddatacb, PME );
}
Break;
Default:
// Some other network error
Break;
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/wireless_com/archive/2010/09/29/5914099.aspx