Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » U++ users applications in progress and useful code snippets, including reference examples! » Find an available/free port - code snippet (TcpSocket, port)
icon3.gif  Find an available/free port - code snippet [message #57943] Fri, 31 December 2021 13:18 Go to next message
Mountacir is currently offline  Mountacir
Messages: 45
Registered: November 2021
Member
Hello,

I needed to get an available/free port for localhost, so I used the code below with a derived TcpSocket class.
I'm wondering if it is possible to implement it in TcpSocket?



int TcpSocket::GetFreePort(){
	
	socklen_t addrLen;
	int s  = ::socket(AF_INET, SOCK_STREAM, 0);

    if (s == -1) {
        LOG("Failed to create socket");
    }
	
	sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = 0;
	
	//  INADDR_ANY as the default parameter?
	//sin.sin_addr.s_addr = htonl(INADDR_ANY);
	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
	
	if (bind(s, (const sockaddr *)&sin, sizeof(sin)) == -1) {
        LOG("Failed to bind");
    }
    addrLen = sizeof(sin);
    if (getsockname(s, (struct sockaddr *)&sin, &addrLen) == -1) {
        LOG("getsockname() failed");
    }
    LOG("port : " << sin.sin_port);

	return sin.sin_port;
}



Thanks!

[Updated on: Sun, 23 January 2022 19:05] by Moderator

Report message to a moderator

Re: Find an available/free port [message #58035 is a reply to message #57943] Sat, 22 January 2022 19:14 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Ausin2029 wrote on Fri, 31 December 2021 13:18
Hello,

I needed to get an available/free port for localhost, so I used the code below with a derived TcpSocket class.
I'm wondering if it is possible to implement it in TcpSocket?



int TcpSocket::GetFreePort(){
	
	socklen_t addrLen;
	int s  = ::socket(AF_INET, SOCK_STREAM, 0);

    if (s == -1) {
        LOG("Failed to create socket");
    }
	
	sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = 0;
	
	//  INADDR_ANY as the default parameter?
	//sin.sin_addr.s_addr = htonl(INADDR_ANY);
	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
	
	if (bind(s, (const sockaddr *)&sin, sizeof(sin)) == -1) {
        LOG("Failed to bind");
    }
    addrLen = sizeof(sin);
    if (getsockname(s, (struct sockaddr *)&sin, &addrLen) == -1) {
        LOG("getsockname() failed");
    }
    LOG("port : " << sin.sin_port);

	return sin.sin_port;
}



Thanks!


This is a good snippet of core, but I am not quite sure how is it related to TcpSocket instance?
Re: Find an available/free port [message #58042 is a reply to message #58035] Sun, 23 January 2022 07:56 Go to previous messageGo to next message
Mountacir is currently offline  Mountacir
Messages: 45
Registered: November 2021
Member
Quote:
This is a good snippet of core, but I am not quite sure how is it related to TcpSocket instance?


I started making a plugin for Selenium WebDrivers and i used TcpSocket for the communication. So instead of relying on the default port used by each Driver or randomly assigning one, i made the above code work with TcpSocket.

Should I remove this post and repost in "U++ users applications in progress..." as just a useful code snippet?

Thanks
Re: Find an available/free port [message #58043 is a reply to message #58042] Sun, 23 January 2022 19:06 Go to previous message
Klugier is currently offline  Klugier
Messages: 1076
Registered: September 2012
Location: Poland, Kraków
Senior Contributor
Hello Ausin,

I moved your topic to "U++ users applications in progress and useful code snippets, including reference examples!" subforum.

Klugier


U++ - one framework to rule them all.

[Updated on: Sun, 23 January 2022 19:47]

Report message to a moderator

Previous Topic: Horizontal Cursor for CodeEditor/LineEdit
Next Topic: SUniGuiCreator
Goto Forum:
  


Current Time: Tue Apr 16 14:57:28 CEST 2024

Total time taken to generate the page: 0.02460 seconds