Skip to content

Add possibility to change server tcp port in void setup. No more fixed port! #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions examples/WebServer/WebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ IPAddress ip(192, 168, 1, 177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
//EthernetServer server(80);
// User can now posticipate the port declaration in void setup()
EthernetServer server();

void setup() {
// Open serial communications and wait for port to open:
Expand All @@ -42,7 +44,9 @@ void setup() {

// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
//server.begin();
//The tcp port used by server now can be a result of a eeprom.read (for example) or a normal int
server.begin(80);
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
Expand Down
11 changes: 9 additions & 2 deletions src/EthernetServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ extern "C" {
#include "EthernetClient.h"
#include "EthernetServer.h"

EthernetServer::EthernetServer(uint16_t port)
EthernetServer::EthernetServer(uint16_t port)
{
_port = port;
}

void EthernetServer::begin()
EthernetServer::EthernetServer() {}

void EthernetServer::begin(uint16_t port)
{
_port = port;
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
EthernetClient client(sock);
if (client.status() == SnSR::CLOSED) {
Expand All @@ -26,6 +29,10 @@ void EthernetServer::begin()
}
}

void EthernetServer::begin() {
begin(_port);
}

void EthernetServer::accept()
{
int listening = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/EthernetServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public Server {
void accept();
public:
EthernetServer(uint16_t);
EthernetServer();
EthernetClient available();
virtual void begin(uint16_t);
virtual void begin();
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buf, size_t size);
Expand Down