# problem with Connection closed socket (re-loop)

**URL:** <https://forums.digi.com/t/problem-with-connection-closed-socket-re-loop/21735>\
**Category:** Rabbit\
**Tags:** dynamic-c, rabbit\
**Created:** [September 2, 2020, 10:58am UTC](https://forums.digi.com/t/problem-with-connection-closed-socket-re-loop/21735 "2020-09-02T10:58:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JPal](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@JPal](https://forums.digi.com/u/JPal)\
**Post date:** [September 2, 2020, 10:58am UTC](https://forums.digi.com/t/problem-with-connection-closed-socket-re-loop/21735/1 "2020-09-02T10:58:21Z")

</div>

Hello,

To communicate with my server i have tried active.c who works but my problem is this part :

" while(tcp\_tick(&socket));

```
sock_abort(&amp;socket);
printf("

```

Connection closed…  
“);”

doesn’t work.

I must implement a interrupt function but as my connection doesn’t close, i can’t re-loop my interrupt, excepted that it works. So if you have any ideas to help me i will appreciate this.

---

<div class="post-metadata">

**Author:** ![TomCollins](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/tomcollins/32/37_2.png) [@TomCollins](https://forums.digi.com/u/TomCollins)\
**Post date:** [September 2, 2020, 6:06pm UTC](https://forums.digi.com/t/problem-with-connection-closed-socket-re-loop/21735/2 "2020-09-02T18:06:15Z")

</div>

If the remote end closes its end of the socket, then the Rabbit should eventually close the socket and exit that while loop.

You can call sock\_close() if you want to initiate closure of the socket.

After that call to sock\_abort(), you should be able to go right back to calling tcp\_open() on the socket structure to open another connection.

---

<div class="post-metadata">

**Author:** ![JPal](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@JPal](https://forums.digi.com/u/JPal)\
**Post date:** [September 3, 2020, 2:27am UTC](https://forums.digi.com/t/problem-with-connection-closed-socket-re-loop/21735/3 "2020-09-03T02:27:22Z")

</div>

void main(){

```
auto int key, channel;
  // Initialize the controller
brdInit();

```

// Configure all inputs to be general digital inputs  
for(channel = 0; channel \< BL\_DIGITAL\_IN; ++channel)  
{  
setDigIn(channel);  
}

```
if (digIn(3) != 0)
{
   char buffer[2048];
 int bytes_read;
  	longword destIP;
	tcp_Socket socket;
   int digIn();

```

//création de la socket  
sock\_init\_or\_exit(1);

```
if( 0L == (destIP = resolve(DEST)) ) {
	printf( "ERROR: Cannot resolve \"%s\" into an IP address

```

", DEST );  
exit(2);  
}

```
tcp_open(&amp;socket,0,destIP,PORT,NULL);

printf("Waiting for connection...

```

");  
while(!sock\_established(&socket) && sock\_bytesready(&socket)==-1) {  
tcp\_tick(NULL);  
}

```
printf("Connection established, sending request...

```

");

```
/*
 * When tcp_tick()-ing on a specific socket, we get non-zero return while
 * it is active, and zero when it is closed (as used here).
 */

sock_write(&amp;socket,"client actif 

```

",30);

```
do {
	bytes_read=sock_fastread(&amp;socket,buffer,sizeof(buffer)-1);

	if(bytes_read&gt;0) {
		buffer[bytes_read] = '\0';
		/*
		 * By using the "%s" format, if there are "%" in the buffer, printf()
		 * won't try to interpret them!
		 */
		printf("%s",buffer);
	}
}

```

while(tcp\_tick(&socket));

```
sock_abort(&amp;socket);
printf("

```

Connection closed…  
");  
}  
}

As you see (in this part) it’s a the end, i have tried sock\_close() without succes. I don’t understand why my function stay in the do and doesn’t continue.

---

<div class="post-metadata">

**Author:** ![TomCollins](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/tomcollins/32/37_2.png) [@TomCollins](https://forums.digi.com/u/TomCollins)\
**Post date:** [September 3, 2020, 11:37am UTC](https://forums.digi.com/t/problem-with-connection-closed-socket-re-loop/21735/4 "2020-09-03T11:37:02Z")

</div>

Do you understand that the “while” is part of the “do” above it? “do { … } while (condition);” is a loop that executes the statements at least once, and continues to loop as long as the condition is true. In this case, you’ll leave the “do” when tcp\_tick() returns false (0), which it should do if the remote end has closed the socket correctly.

---

<div class="post-metadata">

**Author:** ![JPal](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@JPal](https://forums.digi.com/u/JPal)\
**Post date:** [September 4, 2020, 1:58am UTC](https://forums.digi.com/t/problem-with-connection-closed-socket-re-loop/21735/5 "2020-09-04T01:58:52Z")

</div>

Thank for this answer TomCollins, so I need to correctly close my server.
