Problem in sending data on a particular IP and Port

Hello everyone!!!
I am working on a project in which I need to send data buffer on a server with a particular IP and Port.
e.g. - IP = 122.200.53.90
Port = 5649

As I am new to all this stuff, so I read many discussions from this forum but didn't succeed yet. So please help me out.

I have Arduino mega and uno as well. For ethernet connection, I am using enc28j60 module.

To check the hardware, I have tested them by running built in examples. And the setup is responding well.

connections:
Arduino _Mega------Ethernet_Module
VCC --------5v
Gnd---------Gnd
CS------------53
SI-------------51
SO------------50
SCK----------52

and the example I have used is given below

// Present a "Will be back soon web page", as stand-in webserver.
// 2011-01-30 <jc@wippler.nl>
//
// License: GPLv2

#include <EtherCard.h>

#define STATIC 0  // set to 1 to disable DHCP (adjust myip/gwip values below)

#if STATIC
// ethernet interface ip address
static byte myip[] = { 192,168,1,200 };
// gateway ip address
static byte gwip[] = { 192,168,1,1 };
#endif

// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer

const char page[] PROGMEM =
"HTTP/1.0 503 Service Unavailable\r\n"
"Content-Type: text/html\r\n"
"Retry-After: 600\r\n"
"\r\n"
"<html>"
  "<head><title>"
    "Service Temporarily Unavailable"
  "</title></head>"
  "<body>"
    "<h3>This service is currently unavailable</h3>"
    "<p><em>"
      "The main server is currently off-line.<br />"
      "Please try again later."
    "</em></p>"
  "</body>"
"</html>"
;

void setup(){
  Serial.begin(57600);
  Serial.println("\n[backSoon]");

  // Change 'SS' to your Slave Select pin, if you arn't using the default pin
  if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
    Serial.println( "Failed to access Ethernet controller");
#if STATIC
  ether.staticSetup(myip, gwip);
#else
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");
#endif

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);
}

void loop(){
  // wait for an incoming TCP packet, but ignore its contents
  if (ether.packetLoop(ether.packetReceive())) {
    memcpy_P(ether.tcpOffset(), page, sizeof page);
    ether.httpServerReply(sizeof page - 1);
  }
}

Please let me know if I need to provide any other information.

Thanks in advance.

you could use this library: EthernetENC - Arduino Reference

You can use Ethernet - Arduino Reference to connect to the IP and port and then use print or write to send the paylaod

What is showing up on Serial Monitor?

Yes, I tried this, run the reference code and don't know what modification it requires.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 64, 233, 187, 99 }; // Google

EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  delay(1000);

  Serial.println("connecting...");

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

The output is:

connecting...
connection failed

disconnecting.

The code is asking for server also but I only have IP address and port number of server.
can you please check what I need to do?

The above program is giving following output:

[backSoon]
IP: 192.168.137.46
GW: 192.168.137.1
DNS: 192.168.137.1

and after typing same IP on browser. It is showing output as in below image.
image
Please let know what I need to do.

Isn't that page exactly what you have instructed your arduino to serve up? I don't see the issue since it is doing what you told it to do.

Is your arduino trying to fetch something from a server? Or send something to a server? Or is the arduino supposed to be the server? I am confused

Do you understand what you are asking? Or any of the client/server/IP stuff?

What is the IP address of the computer with the browser?

Ok, First of all sorry for being late, I am because I thought I should go through TCP/ip, server, DHCP, DNS, NTP etc. , Now I have some idea how these works but I can not buid a code so I have tried few examples on internet.

Now the problem statement is.....

how to send data or write something on server?

For this I tried below code

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 64, 233, 187, 99 }; // Google

EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  delay(1000);

  Serial.println("connecting...");

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

I am getting the output showing in the below image
image

so why why it is unable to connect with server. Do I need to add modify it.

And I hope this time I am making some sense.

Wiring between arduino Mega and ethernet module(ENC28j60).

Arduino _Mega------Ethernet_Module
VCC --------5v
Gnd---------Gnd
CS------------53
SI-------------51
SO------------50
SCK----------52

