views:

674

answers:

4

In Java, what would the best way be to have a constantly listening port open, and still send upon receipt of a packet. I'm not particularly savvy with network programming at the moment, so the tutorials I've found on the net aren't particularly helpful.

Would it make sense to have the listening socket as a serversocket and run it in a separate thread to the socket I'm using to send data to the server?

In a loosely related question. Does anyone know if programming simply for java, in netbeans then exporting it for use on a blackberry (using a plugin) the sockets would still work?

Thanks in advance for any responses =]

+11  A: 

If you can afford the threading, try this (keep in mind I've left out some details like exception handling and playing nice with threads). You may want to look into SocketChannels and/or NIO async sockets / selectors. This should get you started.

boolean finished = false;
int port = 10000;
ServerSocket server = new ServerSocket(port);

while(!finished)
{
    // This will block until a connection is made
    Socket s = server.accept();
    // Spawn off some thread (or use a thread pool) to handle this socket
    // Server will continue to listen
}
basszero
+1, although for a beginner this is enough, he should **not** try to look into NIO
flybywire
+1  A: 

I'd need to go back to the basics for this one too. I'd recommend O'Reilly's excellent Java in a Nutshell that includes code examples for just such a case (available online as well). See Chapter 7 for a pretty good overview of the decisions you'd want to make early on.

Joe Liversedge
+1  A: 

As for connecting to a Blackberry, this is problematic since in most cases the Blackberry won't have a public IP address and will instead be behind a WAP gateway or wireless provider access point server. RIM provides the Mobile Data Server (MDS) to get around this and provide "Push" data which uses ServerSocket semantics on the Blackberry. The MDS is available with the Blackberry Enterprise Server (BES) and the Unite Server.

Once set up data can be sent to a particular unit via the MDS using the HTTP protocol. There is an excellent description of the Push protocol here with LAMP source code. The parameter PORT=7874 in pushout.pl connects to the Blackberry Browser Push server socket. By changing that parameter the payload can be sent to an arbitrary port where your own ServerSocket is accepting connections.

Richard
+1  A: 

If your socket code has to run on a BlackBerry, you cannot using standard Java sockets. You have to use the J2ME Connector.open API for creating both types of sockets (those that initiate connections from the BlackBerry, and those that listen for connections/pushes on the BlackBerry). Have a look at the examples that come with RIM's JDE.

Alexander