Jump to content
  • Advertisement
Sign in to follow this  

What is a good port to listen to/send data to?

This topic is 3118 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, My application hast to communicate with a server for activation. That works fine, but sometimes the connection fails because the institution (e.g. Universities) block specific ports. So my question is: what is a good port that has a better chance to be open than others? Currently I am using port 20248. As a side question: how can I listen to two (or more) ports at the same time without having more than one thread running? (that is in order to support the old port connection and the new one) This is what I currently have:
	if (WSAStartup(0x101,&wsaData)!=0)
		return(false);	 // WSAStartup failed.
	local.sin_family=AF_INET;
	local.sin_addr.s_addr=INADDR_ANY;
	local.sin_port=htons((u_short)_connectionPort);
	server=socket(AF_INET,SOCK_STREAM,0);
	if (server==INVALID_SOCKET)
		return(false); // socket failed.
	if (bind(server,(sockaddr*)&local,sizeof(local))!=0)
		return(false); // bind failed.
	if (listen(server,10)!=0)
		return(false); // listen failed.
	sockaddr_in from;
	int fromlen=sizeof(from);
	client=accept(server,(struct sockaddr*)&from,&fromlen);
	fd_set theSet;
	timeval timeOut;
	timeOut.tv_sec=1;
	timeOut.tv_usec=0;
	FD_ZERO(&theSet); 
	FD_SET(client,&theSet); 
	int p=0;
	while (select(0,&theSet,NULL,NULL,&timeOut)==1)
		p+=recv(client,&receivedData[0]+p,_packetSize-p,0);

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by floatingwoods
Hello,

My application hast to communicate with a server for activation. That works fine, but sometimes the connection fails because the institution (e.g. Universities) block specific ports. So my question is: what is a good port that has a better chance to be open than others?

Currently I am using port 20248.
Anything < 1024 is likely to be blocked, since that's a range of ports which requires root permissions to bind to on UNIX. To be honest, I don't think there's any particular port that's more likely to be open than any other; just avoid ports that are in use by other applications (Or explicitly use one if the university allows that application to work).
A better solution would be to make the port number configurable as a command line switch or settings file value.

EDIT #2: Oh wait, you're talking about outgoing connections - the above applies to incoming connections.

Quote:
Original post by floatingwoods
As a side question: how can I listen to two (or more) ports at the same time without having more than one thread running? (that is in order to support the old port connection and the new one)
You can put multiple sockets into one fd_set, and when select() says there's a connection, see which socket(s) is/are in the fd_set for accepting.

EDIT: Oh, you're not using select() to see when you can accept() - you can call select() to see if a socket has a connection pending, because that socket will be marked as readable.

Share this post


Link to post
Share on other sites
Quote:
Original post by floatingwoods
So my question is: what is a good port that has a better chance to be open than others?

Well, obviously avoid ports used by popular P2P applications (to name a few: BitTorrent, Limewire, Kazaa, Soulseek, DC++). I haven't had any direct experience with University firewalls, so I can't say much else in this regard.

Also, you probably want to avoid allocated port numbers.

That said, if you really need to ensure your application will make it past a firewall, considering using HTTP as your protocol. Of course you would need to abuse it if you needed a reasonably long-lasting connection (Comet).

Quote:
As a side question: how can I listen to two (or more) ports at the same time without having more than one thread running? (that is in order to support the old port connection and the new one)

This is what I currently have:

*** Source Snippet Removed ***


What you want to do is listen on two sockets, and then poll by putting them in the readfds set in a call to select. When they are marked as readable, a connection can be accepted from them, and you can go back to polling (presumably after adding this new connection to the appropriate sets for the next select, if you want to process the connection in the same single thread).

Share this post


Link to post
Share on other sites
Thank you for the replies!

Evil Steve: forgive my ignorance, but a firewall (or other means of protecting a computer/network) can block incoming as well as outgoing ports, right?

mattd: all I need is send exactly 320 bytes to a server and receive 320 bytes in reply, just once to activate the software. Using HTTP as protocol should then not be a problem I guess. Do you know of any simple class/library for that? (I am a perfect ignorant when it goes beyond desktop applications ;) )

Share this post


Link to post
Share on other sites
Quote:
Original post by floatingwoods
Thank you for the replies!

Evil Steve: forgive my ignorance, but a firewall (or other means of protecting a computer/network) can block incoming as well as outgoing ports, right?

Just to answer for Steve: Yes, they can (and probably will).

Quote:
mattd: all I need is send exactly 320 bytes to a server and receive 320 bytes in reply, just once to activate the software. Using HTTP as protocol should then not be a problem I guess. Do you know of any simple class/library for that? (I am a perfect ignorant when it goes beyond desktop applications ;) )

libcurl is the standard in this area from what I've seen. It has an 'easy' interface which should make doing a simple GET/POST to your activation server simple.

The other nice thing about using HTTP is that your activation server can be written in any old web framework, and run on any standard web hosting. No custom application and VPS/whatever needed. You could even link to OpenSSL and throw in SSL (HTTPS) to protect against casual users sniffing any exciting parts of your activation process (but then again, the firewall might come back into play and block HTTPS)

Share this post


Link to post
Share on other sites
Thank you mattd,

Libcurl seems to be what I need.
On the client side I would have something like:

curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://anAddresseHere.com");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "Hello world!");
res = curl_easy_perform(curl);
// How to receive data back??
curl_easy_cleanup(curl);
}



But what would I have on the server side? (c++ application, no actual website)
Is there some command like "receiveIncomingData" and "replyToIncomingData" for the server side? And something like "receiveReplyData" for the client side?

Share this post


Link to post
Share on other sites
Re: "// How to receive data back??":

I think you set up a CURLOPT_WRITEFUNCTION callback using curl_easy_setopt.

Quote:
Original post by floatingwoods
But what would I have on the server side? (c++ application, no actual website)
Is there some command like "receiveIncomingData" and "replyToIncomingData" for the server side? And something like "receiveReplyData" for the client side?


Ah, no, libcurl is client-side only. That's why I suggested using a normal/lightweight web server and something like Python/PHP/Ruby :)

Try searching for a small embeddable web server. Mongoose is MIT-licensed, looks really lightweight (only 1 .c source file!) and simple to use.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!