Please let me know, what should I do now?

and @J-M-L @SteveMann @johnwasser @blh64 thank so much for giving your precious time.

to answer this you need to know what the server is expecting... (protocol, format, etc)

Yes I know,
protocol - TCP/ip
format - client.connect(ip,port)

anything else please.

That's no answer to what is the server expecting?

try something like this if you want to connect on a specific port, send something and listen to what's coming back until the connection is closed

(typed here, fully untested)

#include <EthernetENC.h> // https://github.com/jandrassy/EthernetENC

const uint8_t mac[] = {0x74, 0x69, 0x69, 0x2D, 0x30, 0x31};
EthernetClient client;

void setup() {
  Serial.begin(115200);
  Ethernet.begin(mac);

  Serial.print("localIP: ");     Serial.println(Ethernet.localIP());
  Serial.print("subnetMask: ");  Serial.println(Ethernet.subnetMask());
  Serial.print("gatewayIP: ");   Serial.println(Ethernet.gatewayIP());
  Serial.print("dnsServerIP: "); Serial.println(Ethernet.dnsServerIP());

  if (client.connect("192.168.1.1", 5649)) {
    Serial.println("Client connected");
    // send stuff
    client.println(F("Hello Wolrd"));
  } else {
    Serial.println("Client connect failed");
    while (true) yield(); // die here
  }
}

void loop() {
  // loop and print whatever we get back
  if (client.available()) Serial.print(client.read());

  // until we get disconnected
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    while (true) yield(); // die here
  }
}


This is going to assume your Default Router is at 10.0.0.1. Is that the correct address? If not, that would explain why an outgoing connection failed.

When you used DHCP in your original sketch you received the following configuration:

IP: 192.168.137.46
GW: 192.168.137.1
DNS: 192.168.137.1

Have you moved to a different network?

Asking AGAIN.

I strongly suspect that your PC and your device are on different networks, so it would never connect.

Try commenting or deleting these lines. This will make your device use DHCP for the IP, (which I suspect your PC is), and the devices' actual MAC address.

Hi @J-M-L , I tried the code you have given which is working but not printing data on server. To check whether it is going inside

I printed some data on serial monitor which is showing data on Serial monitor. The below output is coming for the code.

#include <EthernetENC.h> // https://github.com/jandrassy/EthernetENC

const uint8_t mac[] = {0x74, 0x69, 0x69, 0x2D, 0x30, 0x31};
EthernetClient client;

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac);

  Serial.print("localIP: ");     Serial.println(Ethernet.localIP());
  Serial.print("subnetMask: ");  Serial.println(Ethernet.subnetMask());
  Serial.print("gatewayIP: ");   Serial.println(Ethernet.gatewayIP());
  Serial.print("dnsServerIP: "); Serial.println(Ethernet.dnsServerIP());

}

void loop() {
  // loop and print whatever we get back
    if (client.connect("123,275,05,65", 6558)) {
    Serial.println("Client connected");
    // send stuff
    client.println("000000000001111111111111111");
    Serial.println("It's Working");
  } else {
    Serial.println("Client connect failed");
//    while (true) yield(); // die here
  }

  // until we get disconnected
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    Serial.println();
//    while (true) yield(); // die here
  }
}

image

I found another code which is working properly. But I am not able to understand because of its complexity ,I hope you will, so I am adding it for your reference.



#if defined(__MBED__)
  #include <mbed.h>
  #include "mbed/millis.h"
  #define delay(x) wait_ms(x)
  #define PROGMEM
  #include "mbed/Print.h"
#endif

#include <UIPEthernet.h>
#include "utility/logging.h"

EthernetClient client;
unsigned long next;

