0

Kicking it old school: I'm using Java SE 5 (or java v1.5) (please don't tell me to upgrade, because for what I'm working on [which is private] I need to use this version of java).

I need help setting up a web client/server application. Every variation I've looked for online has narrowed down to using websockets/sockets, however the version of java I'm using does not have sockets yet.

Does anyone have a tutorial for setting up a client/server model application not using sockets, or some sample code that can point me in the correct direction?

EDIT: Okay, so apparently my version of java does have sockets. I'll look into that more. However, now I'm just curious, how does one create a java server not using sockets?

Kara
  • 6,115
  • 16
  • 50
  • 57
EndingWithAli
  • 449
  • 1
  • 5
  • 11
  • 9
    Of course Java 5 has sockets, they've been around since JDK 1.0: http://docs.oracle.com/javase/1.5.0/docs/api/java/net/Socket.html – AntonH Jul 07 '14 at 17:57
  • Java v1.5 has sockets... – Jashaszun Jul 07 '14 at 17:58
  • `client/server` implies you are using a `connection based` protocol of which `TCP` is the most common. The way to use TCP is with plain old `Socket` or NIO `SocketChannels` both have been around for years before Java 5.0. If you upgraded to Java 7 you could use NIO2, but I wouldn't bother unless you have Infiniband (most likely, you don't) – Peter Lawrey Jul 07 '14 at 18:02

2 Answers2

3

You need sockets. They serve as the end-points to the connection. Although, you can use alternatives to the blocking Socket and ServerSocket implementations.

NIO (New IO) uses channels. It allows for non-blocking I/O:

class Server {
     public static void main(String[] args) throws Exception {
          ServerSocketChannel ssc = ServerSocketChannel.open();
          ssc.configureBlocking(false);

          while(true) {
               SocketChannel sc = ssc.accept();

               if(sc != null) {
                    //handle channel
               }
          }
     }
}

Using a Selector (optional, but recommended)

You could use a Selector to switch between reading/writing/accepting to allow blocking only when theres nothing to do (to remove excess CPU usage)

class Server {
     private static Selector selector;
     public static void main(String[] args) throws Exception {
          ServerSocketChannel ssc = ServerSocketChannel.open();
          ssc.configureBlocking(false);
          Selector selector = Selector.open();
          ssc.register(selector, SelectionKey.OP_ACCEPT);

          while(true) {
               selector.select();

               while(selector.iterator().hasNext()) {
                    SelectionKey key = selector.iterator().next();

                    if(key.isAcceptable()) {
                         accept(key);
                    }
               }
          }
     }

     private static void accept(SelectionKey key) throws Exception {
          ServerSocketChannel channel = (ServerSocketChannel) key.channel();
          SocketChannel sc = channel.accept();
          sc.register(selector, OP_READ);
     }
}

SelectionKey supports accepting via isAcceptable(), reading via isReadable() and writing via isWritable(), so you can handle all 3 on the same thread.

This is a basic example of accepting a connection using NIO. I highly suggest looking into it a bit more to get a better understanding of how things work.

Vince
  • 14,470
  • 7
  • 39
  • 84
2

Here's the API documentation of the class Socket from Java 5.

Sockets are such a basic abstraction of network communication that you'd be hard pushed to find an OS which doesn't support them, let alone a high level programming language.

biziclop
  • 48,926
  • 12
  • 77
  • 104
  • 3
    +1 In case there was any doubt, Javadoc says `Since: JDK1.0` – Peter Lawrey Jul 07 '14 at 18:03
  • I upvoted this since it answered the original question, but please see the edit for what the answer should be now. – Ricky Mutschlechner Jul 07 '14 at 18:15
  • 1
    @RickyMutschlechner The answer is you can't. As I said, sockets are a network abstraction that is handled on the OS level already, the only way to avoid using them is to program the network interface layer directly, essentially writing a new device driver for your network card. And even then due to the way IP addressing works, you'll probably end up with something that is a socket in all but name. – biziclop Jul 07 '14 at 19:04