Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 168
Registered: ‎11-22-2009
My Device: 9000 Bold, 8300 Curve

What ports are "safe" to use for a SocketConnection?

[ Edited ]

I'm writing a program that will allow a device to allow a direct socket connection with a server. Is there certain ports that are reccommend for this sort of thing and other ports that I should avoid? This is for an incoming connection to the device.

 

I have been testing with port 6884 and I get an IOException,  General Socket Error.

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: What ports are "safe" to use for a SocketConnection?

Just double-checking, are you really talking about a TCP connection established from the server to a BlackBerry and not the other way around (i.e., connection is established from the BlackBerry to the server)?

Developer
Posts: 168
Registered: ‎11-22-2009
My Device: 9000 Bold, 8300 Curve

Re: What ports are "safe" to use for a SocketConnection?

[ Edited ]

I'm talking about opening a port on the Blackberry to receive data from an outside source.

 

I now realize from some additional research since I made this post that what I'm asking for is likely not possible using WAP2 or a direct connection. Based on one of the threads though it looks like I could use wi-fi to circumvent this since I could get a real ip address. For the type of app that I'm building wi-fi would be the preferable method of usage anyways. Does anyone have any comment on if this would work?

Developer
Posts: 54
Registered: ‎10-23-2008
My Device: Not Specified

Re: What ports are "safe" to use for a SocketConnection?

If I am understanding your question correctly you are looking to send data to the device where the source IP is somewhere on the internet and the destination IP is your device?

 

Most (if not all) WiFi connections will give you an internal (non routable) network IP address such as 192.168.x.x or 10.x.x.x which is NAT'ed on the way out through the router/firewall to the internet. Unless the router/firewall is configured to do port forwarding for incoming packets back to your device IP address (which seems unlikely as you are probably being allocated an IP via DHCP) you wont receive the packets on the device.

Developer
Posts: 709
Registered: ‎09-10-2009
My Device: 8520, 8900, 9000, 9300, 9650, 9700, 9780, 9800, 9810, 9900, 9930
My Carrier: Verizon

Re: What ports are "safe" to use for a SocketConnection?

[ Edited ]

 


schepurny wrote:

I'm talking about opening a port on the Blackberry to receive data from an outside source.

 

I now realize from some additional research since I made this post that what I'm asking for is likely not possible using WAP2 or a direct connection. Based on one of the threads though it looks like I could use wi-fi to circumvent this since I could get a real ip address. For the type of app that I'm building wi-fi would be the preferable method of usage anyways. Does anyone have any comment on if this would work?


 

There is no way or very very difficult to do this in TCP. So that leave you UDP as the only option. You can create a UDP socket on the device with any server port. However, that server port might not be the same one exposed to the public internet. To find out the public IP address and port. From the socket you just created, query a STUN server for that information and send that information to some kind of registration server with a unique name of you choice. Now the client, from the internet, can query the same registration server with the unique name of your device to find out the IP address and port, and start communicating with your device. Remember, this is using UDP. reliable data transmission is not guaranteed.

 

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: What ports are "safe" to use for a SocketConnection?

[ Edited ]

I would say it doesn't matter whether it's TCP or UDP. The core issue is NATing which affects reachability both over UDP and TCP very similarly. As previous posters correctly pointed out, both over cellular and Wi-Fi interfaces, you usually end up in a private network with NATed Internet access. This usually means it's hard/impossible to access the BlackBerry from outside unless it (puches a hole in the firewall) sends some outgoing data first to the destination which wants to access it.

 

P.S. Some cellular carriers offer non-NATed Internet access (or access to the corporate network via a private APN), but this is quite rare and usually costs extra.

 

P.P.S. Why not solve the reachability issue by establishing a connection (TCP or UDP) from BlackBerry to the server and keeping the connection alive by periodically sending and receiving data (TCP is better in this case, as the keep-alive interval can be much larger than for UDP). This will ensure that the server can send a request to the BlackBerry anytime. Keeping the connection working will drain the battery quicker though... To save battery, instead of keeping a connection open at all times, you could rely on BIS or MDS push for sending data from the server to BlackBerry.

Developer
Posts: 709
Registered: ‎09-10-2009
My Device: 8520, 8900, 9000, 9300, 9650, 9700, 9780, 9800, 9810, 9900, 9930
My Carrier: Verizon

Re: What ports are "safe" to use for a SocketConnection?

kylubin,  if you are talking about a private coorperate network, all bets are off. I have seen some of those networks have everything blocked and only allow 80 and http to go out. UDP has no problem as long as NAT is available (STUN will help you to find out what the public IP address and port are. There is no need to use non-NATed network). A few days ago when I was updating my BB, I accidentally deleted the BIS entry in service book. However,  my UPD communicating application was still working eventhough the browser was not able to goto sites that does not use port 80.  Maintaining a 24/7 TCP connection is possible solution, but that gets complicated when more than one client needs to be connected to the phone.

 

 

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: What ports are "safe" to use for a SocketConnection?

Aviator168, are you talking about making your BlackBerry accessible via UDP from the outside using STUN? Unless I'm missing something, I guess you're lucky that your cellular carrier has a weak-enough (i.e., non-symmetric) NAT. I also wonder how many cellular carriers' Internet access APN has a NAT that is OK for STUN.

Developer
Posts: 709
Registered: ‎09-10-2009
My Device: 8520, 8900, 9000, 9300, 9650, 9700, 9780, 9800, 9810, 9900, 9930
My Carrier: Verizon

Re: What ports are "safe" to use for a SocketConnection?

[ Edited ]

No. STUN is just a service which allowed a device (phone or computer) to discover a UDP socket's external IP address and port. Once addr/port is known, the device can register this info with a server. If the router allows NAT for UDP, it will keep its entry for a few minutes or whatever time set by the network admin. The trick is that the device has to query the STUN server periodically so that if the NAT entry is deleted by the router (expired), a NAT entry can be re-established, and if the external IP addr/port are changed, update the entry in the registration server. Also, this is how part of SIP protocol works.

 

P.S. My carrier is AT&T. I also has tested my application on other networks (Verison, T-Mobile, Spirint PCS), and they all work the same way.

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: What ports are "safe" to use for a SocketConnection?

But then if the NAT is symmetric, then you either have to use the STUN as a relay for all the data (meaning you're no worse than using a TCP relay), or it won't really work. I guess you carrier's NAT is not symmetric. Anyway, it's nice to have a list of carriers that work with STUN!