Chat Server ethernet APp (Wrong!! Somewhere)

I got a solution but my server code on arduino does not seems to work. and im using a c# console app to send values to the server.

Arduino server code:

/*
 Chat  Server
 
 A simple server that distributes any incoming messages to all
 connected clients.  To use telnet to  your device's IP address and type.
 You can see the client's input in the serial monitor as well.
 Using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)
 
 created 18 Dec 2009
 by David A. Mellis
 modified 10 August 2010
 by Tom Igoe
 
 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,4);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,0,0);

// telnet defaults to port 23
EthernetServer server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet

void setup() {
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);
  // start listening for clients
  server.begin();
  // open the serial port
  Serial.begin(9600);
}

void loop() {
  // wait for a new client:
  EthernetClient client = server.available();
  
  // when the client sends the first byte, say hello:
  if (client) {
    if (!gotAMessage) {
      Serial.println("We have a new client");
      client.println("Hello, client!"); 
      gotAMessage = true;
    }
    
    // read the bytes incoming from the client:
    char thisChar = client.read();
    // echo the bytes back to the client:
    server.write(thisChar);
    // echo the bytes to the server as well:
    Serial.print(thisChar);
  }
}

C# code:

/*       Client Program      */

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;


public class clnt
{

    public static void Main()
    {

        try
        {
            TcpClient tcpclnt = new TcpClient();
            Console.WriteLine("Connecting.....");

            tcpclnt.Connect("192.168.1.4", 80);
            // use the ipaddress as in the server program

            Console.WriteLine("Connected");
            Console.Write("Enter the string to be transmitted : ");

            String str = Console.ReadLine();
            Stream stm = tcpclnt.GetStream();

            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] ba = asen.GetBytes(str);
            Console.WriteLine("Transmitting.....");

            stm.Write(ba, 0, ba.Length);

            byte[] bb = new byte[100];
            int k = stm.Read(bb, 0, 100);

            for (int i = 0; i < k; i++)
                Console.Write(Convert.ToChar(bb[i]));

            tcpclnt.Close();
        }

        catch (Exception e)
        {
            Console.WriteLine("Error..... " + e.StackTrace);
        }
    }

}

//

The c# console shows connected but the Serial window on Arduino IDE is al blank always even when i send stuff from client.

Just a question.

Arduino server:

// this is port 23
EthernetServer server(23);

C# client with question:

            // is this attempting to connect on port 80?
            tcpclnt.Connect("192.168.1.4", 80);

Yes Yes i got it too as soon i posted , but changed it already in the main programme.

this is in the arduino:(changes)

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,4);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

// telnet defaults to port 23
EthernetServer server(80);
boolean gotAMessage = false; // whether or not you got a message from the client yet

i tried other sketches too; like the DHCP address printer, the case is same here too , nothing is on the Serial Monitor screen and the Baud rate is correct.

However sketches like udpntpclient and webclient work good , can you catch up whats bad here?

You are having trouble with DHCPAddressPrinter? It never shows anything on the serial monitor? It may take up to a minute or more for the request to timeout and print something.

Does this ethernet shield have a microSD slot? Do you have a microSD card in the slot?

You are having trouble with DHCPAddressPrinter? It never shows anything on the serial monitor?

It shows stuff on the Serial Monitor, have a look at the Serial prints??!!

/*
  DHCP-based IP printer
 
 This sketch uses the DHCP extensions to the Ethernet library
 to get an IP address via DHCP and print the address obtained.
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 12 April 2011
 by Tom Igoe
 
 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // start the serial library:
  Serial.begin(9600);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
}

void loop() {

}

Do you have a microSD card in the slot?

Yes i have a 512MB SD card in it.!!!!!!

One more thing im getting into using the port 80 in both place here :

 tcpclnt.Connect("192.168.1.4", 80);

and

EthernetServer server(80);

bcz c# app wont get connected with 23 as port it sticks till connecting.....

Yes i have a 512MB SD card in it.!!!!!!

That is the challenge.

void setup() {
  // start the serial library:
  Serial.begin(9600);

  // disable the SD card SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // start the Ethernet connection:
  // and the rest
}

That will prevent DhcpAddressPrinter from working also. Remove the microSD card as a test. It should work then.

I took the SD card from that moment on and reprogrammed the Processor with the following code:

/*
 Chat  Server
 
 A simple server that distributes any incoming messages to all
 connected clients.  To use telnet to  your device's IP address and type.
 You can see the client's input in the serial monitor as well.
 Using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)
 
 created 18 Dec 2009
 by David A. Mellis
 modified 10 August 2010
 by Tom Igoe
 
 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,4);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

// telnet defaults to port 23
EthernetServer server(80);
boolean gotAMessage = false; // whether or not you got a message from the client yet

void setup() {
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);
  // start listening for clients
  server.begin();
  // open the serial port
  Serial.begin(9600);
  Serial.print("done");
}

void loop() {
  // wait for a new client:
  EthernetClient client = server.available();
  
  // when the client sends the first byte, say hello:
  if (client) {
    if (!gotAMessage) {
      Serial.println("We have a new client");
      client.println("Hello, client!"); 
      gotAMessage = true;
    }
    
    // read the bytes incoming from the client:
    char thisChar = client.read();
    // echo the bytes back to the client:
    server.write(thisChar);
    // echo the bytes to the server as well:
    Serial.print(thisChar);
  }
}

and changed this in C#:

tcpclnt.Connect("192.168.1.4", 80);

i think its all is correct but still the serial monitor shows nothing and nothing happens when i send from c# programme also the DHCPAddress printer i left for 4-5 minutes it still shows the Serial monitor Clean!

My computers IP is 192.168.1.2

edit: My bad. The Arduino ip is 192.168.1.4. The computer is 192.168.1.2. Is that right?

Can you ping 192.168.1.4 from the computer?

Can you ping 192.168.1.4 from the computer?

No sir it says Request timed out

Try DhcpAddressPrinter again with the SD card removed. Can you ping the Arduino at the new ip?

edit: My bad. The Arduino ip is 192.168.1.4. The computer is 192.168.1.2. Is that right?

Yes but i have to disconnect the Ethernet and plug into the Itead Iboard (that equals shield as W5100 is used on it too) and utilise the GPRS on my handset on the computer for internet till im testing.

and as such the ethernet adapter Local area network 3 says:

ipv4 address is: 192.168.42.226 , so is it this i need to use?

Try DhcpAddressPrinter again with the SD card removed. Can you ping the Arduino at the new ip?

Now i have removed the SD card and im not inserting it anymore till i make this work without it.Thanks!

Yes but i have to disconnect the Ethernet and plug into the Itead Iboard (that equals shield as W5100 is used on it too) and utilise the GPRS on my handset on the computer for internet till im testing.

and as such the ethernet adapter Local area network 3 says:

ipv4 address is: 192.168.42.226 , so is it this i need to use?

Why does that sound like a conversation between Kirk and Scotty? I don't have a clue.

Why does that sound like a conversation between Kirk and Scotty? I don't have a clue.

Sorry for that because i even didn't have the attention to this particular aspect i didn't thought about it.

so what to here? do i need to mould the Arduino programme IP as in 192.168.42.5/6/7 etc i mean on the same domain?

and moreover in the Arduino CharServer it says

// telnet defaults to port 23
EthernetServer server(80);

so changing it to 80 is ok or not? i did it bcz at 23 port number the c# programme just stands at connecting..... and with 80 as port its able to access the server and prompt for "Enter the string to be transmitted : "