How to check for opened ports on a Client/Server connection?

Hey bros!
I’m starting coding some programs which support two-machines connection (a Server one and a Client one, using the net library); and I’m trying to do something that, like every time I post something there, I’m not sure about if it is actually possible or not.
I was thinking about a piece of the Client-side source code, based on a cycle, which is focused on scrolling up a int variable that would check if a Server program is open at the port indicated from that variable. The issue is that I’m actually not able to come out how to write it… or well: I wrote something I thought it could work, but if I run a Server program and the Client, which searches for an opened port, it founds every time “88” (idk why but it does LOL).
And I have to add that, if I specify a particular Client port (in where I previously opened a Server program), the program itself actually works perfectly and does everything it should do. So… anyone who can help?

I’m pasting here the solution I was trying to develop:

  for (int a=0; a<10000; a++) { //from 0 to 10000 because it only uses 4 digits
    
    myClient=new Client(this, "127.0.0.1", a);
    if (myClient.active()) { //checks if the current 'a' value is an open Server port
      println("Found an open server port at: "+a);
      break; //exits from the cycle and uses "a"'s current value as a port for the Client
    }
  }
1 Like

It’s a little unclear what you’re trying to accomplish. Perhaps if you posted the server and client code in their entirety or at least the relevant parts, it would become clearer?

If you attempt to open a connection (network socket) to another machine (or the same one) and it happens to be accepting connections at the specified port then in theory you would have an active connection until the client disconnected, the server kicked you, or something else caused connection loss (e.g. a timeout).

Simply looking for any server on any port that accepts a basic connection, however temporarily, is effectively port scanning.

1 Like

Ok, I’m trying to clarify the problem a little more.
The Server-side of the problem is OK, looks good to me and works perfectly. When I open the Client-side of the program, it should cycle a number and check for it if there’s an open Server that runs on that port… I don’t know how to explain well sorry

I’m pasting here all the code for the two programs:

//Server program - works fine, no problems
import processing.net.*;

Server myServer;
int val=0, portLength=4;
int port[]=new int[portLength], newP;

void setup() {
  
  size(300, 200);
  colorMode(HSB);
  textAlign(CENTER, CENTER);
  for (int a=0; a<portLength; a++)
    port[a]=(int)random(0, 10); //it generates an array of 4 digits which stands as the port to open the Server
  for (int a=portLength-1; a>=0; a--) {
    newP+=port[a]*pow(10, a); //creates the int variable to create the port number
  }
  println("Opened a server port at: "+newP);
  myServer = new Server(this, newP); //newP is the port generated before
}

void draw() {
  //here I just verify if the Server works
  //it does perfectly! :)
  
  val++;
  if (val>255) val=0;
  background(val, 255, 255);
  fill(128-val, 255, 255);
  text("Sending the background hue value: "+val, width/2, height/2);
  myServer.write(val);
}
//Client program - issues in finding the server to connect
import processing.net.*; 

Client myClient; 
int dataIn, port; 
 
void setup() { 
  
  size(300, 200);
  colorMode(HSB);
  textAlign(CENTER, CENTER);
  for (int a=0; a<10000; a++) {
    //this cycle doesn't work as it should:
    //"a" is the variable which cycles from 0 to 9999 (4 digits in the port serial number)
    //and every time the program checks if there is an open Server program at that port
    //the issue comes out in the check, because it does not find a port
    //(I changed the PC and it finds every time 22 as an open Server)
    
    myClient=new Client(this, "127.0.0.1", a); //tries to connect at a Server opened at port "a"
    if (myClient.active()) { //if the Server exists, so it is still active at the check...
      println("Found an open server port at: "+a);
      break; //exits from the cycle and keeps "a" current value as the port number
    }
  }
} 
 