#if defined(ARDUINO)
void setup() {
#endif
Serial.begin(9600);
#if defined(__MBED__)
int main() {
#endif
  #if ACTLOGLEVEL>LOG_NONE
    #if defined(ARDUINO)
      LogObject.begin(9600);
    #endif
    #if defined(__MBED__)
      Serial LogObject(SERIAL_TX,SERIAL_RX);
    #endif
  #endif

  uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
  Ethernet.begin(mac); //Configure IP address via DHCP

  #if ACTLOGLEVEL>=LOG_INFO
    LogObject.uart_send_str(F("localIP: "));
    #if defined(ARDUINO)
      LogObject.println(Ethernet.localIP());
      #endif
    #if defined(__MBED__)
      LogObject.printf("%d.%d.%d.%d",Ethernet.localIP()[0],Ethernet.localIP()[1],Ethernet.localIP()[2],Ethernet.localIP()[3]);
    #endif
    LogObject.uart_send_str(F("subnetMask: "));
    #if defined(ARDUINO)
      LogObject.println(Ethernet.subnetMask());
    #endif
    #if defined(__MBED__)
      LogObject.printf("%d.%d.%d.%d",Ethernet.subnetMask()[0],Ethernet.subnetMask()[1],Ethernet.subnetMask()[2],Ethernet.subnetMask()[3]);
    #endif
    LogObject.uart_send_str(F("gatewayIP: "));
    #if defined(ARDUINO)
      LogObject.println(Ethernet.gatewayIP());
    #endif
    #if defined(__MBED__)
      LogObject.printf("%d.%d.%d.%d",Ethernet.gatewayIP()[0],Ethernet.gatewayIP()[1],Ethernet.gatewayIP()[2],Ethernet.gatewayIP()[3]);
    #endif
    LogObject.uart_send_str(F("dnsServerIP: "));
    #if defined(ARDUINO)
      LogObject.println(Ethernet.dnsServerIP());
    #endif
    #if defined(__MBED__)
      LogObject.printf("%d.%d.%d.%d",Ethernet.dnsServerIP()[0],Ethernet.dnsServerIP()[1],Ethernet.dnsServerIP()[2],Ethernet.dnsServerIP()[3]);
    #endif
  #endif

  next = 0;
#if defined(ARDUINO)
}

void loop() {
#endif  

#if defined(__MBED__)
while(true) {
#endif
      #if ACTLOGLEVEL>=LOG_INFO
        LogObject.uart_send_strln(F("Client connect"));
      #endif
      // replace hostname with name of machine running tcpserver.pl
//      if (client.connect("server.local",5000))
      if (client.connect(IPAddress(123,275,05,65),6558))
        {
          #if ACTLOGLEVEL>=LOG_INFO
            LogObject.uart_send_strln(F("Client connected"));
          #endif
          client.println("00000000000000000000000000000");
          Serial.println("M00999,00000000000000000000000000000");

          //disconnect client
          #if ACTLOGLEVEL>=LOG_INFO
            LogObject.uart_send_strln(F("Client disconnect"));
          #endif
          client.stop();
        }
      else
        {
        #if ACTLOGLEVEL>=LOG_INFO
          LogObject.uart_send_strln(F("Client connect failed"));
        #endif
        }
}
#if defined(__MBED__)
}
#endif

image

Please let me know what modification I need to make in your code. Because it's simple and easy to understand and I knew it can work with some correction.

Thank you so much.

the question is — as already expressed — what is the server ?

I just don't get what you are trying to show with your second COM5 screen grab full of 0000

the Serial monitor won't show what you send... you see that on the other side...

so... tell us about the server. what is it, what does it expect. on which network it is etc..

You have many pending questions you have not addressed (inc. from @johnwasser and @SteveMann ), I'm no longer going to reply until you have provided full information required.

ok, I am sorry for inconvenience, but I thought the reply will be sufficient because the code you have given was clarifying the question asked by @johnwasser and @SteveMann.

Bot of them are right that

and

Which all of you can see in the second code (above recent reply) .

Again I am sorry, and thank you so much because now the code you have given is working perfectly with a little modification.

It is a simple server which can be access through IP address and port. I don't know the name.

Thank you everyone @J-M-L @SteveMann @johnwasser and @blh64 for your help I ave learnt a lot because of you.