Socket programming in brew

Source: Internet
Author: User
Tags htons

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.