Rabbit 2600 hanging when I ask it to close a socket

Hi all,

I have inherited code from a former employee that was written for a Rabbit 2500 (which worked fully). My task was to port the code so that it would run on a 2600, since we needed more Digital I/Os. I had never used Dynamic C before so I’m learning as I go, and running into some very frustrating problems.

I’ll spare you the gory details of the project, but basically the Rabbit uses uC/OS-II and connects via TCP/IP to another computer. The Rabbit then sends the data from the Digital I/Os sixty times a second (as a hex string) to a Java program which takes the raw data and writes it to a text file.

My problems arise with the TCP/IP connection. When I run the code I ported on the 2600 it seems to run fine. When I start the javascript sometimes it’ll work, and sometimes it won’t, seemingly randomly. Specifically, sometimes the javascript will run successfully, create the file, and start pumping it full of data. Sometimes the javascript will create the file but nothing is ever written to it. Sometimes it’ll crash the connection and I have to restart the 2600, and sometimes it’ll say that no connection could be established. Again, there is absolutely NO seeming correlation to when I run the javascript and when it’ll work or not.

I believe the problem lies in the 2600 specifically. With all the uC/OS-II related code gone (no tasks, no definitions, etc) here is the code:

[i]#class auto
#define TCPCONFIG 1
#define TCP_PORT 23
#use “dcrtcp.lib”

tcp_Socket Socket0;

void initialize();

void main(){

while(tcp_tick(&Socket0)) {
printf("out of while loop, socket is closed

void initialize(){


while (ifpending(IF_DEFAULT) == IF_COMING_UP) {
printf("init done

The 2500 code I inherited did not have the while loops that wait for the sock_init() to be completed, or the sock_close() to be completed, I added those. I find that the sock_init() always works, while the sock_close() hangs very often, but will randomly work. What I mean is, half the time when I run the code the printline below the while loop won’t ever display, it’s caught in the loop indefinitely. If I remove the loop and just run the code, half the time the javascript can connect, half the time it doesn’t. That code is straight out of the TCP/IP User Manual, why wouldn’t it work?

I guess the other thing that I’m wondering about is why you’d initialize a socket (in the void initialize() function) and as soon as you’re done, close it in main? Nowhere in the uC/OS-II code (that I’ve left out) does it re-establish a connection. That being said, it works. If I don’t close the socket, it never works.

I’m definitely a newbie to Dynamic C, have taken a semester of C++. These problems, however, are entirely beyond my comprehension. If you guys can help me out at all, I’d sure as heck appreciate it!

I would suggest you use sock_abort instead of sock_close.
Sock_close verifies with the client if it is allowed to close the socket, sometimes you get a difficult client :).
Sock_abort always closes the socket.

why you’d initialize a socket…close it in main?

Probably this is not needed, but to ensure that the socket is indeed closed before another process tries to open the socket.


Thanks for the response!

I didn’t mention it in my first post, but I have tried using sock_abort and unfortunately it doesn’t help; it still hangs and is caught in that while loop indefinitely.

I noticed today that I have two 2500s here; one is older and one is newer. I realized that with the newer board, I have the exact same problems with the 2600…microcontroller hangs, connection is dropped, random times it actually does work…the newer 2500 runs at the same speed as the 2600, 44MHz. The older 2500 runs at 29MHz. Could this be the source of the problem? The code I was given, that works flawlessly with the 29MHz 2500 model, doesn’t even work with the 44MHz 2500 model.

Is there a function that controls the time the controller allows for sockets to remain open before they’re closed? I’m searching through the TCP manuals and haven’t seen anything catch my eye, but I’ll keep looking and hopefully someone here might be able to help me.