WIZnet Developer Forum

W7500 TCP client Example

Hi,

Does anybody has a good TCP client example working? I’ve been using the Berkeley Socket API found in
./ioLibrary/Ethernet/socket.c but sometimes it behaves strangely. I have to insert some delay between functions. Sometimes works fine but not others. I’ve been analyzing the return value but does not help.

Example:

I have to insert some delays between creating the socket, open the connection and send or receive data:

sn=0 // using only the socket 0
socket(sn, Sn_MR_TCP, port_server, 0);
delay(100);
connect(sn, destip, destport)
delay(100);
send(sn, buf+sentsize, size-sentsize);

I also realized that depending the server location i have to change the delay time. The further away the server (ex. on Amazon) the higher are the changes to get errors. If i test in a local machine it works most of time.

Any ideia?
Best regards,

Hello, joaopaulob

Are you using W7500 with TCP client?

You only can send data in TCP after tcp connection success.

That is why it is hard to guess the network timing, with delay.

So, w7500 supports useful register, Sn_SR(sn), which can check network conditions.

With Sn_SR(n),

sn=0 // using only the socket 0

while(!getPHYCFGR());  // phy link check

socket(sn, Sn_MR_TCP, port_server, 0);
connect(sn, destip, destport)

while(Sn_SR(sn) != SOCK_ESTABLISHED); // check the tcp connect success
send(sn, buf+sentsize, size-sentsize); 

while(Sn_RX_RSR(sn) != 0); // if server send some data
recv(sn,recv_buf,recv_length);

You can find the Sn_SR & Sn_RX_RSR in W7500 reference sheet.

http://wizwiki.net/wiki/lib/exe/fetch.php?media=products:w7500:w7500_rm_v109.pdf

Thank you

lawrence

2 Likes

Hi,

I found the problem. The socket.c was set as non_blocking mode. The “Sn_SR(sn) != SOCK_ESTABLISHED” was not checked.

Thanks

You did !!

I am glad to hear that!

Thank you for your interests.

Feel free to ask anytime.

Thank you

lawrence

1 Like

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