telnet client reading digital pins

I have a mega 2560 with an ethernet shield and I'm a newbie to programming it.

I have followed the telnet client example which works fine, however i want to now modify this project to monitor 10 digital pins and send the results back to the connected telnet client.

The code example below always disconnects the client as soon as the digital pin value is read and so i think i'm missing some understanding about the best/proper method to achieve this. (As the telnet example does not disconnect as it processes the user input.)

Any help would be extremely welcome!

From my understanding the code would be something like: -

IPAddress server(ipAddressOfTelnetServer);
int inputPin = 51;

void Setup(){

Ethernet.begin(mac, ipAddressOfTelnetClient);

pinMode(inputPin, INPUT);

if (client.connect(server, PORT_NUMBER)) {
Serial.println("connected");
}
else {
Serial.println("connection failed");
}

}

void loop(){

pinVal = digitalRead(inputPin);

if (client.connected()) {
// send the input pin value to the telnet client
client.print(pinVal);
}
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing:
while (true);
}

So, don't disconnect and read and send the data from the other pins. Then disconnect.

The code doesn't try to disconnect and so i'm unsure what you mean by this?

The sample telnet project doesn't disconnect the client when processing and so the only line that is different in my modified project is: -

pinVal = digitalRead(inputPin);

and so i'm looking to understand why this line of code makes the telnet client disconnect and what I need to change to ensure that when the digital pins are read it doesn't disconnect the telnet client.

jclm:
The code doesn't try to disconnect and so i'm unsure what you mean by this?

You wrote The code example below always disconnects the client as soon as the digital pin value is read and so i think i'm missing some understanding about the best/proper method to achieve this

Are you wrong then or are you wrong now?

I'm right in both. Please read my answers as 'the code below' refers to the modified example.

Does anyone else have any ideas...?

jclm:
I'm right in both. Please read my answers as 'the code below' refers to the modified example.

Does anyone else have any ideas...?

I am confused by what you are asking.

The question I'm asking is why the single digital pin read line of code disconnects the telnet client. This is the only difference from the sample project which works fine.

Pin 51 on the Mega is SPI MOSI.

Ok thanks.

Sorry for sounding dim but I'm unsure what that means in this scenario. Does it mean I shouldn't be doing a digital read on it?

I'm not sure as I'm just headed out shopping, and too busy to double check, but I think you will find that the Ethernet board uses SPI pins on the ICSP header.

The Ethernet shield uses SPI to communicate between the board and the shield. Pin 51 is part of the SPI bus. So you need to pick a different pin. MOSI needs to be left as an output as the SPI library sets it.

Yes that's very helpful- thanks!

I think I need to make sure that I use the other digital pins as pin 51 is different. Will try again in the morning. Thanks for your help.