void draw() {
  //from there and over the program works properly:
  //if I open a Server program at a random-generated port (with the system in the Server code)
  //and I try to connect manually at that server, it works perfectly
  //(you can try yourself opening a random Server and connecting this program to the port generated before)

  if (myClient.available()>0) { 
    dataIn=myClient.read(); 
  } 
  background(dataIn, 255, 255);
  fill(128-dataIn, 255, 255);
  text("Current background hue value: "+dataIn, width/2, height/2);
} 

I hope that now the problem is a bit more clear, also because the situation shows that the issue is in the check of the open Server (in the Client side). Waiting for answers, thx from now!

1 Like

Thanks for the clarification.

So, you have the ‘server’ code which basically generates a random port number between 0 and 9999 and starts a server. It just sits around drawing the screen and text in contrasting colors and broadcasting the current background color.

Then you have ‘client’ code which tries to locate your server by literally testing every port on your machine blindly hoping to find an open connection. And then it just reports the first one it finds, assumes it found the expected server and tries to match the display of what’s on the other end.

I think I can see where the problems are. Unfortunately you’ve got a few problems here.

First, your code will stop searching the moment it finds an open port (that is an active server accepting connections). Secondly that could be any sort of server, because you have no way of knowing what you’ve connected to. Thirdly as long as you’re reading just a single byte at a time and using it, any data you receive will seem valid, even if it’s just a ‘what the heck’ response from the server you’ve connected to because 8-bit bytes (values 0 - 255) are the smallest unit of transfer afaik.


So, for starters, I would advise you change your port range.

You should start by limiting the range to 1024 - 49151 (out of 0 - 65535) because of how ICANN has decided to assign ports to common usages.

The lower end should be 1024+ because 0 - 1023 are already established as standard ports for a variety of common servers like SSH (secure shell, 22), Telnet (23), SMTP (25), DNS (59), NTP (123), etc. There are all kinds of server and machines using those ports every single day for stuff. This is less of an issue with connecting to yourself, unless you happen to be running a bunch of personal servers on that machine. But it’s good practice to avoid those.

The upper end is likewise assigned to be used by stuff making temporary connections on semi-random ports, that is services which choose a port to use on startup, etc. You could safe go up to the end of this in general since you are doing something of that sort, but you could also avoid the change something running in the background on your system is using one of them.

Most probably this why you are getting stuck on port 22, since it may well be that you have an SSH server running in the background already. That is especially likely if you are using a Raspberry Pi or a system running Unix/Linux/Android/etc or even possibly Windows 10.

Basically you want to be fairly sure that there is next to no chance that you’ll find anything but the server you started.

Alternately you can try to create a basic protocol/message structure that will allow you to verify that you are talking to your own code before you proceed further. And while I would still avoid ports below 1024, you’d at least have a decent chance of avoiding anything that’s already running and isn’t your code.

It should be just as easy to ask for a random number between (1024, 49151) as to do what you were doing.

P.S.


3 Likes

thx so much, I restricted the field of the port generator and it worked properly!

The only other issue I have is that actually it takes a very long time to it (Client) for scan in every single port, so I’m gonna work to it; but for the moment it is enough for me to have a working connection between programs!

Glad you got it working.

The other issue is inevitable unless you quit blindly selecting a port and leaving it up to the client to find it. It would be much, much easier to simply use the same port every time unless for some reason you want to set up a bunch of different server instances on the same machine and have different clients connect to different ones. Because in that case you’d either need different ports or have to make the code more sophisticated in order to handle multiple clients.

You could make of course make it a smaller range of ports. Rather than 48,000+ ports, you could restrict yourself to a thousand, a few hundred, or even as small as ten or twenty. Keeping in mind the above hypothetical situation there probably is a lower threshold to reduce the chance of having to introduce more code for when two instances inevitably choose the same port.

It might be possible with a less random selection mechanism to ensure that some algorithmic approach could be used to narrow down choices quickly. But that would generally involve deliberately introducing some piece of shared information up front such as only using even/odd numbered ports or only ports in the fibonacci sequence, etc.

1 Like