TCP client/server -> SERVER PROBLEM

Hello
I am making tcp client/server and for now I can read the incoming packages over tcp but I can not send any messages.

#include <SPI.h>
#include <UIPEthernet.h>
String serial_data;
char incomingByte = 0;
char s = 0;
int choice = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(10, 5, 5, 10); // numeric IP for Google (no DNS)
IPAddress ip(10, 5, 5, 106);
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 9876)) {
    Serial.println("connected");
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  Serial.println("Wcisnij a aby odczytywac pakiety");
  Serial.println("Wcisnij b aby wysylac pakiety");
}


void loop() {
Ethernet.maintain();
  // Read serial input:
  if (Serial.available() > 0)
  {
    incomingByte = Serial.read();
  }


  switch (incomingByte) {

    case 'a':
      //Serial.print("Logi");
      if (client.available())   {

        char c = client.read();

        Serial.print(c);
        Serial.print("");
      }
      if (!client.connected()) {
        client.connect(server, 9876);
      }
      break;

    case 'b':
      //Serial.print("Wpisz komendę");
      if (client.available())   {
        if (Serial.available() > 0) {
       Serial.println("Sent..");
    serial_data = String(Serial.read());
    client.print(serial_data);
        }
      }
      if (!client.connected()) {
        client.connect(server, 9876);
      }
      break;

add Ethernet.maintain(); as first in loop()

Added it, nothing changed.

#include <SPI.h>
#include <UIPEthernet.h>
String serial_data;
char incomingByte = 0;
char s = 0;
int choice = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(10, 5, 5, 10); // numeric IP for Google (no DNS)
IPAddress ip(10, 5, 5, 106);
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 9876)) {
    Serial.println("connected");
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  Serial.println("Wcisnij a aby odczytywac pakiety");
  Serial.println("Wcisnij b aby wysylac pakiety");
}


void loop() {
Ethernet.maintain();
  // Read serial input:
  if (Serial.available() > 0)
  {
    incomingByte = Serial.read();
  }


  switch (incomingByte) {

    case 'a':
      //Serial.print("Logi");
      if (client.available())   {

        char c = client.read();

        Serial.print(c);
        Serial.print("");
      }
      if (!client.connected()) {
        client.connect(server, 9876);
      }
      break;

    case 'b':
      //Serial.print("Wpisz komendę");
      if (client.available())   {
        if (Serial.available() > 0) {
       Serial.println("Sent..");
    serial_data = String(Serial.read());
    client.print(serial_data);
        }
      }
      if (!client.connected()) {
        client.connect(server, 9876);
      }
      break;
  }
}

so if you enter in Serial Monitor bA, A is not sent?

For now the choosing option is working
i press a - i read from server
i press b i jump to "b" case
BUT i don't know how to properly send soem packages to the server

I don't know what you want to do. your code receives one character if 'a' is sent from Serial Monitor and sends one character if 'b' is sent from Serial Monitor.
you could replace if (Serial.available()) with while (Serial.available()) to send more then one character after b. but I guess even then it would not send everything because of gaps between characters sent from Serial Monitor

In case a:
I read the incoming messages from the server
In case b:
I want to SEND messages to the server

read() reads only one character.

see this example

I see they are sending packages like hits:

while (Serial.available() > 0) {
    char inChar = Serial.read();
    if (client.connected()) {
      client.print(inChar);
    }
  }

so it looks just like my code...
Tried it anyway it does not work.
I use TCPIPbuilder app to listen on that port and when i send packets from the server (fq2 camera) it appears both in this app and in my serial (when i choose "a")
But if I type in my command "b case" nothing appears in TCPIPbuilder so I guess i do not send anything...

do you have while?

Okay let's take it easier.
Let's make JUST sending option (if it works i will add it to the main program).

code:

#include <SPI.h>
#include <UIPEthernet.h>
//#include "packet.h"
String serial_data;
char incomingByte = 0;
char s = 0;
int choice = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(10, 5, 5, 10); // numeric IP for Google (no DNS)
IPAddress ip(10, 5, 5, 106);
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 9876)) {
    Serial.println("connected");
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }

 
}
void loop() {
 Ethernet.maintain();
   while (Serial.available() > 0) {
    Serial.println("Type command");
    char inChar = Serial.read();
    if (client.connected()) {
      
      client.print(inChar);
      client.write(inChar);
      Serial.println("Sent");
    }
  }
      if (!client.connected()) {
        client.connect(server, 9876);
      }

}

And below i will put a screenshot in which you see that if i send packages from my server (smart camera) the logs appear there BUT as you can see my program went through ("Sent package" and "Sent" is a confirmation and nothing appears :confused:

what did you type?

I tired everything :smiley: one letter, one number words, numbers evberything. nothing works. I think i must send it in other way, like a "package" with specific length etc but that's just my idea i do not know how to do it

you send to 10. 5. 5.10, not to 10.5.5.101

Yes and that's where i want to send it. 10.5.5.10 is the camera's adress.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.