Sockets and processes in Java

In Java, what is best would be to have an open listening port, and still send it after receiving the packet. At the moment, I'm not particularly good at network programming, so the tutorials I found on the network are not particularly useful.

Does it make sense to have a listening socket as the server core and run it in a separate thread in the socket that I use to send data to the server?

Regarding a loosely related issue. Does anyone know if programming just for java, in netbeans, then exporting it for use on a blackberry (using the plugin), sockets will still work?

+4
source share
4 answers

If you can afford threads, try this (keep in mind that I didn’t take into account some details, such as handling exceptions and playing with beautiful threads). You might want to look into SocketChannels and / or NIO sockets / selectors. That 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 } 
+12
source

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

Once configured, data can be sent to a specific block via MDS using the HTTP protocol. There is an excellent description of the Push protocol here with the LAMP source code. The PORT = 7874 parameter in pushout.pl connects to the BlackBerry Browser Push socket server. By changing this parameter, the payload can be sent to an arbitrary port where your own server server accepts connections.

+2
source

If your socket code should run on BlackBerry, you cannot use standard Java sockets. You must use the J2ME Connector.open API to create both types of sockets (those that initiate connections with BlackBerry, and those that listen for connections / click on BlackBerry). Check out the examples that ship with RIM JDE.

+2
source

I also need to get back to the basics of this. I would recommend O'Reilly excellent Java in a nutshell, including code samples for such a case (available online ). See Chapter 7 for a pretty good overview of the decisions you want to make early on.

+1
source

Source: https://habr.com/ru/post/1276555/


All Articles