Hi all,
I am using Arduino Mega together with W5100 based ethernet shield.
My project involves a Telnet server, a UDP server and a web server running simultaneously.
Occasionally I need to also run an ftp command from within a telnet session in order to upload / download a file from/to the SD card for example in order to update the webpage without removing the SD card from the socket.
The W5100 can support maximum of 4 simultaneous socket connections, so obviously there are not enough sockets available to run all above services at the same time.
Therefore, I am able to stop the udp service temporarily before using the ftp and then re-start it once the ftp is complete.
This still leaves me with shortage of one socket since the ftp service needs two sockets (data & commands)
Therefore I need to be able to also stop the web server as well before using ftp and release its resources.
The ethernet library doesn't seem to support stopping a server once started.
Anyone can suggest a way to do this or any other way around it?
Thanks
There is no ethernet library function to stop the server, but you might be able to do that with the w5100 low level functions. Here are a couple functions I use with my web server code. It can close an open (listening) socket. Take a look at the checkSockStatus function.
#include <utility/w5100.h>
#include <utility/socket.h>
byte socketStat[MAX_SOCK_NUM];
void ShowSockStatus()
{
for (int i = 0; i < MAX_SOCK_NUM; i++) {
Serial.print(F("Socket#"));
Serial.print(i);
uint8_t s = W5100.readSnSR(i);
socketStat[i] = s;
Serial.print(F(":0x"));
Serial.print(s,16);
Serial.print(F(" "));
Serial.print(W5100.readSnPORT(i));
Serial.print(F(" D:"));
uint8_t dip[4];
W5100.readSnDIPR(i, dip);
for (int j=0; j<4; j++) {
Serial.print(dip[j],10);
if (j<3) Serial.print(".");
}
Serial.print(F("("));
Serial.print(W5100.readSnDPORT(i));
Serial.println(F(")"));
}
}
void checkSockStatus()
{
unsigned long thisTime = millis();
for (int i = 0; i < MAX_SOCK_NUM; i++) {
uint8_t s = W5100.readSnSR(i);
if((s == 0x17) || (s == 0x1C)) {
if(thisTime - connectTime[i] > 30000UL) {
Serial.print(F("\r\nSocket frozen: "));
Serial.println(i);
close(i);
}
}
else connectTime[i] = thisTime;
socketStat[i] = W5100.readSnSR(i);
}
}
edit: A socket status of 0x14 is a server socket listening for a connection. Use the close function call to stop the server.
UPDATE: The code above does not stop the server. I'm still working on it.
Thanks SurferTim..I know hou are a expert on ethernet stuff! Looking forward to your advice!
I have used those routines before but i am afraid it didn't solve the problem. May be you can make a better diagnosis ! Heres the output with Telnet connected before trying ftp :
Socket#0:0x17 23 D:192.168.10.7(51459)
Socket#1:0x22 8888 D:0.0.0.0(0)
Socket#2:0x14 80 D:0.0.0.0(0)
Socket#3:0x14 23 D:0.0.0.0(0)
And this is the output after trying ftp:
Socket#0:0x17 23 D:192.168.10.7(51459)
Socket#1:0x22 8888 D:192.168.10.7(21)
Socket#2:0x14 80 D:0.0.0.0(0)
Socket#3:0x14 23 D:0.0.0.0(0)
And the ftp messages:
Downloading file :test.txt
SD opened
Command connected
220 Microsoft FTP Service
331 Password required for win7.
230 User logged in.
215 Windows_NT
200 Type set to I.
227 Entering Passive Mode (192,168,10,7,201,252).
Data port: 51708
Data connection failed
FTP FAIL
I'm running into the same problem. I've tried modifying several registers in the w5100 with no positive results. I can't seem to stop the w5100 internal "server" from listening once it starts.
edit: I was going to ask Wiznet about it, but it appears they do not offer support for the W5100 IC. They have migrated almost exclusively to the W5500 now. Maybe that will be your only option. I'll continue to research it and experiment, but so far, no luck here.
I guess I might have to migrate to the w5500. Having 8 sockets would definitely have advantages.
Hmm will the same libraries still work?
Watcher:
Hmm will the same libraries still work?
In a way. I am already using Wiznet's new library that supports the w5500. Works fine with the w5100 also. Requires a define change in the /utility/W5100.h file.
Ok thanks!
Which w5500 shield are you using?
Watcher:
Ok thanks!
Which w5500 shield are you using?
I'm not using a W5500. I'm using a W5100. That library supports the W5100, W5200, and W5500. The default is W5500, but can be changed with a define in W5100.h.
Another thought: Would it be possible to somehow reset (by software) the W5100 chip and hence close all connections and start over again?
Edit: I guess this would then defeat the purpose since it will also disconnect the Telnet session!
The software reset register change didn't work. It did not reset the W5100, but it locked up and needed a hardware reset. edit: The network end locked up. I could still read and write on the SPI end.
I suppose one way around this would be to terminate the telnet session, do ftp and then re connect the
telnet. Its not the most efficient way to do it but it works...