Quantcast
Channel: Wireless Connectivity
Viewing all articles
Browse latest Browse all 116964

Forum Post: RE: Help with accept() on CC3000

$
0
0

Yael's advice, as is usual around here, was right.  I was attempting to accept() all wrong.  Following the example in server.c within the sensor application that Yael recommended helped me understand what was going on.

I'll do my best to explain it here so that new people like me hopefully won't make the same mistakes.  The mistake I made was that I thought the bind() function took a full, defined sockaddr variable (as opposed to an empty one).  What you need to do instead is declare a new sockaddr for the bind() function, give .sa_data[] the port information, and leave the rest empty.  The CC3000 will fill the empty sockaddr when a client connects.  The same goes with the sockaddr that is passed to the accept() function.  Leave it empty and let the CC3000 fill it.  Here is the code I'm using that works now:

// Set up listener on port (inport)
sockaddr sraddr;
long sr = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
sraddr.sa_family = AF_INET;
sraddr.sa_data[0] = (inport & 0xFF00) >> 8;
sraddr.sa_data[1] = (inport & 0x00FF);
memset (&sraddr.sa_data[2], 0, 4);

sockaddr clientaddr;

socklen_t addrlen;

bind(sr, &sraddr, sizeof(sockaddr));
__delay_cycles(100000);
listen(sr,0);

while (1)

{

__bis_SR_register(LPM2_bits + GIE);
__no_operation();

//
// Handle any un-solicited event if required - the function shall be triggered
// few times in a second
//
hci_unsolicited_event_handler();

long acceptresult = accept(sr, &clientaddr, &addrlen);

if(acceptresult >= 0)
{

//have a connection
turnLedOn(3);

}
else if(acceptresult == -2)
{

//connection pending
turnLedOn(2);

 }

else if(acceptresult == -1)
{

//socket error
turnLedOn(1);

}

}

I hope what I explained is actually right and not just some fluke that happens to work. I haven't actually received any data yet and parsed through it, but that's next on my list. My goal is to have the while(1) loop continually look for incoming connections. I think I might need to move the "sockaddr clientaddr" into the while loop to accomplish this, but I'm not sure yet. If I get something that works, I'll report back. Thanks for everyone's help!


Viewing all articles
Browse latest Browse all 116964

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>