How to send data via TCP ethercard.h library.

Hi guys, i have found examples on this forum how to receive data but i couldn't find and figure it out how to send data via TCP/IP i want to receive it on an app server, i don't want to send to Web browser.

Any help would be apreciate it.

This is the code i'm using, i want to send the incomingData via TCP

Edited

#include <EtherCard.h>
#include <IPAddress.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,0,12 };
// gateway ip address
static byte gwip[] = { 192,168,0,1 };
#endif


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



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



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

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println(F("Failed to access Ethernet controller"));
#if STATIC
  ether.staticSetup(myip, gwip);
#else
  if (!ether.dhcpSetup())
    Serial.println(F("DHCP failed"));
#endif

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

;
}

void loop(){
  uint16_t payloadPos = ether.packetLoop(ether.packetReceive());
 
  if (payloadPos)
  {
    char* incomingData = (char *) Ethernet::buffer + payloadPos;
    
    Serial.println("INCOMING TCP PACKET"); //PORT 80
    Serial.println("-------------------");
    Serial.println(incomingData); 
    Serial.println(incomingData[0]); 
    Serial.println("-------------------");

     // I want to send or write incomingData via TCP
 
  }


  delay(500);
  
}

Where is the data coming from? Where do you want to send it to? Why is the Arduino in the middle? Why doesn't the client access the server directly?

Hi PaulS, I'm sending data from an C# app, and i want the app to receive the data that the arduino has received.

I'm sending data from an C# app

Using what process?

and i want the app to receive the data that the arduino has received.

This is the part that I don't understand. Obviously, the C# app KNOWS what it sent. In general, TCP/IP is a stateless connection. A server (the C# app) blindly responds to a client request. The client is not given an opportunity to provide anything to the server IN THAT TRANSACTION.

This is the c# code to connect. It sends data to arduinoperfectly, but it doesn't receive, When i use this code with an c# server app it works fine.

 const int PORT_NO = 80;
        const string SERVER_IP = "192.168.0.12";
        static void Main(string[] args)
        {
            //---data to send to the server---
            string textToSend = Console.ReadLine() ;

            //---create a TCPClient object at the IP and port no.---
            TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
            NetworkStream nwStream = client.GetStream();
            byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(textToSend);

            //---send the text---
            Console.WriteLine("Sending : " + textToSend);
            nwStream.Write(bytesToSend, 0, bytesToSend.Length);

            //---read back the text---
            byte[] bytesToRead = new byte[client.ReceiveBufferSize];
            int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize); //App stops Here
            Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
            Console.ReadLine();
            client.Close();
        }

This is the c# code to connect. It sends data to arduinoperfectly, but it doesn't receive

What, exactly, are you typing in for it to send?

Why are there no useful Serial.print()s in the Arduino code? (Or why didn't you show the output?)Why is there all that commented out code? Delete it!

Why are you using Tcp to send and Udp to receive?

I have it coded, the received data from the C# app its printed by " Serial.println(incomingData); "

I send a string text from the c# app.

I have it coded, the received data from the C# app its printed by " Serial.println(incomingData); "

I have no idea what this means. Your C# app is using a TcpClient's NetworkStream instance to send and receive data. That is NOT the instance that reads data from the serial port.

Sorry about the comments, i edited the in arduino code . I didn't use the UDP.

Hi, I want to know if you solved it. How do you send data via TCP... ? thanks