Ethernet multiple client ports

Hi guys,
I am using the Intel Galileo gen 1 board.
I am trying to open multiple EthernetClients in order to simulate more than one client on the galileo for a school project.
I have tried to declare them as follows:

EthernetClient client(90);
EthernetClient client2(100);

but when executing client.connect(, ) both of them fail to connect.

When running the same code with only one client declared as:
EthernetClient client;
it connects well on both times.

Also tried declaring:
EthernetClient client;
EthernetClient client2;

But the Galileo crashed when trying to connect.

Does anyone have an solution for my problem?
Thanks.

This worked for me on an UNO with an ethernet shield.

// port 80 for browser, port 58888 for data collection:

EthernetServer serv1(80);
EthernetServer serv2(58888);

then:

EthernetClient client = serv1.available();
    if (client){
        Serial.println("new browser client");

or

client = serv2.available(); 
    if (client){
        Serial.println("new database client");

That was a long while ago when I did that and now I see a few programming issues! But it has been working like that for a few years on many Uno's doing data collection.

Thanks jogl8 for answering, But my problem is creating two clients not two servers. In my project I am using the Galileo for simulating different things controlled by a server. The goal is to be able to execute something like:

IPAddress server (111, 222, 333, 444); // Server IP
#define SERVER_PORT 80
EthernetClient client1;
EthernetClient client2;

:

if(client.connect(server , SERVER_PORT )
{
  // Send http message, don't read reply
}
else if(client2.connect(server , SERVER_PORT ))
{
  // Send http message, don't read reply
}

:

//Check for reply
while(client.available())
{
  char c = client.read();
  client.print(c);
}

while(client2.available())
{
  char c = client2.read();
  client2.print(c);
}
    }

This is just a simple draft of course. The important thing is to have two clients link to different outgoing ports. The server is multi-threaded and redirects each incoming connection to a separate port.

I will be glad to hear any guidelines or comments :) Thanks

Below is some arduino quickly copy/paste two client test code (uses the same remote server) that seems to work sort of.

//zoomkat 01-10-15
//simple two client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e or g to test client GET
//for use with W5100 based ethernet shields
//remove SD card if inserted
//data from myIP server captured in readString 

#include <SPI.h>
#include <Ethernet.h>
String readString;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName1[] = "checkip.dyndns.com"; // myIP server test web page server
char serverName2[] = "checkip.dyndns.com"; // myIP server test web page server
EthernetClient client1;
EthernetClient client2;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println(F("Failed to configure Ethernet using DHCP"));
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println(F("two client readString test 01/10/15")); // so I can keep track of what is loaded
  Serial.println(F("Send an e or g in serial monitor to test")); // what to do to test
  Serial.println();
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET1(); // call sendGET1 function below when byte is an e
    }
    if(inChar == 'g') // checks to see byte is an e
    {
      sendGET2(); // call sendGET2 function below when byte is a g
    }    
   }  
} 

////////// client 1

void sendGET1() //client function to send/receive GET request data.
{
  if (client1.connect(serverName1, 80)) {  //starts client connection, checks for connection
    Serial.println(F("connected"));
    client1.println(F("GET / HTTP/1.1")); //download text
    client1.println(F("Host: checkip.dyndns.com"));
    client1.println(F("Connection: close"));  //close 1.1 persistent connection  
    client1.println(); //end of get request
  } 
  else {
    Serial.println(F("connection failed")); //error message if no client connect
    Serial.println();
  }

  while(client1.connected() && !client1.available()) delay(1); //waits for data
  while (client1.connected() || client1.available()) { //connected or data available
    char c = client1.read(); //gets byte from ethernet buffer
    readString += c; //places captured byte in readString
  }

  //Serial.println();
  client1.stop(); //stop client
  Serial.println(F("client 1 disconnected."));
  Serial.println(F("Data from server 1 captured in readString:"));
  Serial.println();
  Serial.print(readString); //prints readString to serial monitor 
  Serial.println();  
  Serial.println();
  Serial.println(F("End of readString 1"));
  Serial.println(F("=================="));
  Serial.println();
  readString=""; //clear readString variable
}

/////// client 2

void sendGET2() //client function to send/receive GET request data.
{
  if (client2.connect(serverName2, 80)) {  //starts client connection, checks for connection
    Serial.println(F("client 2 connected"));
    client2.println(F("GET / HTTP/1.1")); //download text
    client2.println(F("Host: checkip.dyndns.com"));
    client2.println(F("Connection: close"));  //close 1.1 persistent connection  
    client2.println(); //end of get request
  } 
  else {
    Serial.println(F("connection 2 failed")); //error message if no client connect
    Serial.println();
  }

  while(client2.connected() && !client2.available()) delay(1); //waits for data
  while (client2.connected() || client2.available()) { //connected or data available
    char c = client2.read(); //gets byte from ethernet buffer
    readString += c; //places captured byte in readString
  }

  //Serial.println();
  client2.stop(); //stop client
  Serial.println(F("client 2 disconnected."));
  Serial.println(F("Data from server 2 captured in readString:"));
  Serial.println();
  Serial.print(readString); //prints readString to serial monitor 
  Serial.println();  
  Serial.println();
  Serial.println(F("End of readString 2"));
  Serial.println(F("=================="));
  Serial.println();
  readString=""; //clear readString variable
}

This looks like what I was looking for. Thanks! Do I have to close the first connection before switching to the other one? The ideal thing for me will be keeping both connections alive the entire run.

The ideal thing for me will be keeping both connections alive the entire run.

Can the Galileo multitask?

If by referring multitasking you imply multi-threading / multi-processes than I think no. I am trying to keep two TCP streams open and send and receive data on both of them. I will try the code you suggested in couple of days when I will be working on the Galileo. Thanks.

In 2014, I did hacks to the Ethernet library to allow you to listen for multiple connections on the same port.

http://subethasoftware.com/2013/04/09/arduino-ethernet-and-multiple-socket-server-connections/

Currently, if you listen on a port (like telnet PORT 23) and get a connection, any other connection attempts to that port hang while it is in use. My modifications allow future connections to also be handled. I provide a simple example that sends other connections a message like "System busy. Try back later." But, in my programs, I am using it so multiple telnets can be handled at the same time.

I need to see if it is possible to get the Arduino team to look at my hack and see if it can be implemented.