WIZnet Developer Forum

How can I detect new connections? (re: buffering data)

I’m using the W5500 chip (Wiz850io module) configured as a TCP server that can accept multiple connections. I’d like each client to be able to send commands at the same time.

The command structure is: followed by carriage return. (\r)

Sometimes the commands can be sent over multiple packets so I’d like to buffer the data from each client until I receive the terminator (\r).

How can I do this?

I think the program should flow like this: (?)

  1. Detect new connection
  2. Create new buffer
  3. Receive data until \r is found.
  4. Process command
  5. Detect disconnect (if possible)
  6. Remove buffer

That means I need a way to identify new connections and unique clients so I can buffer their data separately.

Generally it should be correct, and you speak very high level. When designing application, do not for get that you can not get stuck service only single socket as it may be waiting for some time to get data or change its state, and you will need some algorithm (for example, round robin) to check all the sockets and perform tasks attributed to their status.

Sorry, but I’m still not sure how to detect a new connection using the Wiz850io and the Teensy.

For example, the W5500 has 8 available sockets.

Inside the main loop in my code, how can I check if a socket has a new connection from the last time I checked? (ie. the last loop iteration)

Unless I’m mistaken, the socket can go from SOCK_CON, to SOCK_ESTABLISHED faster than a loop iteration, so can I detect a ‘new’ connection versus an existing connection?

It seems to be a bit easier on other platforms (ie. BSD sockets), because a listening socket will create a NEW socket for every incoming connection so you can keep track of the socket handle to see if this is a new connection or an existing one.

Does that help explain my question?

When socket is listening, it can accept incoming connection, becoming ESTABLISHED. If, for some reason, connection drops, socket goes to state CLOSING or CLOSED, and will not accept any other incoming connections until you explicitly instruct socket to get back into listening mode.
Thus you for sure will not confuse connections because when you check for socket’s state you will see that it is not in listening (0x14) mode any more. And then, basing on status register value you have read you perform specific actions.

I recommend you to refer to W5100’s datasheet status register diagram and chapter 5 - you will see more or less comprehensive explanation how W5x00 family is programmed.

You can also see if you can use hardware interrupts from the chip, but it requires different programming approach rather than just checking status of sockets because interrupt can occur at any time and you should be sure socket handling flow is not broken.

Copyright © 2017 WIZnet Co., Ltd. All Rights Reserved.