# How to close a GPRS connection through WIP library

**URL:** <https://forum.sierrawireless.com/t/how-to-close-a-gprs-connection-through-wip-library/1177>\
**Category:** Open AT\
**Created:** [March 12, 2007, 3:08pm UTC](https://forum.sierrawireless.com/t/how-to-close-a-gprs-connection-through-wip-library/1177 "2007-03-12T15:08:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gioiese](https://avatars.discourse-cdn.com/v4/letter/g/46a35a/32.png) [@gioiese](https://forum.sierrawireless.com/u/gioiese)\
**Post date:** [March 12, 2007, 3:08pm UTC](https://forum.sierrawireless.com/t/how-to-close-a-gprs-connection-through-wip-library/1177/1 "2007-03-12T15:08:30Z")

</div>

Hi,  
I’m Using Fastrack M1306B , firmware 6.57.

In my application I am testing GPRS connection and Tcp Client whit WIP library.

In particular I am testing the case in which the GPRS connection must be Deactive.

These are my steps:

1. I active GPRS connection whit  
wip\_bearerOpen(…);  
wip\_bearerSetOpts(…);  
wip\_bearerStart(b); where b is the bearer handle  
2)When I receive WIP\_BEV\_IP\_CONNECTED I start the TCP client with:  
client = wip\_TCPClientCreate(…); where client is the created chanel  
3)when I receive WIP\_CEV\_WRITE I start sending data to the server  
4)After the data sending I close socket with  
wip\_shutdown(client,TRUE,TRUE);my server receives the closing of  
socket
2. when I receive WIP\_CEV\_PEER\_CLOSE:  
I close the created chanel whit wip\_close(client);  
I stop the bearer whit wip\_bearerStop(b);
3. when I receive WIP\_BEV\_STOPPED:  
I close the bearer whit wip\_bearerClose(b);

On TMT I read these trace :

