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

<p>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?</p>

<p>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 ?</p>

[{'answer_id': 45630, 'author': 'basszero', 'author_id': 287, 'author_profile': 'https://Stackoverflow.com/users/287', 'pm_score': 5, 'selected': True, 'text': "<p>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 <code>SocketChannels</code> and/or NIO async sockets / selectors. This should get you started.</p>\n\n<pre><code>boolean finished = false;\nint port = 10000;\nServerSocket server = new ServerSocket(port);\n\nwhile (!finished) {\n    // This will block until a connection is made\n    Socket s = server.accept();\n    // Spawn off some thread (or use a thread pool) to handle this socket\n    // Server will continue to listen\n}\n</code></pre>\n"}, {'answer_id': 45678, 'author': 'Joe Liversedge', 'author_id': 4552, 'author_profile': 'https://Stackoverflow.com/users/4552', 'pm_score': 1, 'selected': False, 'text': '<p>I\'d need to go back to the basics for this one too. I\'d recommend O\'Reilly\'s excellent <em>Java in a Nutshell</em> that includes code examples for just such a case (available <a href="http://oreilly.com/catalog/javanut/examples/" rel="nofollow noreferrer">online</a> as well). See Chapter 7 for a pretty good overview of the decisions you\'d want to make early on.</p>\n'}, {'answer_id': 83460, 'author': 'Richard', 'author_id': 7740, 'author_profile': 'https://Stackoverflow.com/users/7740', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<p>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 <a href="http://blog.fupps.com/documents/blackberry/real-world-blackberry-mds-push-applications-an-extensible-framework/" rel="nofollow noreferrer">here</a> with LAMP source code. The parameter PORT=7874 in <a href="http://fupps.com/code/bberry/push/pushout.pl" rel="nofollow noreferrer">pushout.pl</a> 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. </p>\n'}, {'answer_id': 88354, 'author': 'Alexander', 'author_id': 16724, 'author_profile': 'https://Stackoverflow.com/users/16724', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n"}]
['https://Stackoverflow.com/questions/45623', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4739/']