> [@](#):
>
> Trace HWL 1 Unable to find the string of the remote trace in the file (ID = 9186)  
> Trace DEV 1 Unable to find the string of the remote trace in the file (ID = 12654)  
> Trace L3RR 1 Unable to find the string of the remote trace in the file (ID = 4867)  
> Trace HWL 1 Unable to find the string of the remote trace in the file (ID = 9186)  
> Trace CUS4 1 [GPRS]: initialized.  
> Trace CUS4 1 [GSM]: initialized.  
> Trace CUS4 1 [UART1]: initialized.  
> Trace CUS4 1 [UART2]: initialized.  
> Trace CUS4 1 [GPRS]: open: → DISCONNECTED  
> Trace CUS4 1 [GPRS]: start: → CONNECTING  
> Trace CUS4 1 [GPRS]: GPRS EVENT SETUP OK (cid=1): GPRS activate  
> Trace L3RR 1 Unable to find the string of the remote trace in the file (ID = 5680)  
> Trace CUS4 1 [GPRS]: GPRS EVENT: 27 (cid=1)  
> Trace L3RR 1 Unable to find the string of the remote trace in the file (ID = 5680)  
> Trace RLU 1 Unable to find the string of the remote trace in the file (ID = 528)  
> Trace HWL 1 Unable to find the string of the remote trace in the file (ID = 9186)  
> Trace L3RR 1 Unable to find the string of the remote trace in the file (ID = 5680)  
> Trace CUS4 1 [GPRS]: GPRS EVENT ACTIVATE OK (cid=1): FCM open  
> Trace CUS4 1 [GPRS]: FCM subscribe: 0  
> Trace CUS4 1 [GPRS]: FCM EVENT FLOW OPENNED: → CONNECTED  
> Trace CUS4 1 [WIPEV] WIP\_CEV\_OPEN @ 0x18aff16  
> Trace CUS4 1 [WIPEV] WIP\_CEV\_WRITE @ 0x18aff16 (5840 bytes)  
> Trace HWL 1 Unable to find the string of the remote trace in the file (ID = 9186)  
> Trace HWL 1 Unable to find the string of the remote trace in the file (ID = 9186)  
> Trace HWL 1 Unable to find the string of the remote trace in the file (ID = 9186)  
> Trace HWL 1 Unable to find the string of the remote trace in the file (ID = 9186)  
> Trace CUS4 1 [WIPEV] WIP\_CEV\_PEER\_CLOSE @ 0x18aff16  
> Trace HWL 1 Unable to find the string of the remote trace in the file (ID = 9186)  
> Trace HWL 1 Unable to find the string of the remote trace in the file (ID = 9186)  
> Trace HWL 1 Unable to find the string of the remote trace in the file (ID = 9186)  
> Trace HWL 1 Unable to find the string of the remote trace in the file (ID = 9186)  
> Trace CUS4 1 [GPRS]: stop: FCM close → DISCONNECTING  
> Trace CUS4 1 [GPRS]: FCM unsubscribe (0)  
> Trace CUS4 1 [GPRS]: FCM EVENT FLOW CLOSED: GPRS deactivate  
> Trace L3RR 1 Unable to find the string of the remote trace in the file (ID = 5680)  
> Trace CUS4 1 [GPRS]: GPRS EVENT: 2 (cid=1)  
> Trace CUS4 1 [GPRS]: GPRS EVENT DEACTIVATE OK/KO (cid=1): → DISCONNECTED  
> Trace CUS4 1 [GPRS]: close: → CLOSED  
> Trace HWL 1 Unable to find the string of the remote trace in the file (ID = 9253)  
> Trace HWL 1 Unable to find the string of the remote trace in the file (ID = 9186)

From the trace it is observed that the gprs is closed.  
But if after wip\_bearerClose(b) I attend a minute and after I execute  
adl\_atCmdCreate(“AT+CGATT?”,TRUE,CGATT\_0,“\*”,NULL )  
the answer is +CGATT: 1.  
My question is:  
Is wip\_bearerStop(b) + wip\_bearerClose(b) equal to AT+CGATT = 0??

---

<div class="post-metadata">

**Author:** ![Wallace](https://avatars.discourse-cdn.com/v4/letter/w/b4bc9f/32.png) [@Wallace](https://forum.sierrawireless.com/u/Wallace)\
**Post date:** [March 13, 2007, 8:42am UTC](https://forum.sierrawireless.com/t/how-to-close-a-gprs-connection-through-wip-library/1177/2 "2007-03-13T08:42:18Z")

</div>

Hi,

I think wip\_bearerStop and wip\_bearerClose will just set +CGACT to 0, but +CGATT is still 1.  
I think this is not a problem of leaving +CGATT=1, should not cause any big problem.

---

<div class="post-metadata">

**Author:** ![sladjan](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@sladjan](https://forum.sierrawireless.com/u/sladjan)\
**Post date:** [March 27, 2007, 6:50pm UTC](https://forum.sierrawireless.com/t/how-to-close-a-gprs-connection-through-wip-library/1177/3 "2007-03-27T18:50:46Z")

</div>

I have problem opening and closeing many times in one minute the socket

wip\_netInitOpts(WIP\_NET\_OPT\_SOCK\_MAX,2,WIP\_NET\_OPT\_END);

wip\_bearerOpen( &My\_wip\_bearer, “GPRS”, evh\_bearer, NULL);  
wip\_bearerSetOpts( My\_wip\_bearer, WIP\_BOPT\_GPRS\_APN, “My APN”,  
WIP\_BOPT\_LOGIN,“My login” ,  
WIP\_BOPT\_PASSWORD, “My Pwd”,  
WIP\_BOPT\_END);

wip\_bearerStart( My\_wip\_bearer);

Wip\_current\_channel = wip\_TCPClientCreate( “x.x.x.x”, port, evh\_tcpIp, NULL);

//socket Opens  
wip\_shutdown(Wip\_current\_channel,TRUE,TRUE);  
In evh\_tcp I receive WIP\_CEV\_PEER\_CLOSE and I do wip\_close(ev-\>channel);

Wip\_current\_channel = wip\_TCPClientCreate( “x.x.x.x”, port, evh\_tcpIp, NULL);

//socket Opens  
wip\_shutdown(Wip\_current\_channel,TRUE,TRUE);  
In evh\_tcp I receive WIP\_CEV\_PEER\_CLOSE and I do wip\_close(ev-\>channel);

Wip\_current\_channel = wip\_TCPClientCreate( “x.x.x.x”, port, evh\_tcpIp, NULL);

```
Wip_current_channel == NULL !!!!

```

if I wait for 1 minute and I do  
Wip\_current\_channel = wip\_TCPClientCreate( “x.x.x.x”, port, evh\_tcpIp, NULL);

it works !!!

I do not have the controle of “x.x.x.x” but this things worked with ed\_lib()

if I put  
wip\_netInitOpts(WIP\_NET\_OPT\_SOCK\_MAX, 10 ,WIP\_NET\_OPT\_END);  
The same thing happens after 11 wip\_TCPClientCreate()

Any idea how to close the tcp socket so that I can open it as many times I would like to ???

Thanks.

---

<div class="post-metadata">

**Author:** ![ioull](https://avatars.discourse-cdn.com/v4/letter/i/b5a626/32.png) [@ioull](https://forum.sierrawireless.com/u/ioull)\
**Post date:** [March 29, 2007, 8:33am UTC](https://forum.sierrawireless.com/t/how-to-close-a-gprs-connection-through-wip-library/1177/4 "2007-03-29T08:33:46Z")

</div>

Perhaps the socket is not closed when evh\_tcp is called with prameter WIP\_CEV\_PEER\_CLOSE.

What you can do when event WIP\_CEV\_PEER\_CLOSE arrives is launching a timer with one ticks to be sure that the socket is closed !

Did you try it instead of launching a timer of 1 minute ?

---

<div class="post-metadata">

**Author:** ![sladjan](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@sladjan](https://forum.sierrawireless.com/u/sladjan)\
**Post date:** [March 29, 2007, 2:19pm UTC](https://forum.sierrawireless.com/t/how-to-close-a-gprs-connection-through-wip-library/1177/5 "2007-03-29T14:19:32Z")

</div>

Hi,

One tick timer does not change anything. One minute is OK.

Unfortunately the external application Open the next socket every 3 seconds when the last is closed, so I can not wait one minute.  
I never have idle time between Close/Open more then 3 seconds.

The communication time of opend socket is 2 minutes. What I do not understand is why the first closed socket does not close since it have 2 minutes time while the second socket is running ???

No matter what I do, sooner or later I run out of Sockets …

Thanks…

---

<div class="post-metadata">

**Author:** ![ljweko](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@ljweko](https://forum.sierrawireless.com/u/ljweko)\
**Post date:** [March 31, 2007, 3:43pm UTC](https://forum.sierrawireless.com/t/how-to-close-a-gprs-connection-through-wip-library/1177/6 "2007-03-31T15:43:29Z")

</div>

Reading the documentation shows that wip\_shutdown(sck, TRUE, TRUE) or wip\_close(sck) perform the same result (just the result), except that the former variant produces WIP\_CEV\_PEER\_CLOSE event, which by my opinion has nothing to do with peer, as it fires at the same moment, when the shutdown is executed (turn on timing option on TMT). Even more, executing wip\_bearerStop(br) at the same moment fires error WIP\_CER\_HOST\_UNREACHABLE. Reading the documentation reveals, that bearerStop() tries to abort the connection, if it is active. Your first code snippet tries to close the socket with three different simultaneous attacks!

To close a socket I opted for wip\_close(), which by me is cleaner method to fully close it. Then I wait some seconds, as I can get no disconnect event. Experiment a little do get your optimal delay. At that moment I use wip\_bearerStop(br) and after 1 second I get the WIP\_BEV\_STOPPED event and then I can use the socket again (WIP\_NET\_OPT\_SOCK\_MAX = 1).

So, use just one method to close the socket, and add some delay to let FIN packets to do their job gracefully. As in your second snippet, wip\_bearerStop(br) may not be needed in most applications.

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.sierrawireless.com/u/awneil)\
**Post date:** [August 9, 2007, 4:17pm UTC](https://forum.sierrawireless.com/t/how-to-close-a-gprs-connection-through-wip-library/1177/7 "2007-08-09T16:17:25Z")

</div>

> [@ljweko](#):
>
> … WIP\_CEV\_PEER\_CLOSE event, which by my opinion has nothing to do with peer, as it fires at the same moment, when the shutdown is executed … Even more, executing wip\_bearerStop(br) at the same moment fires error WIP\_CER\_HOST\_UNREACHABLE.

Yes, this is the behaviour that I am seeing!

It seems like the PEER\_CLOSE event event simply indicates that WIP has initiated closing the socket - not that the closedown procedure with the Peer has been completed.

In other words, the Bearer cannot be stopped when the PEER\_CLOSE event is received, because the socket is still handling the closedown protocol! 😒

This seems very poor to me - surely there should be some event to positively indicate when a Socket has been closed and, therefore, it is safe to close the Bearer?! 😮

---

<div class="post-metadata">

**Author:** ![fft](https://avatars.discourse-cdn.com/v4/letter/f/e8c25b/32.png) [@fft](https://forum.sierrawireless.com/u/fft)\
**Post date:** [August 10, 2007, 9:07am UTC](https://forum.sierrawireless.com/t/how-to-close-a-gprs-connection-through-wip-library/1177/8 "2007-08-10T09:07:25Z")

</div>

wip\_shutdown() closes the connection cleanly, but doesn’t release the resources taken by the socket. the _only_ way to free a wip\_channel\_t is to call wip\_close() on it. wip\_shutdown() doesn’t release them.

So wip\_shutdown() causes a WIP\_CEV\_PEER\_CLOSE() (or an EOF under POSIX) _on the peer socket_, not locally! And actually, if you wip\_close() a socket you forgot to shutdown, the shutdown will also be sent implicitly.

As for shutdown: in TCP/IP, you detect when your peer shuts down its writing channel, there’s no way to know whether it shot down its reading channel (it’s up to your applicative protocol over TCP to handle that if necessary). WIP\_CEV\_PEER\_CLOSE signals that the peer stopped writing. What happens in many applicative protocols is that when you close your writing, the peer closes its writing in response, so you get WIP\_CEV\_PEER\_CLOSE soon after you called wip\_shutdown(); but this event is triggered by the peer.

In order to check that everything has been sent, before closing the bearer: I’m affraid there’s no event for that. However, when you receive WIP\_CEV\_PEER\_CLOSE, it means that your peer acknowledged your own shutdown, so it should be safe to close the bearer.
