![]() |
Java SocketsSocket programming made easyJava Sockets is a class library implementing a subset of the C++ Sockets library, and is based on SUN's java.nio.* non-blocking network i/o classes. This software is released under the GNU General public license (GPL). Requires J2SE 5.0. DownloadAll Java Sockets classes are in the net.alhem.jsockets.* package. JavaSockets-0.1.jar 2004-10-29 JavaSockets-0.1-src.tar.gz 2005-03-13 First release 0.1 caveatThere are lots of things that still need work. For the most fatal differences compared to the C++ sockets library, see this section further down this page. DocumentationCode samplesSimple server public class MySocket extends TcpSocket { public MySocket(SocketHandler h) { super(h); SetLineProtocol(); } public void OnAccept() { Send("Welcome\r\n"); } public void OnLine(String line) { Send("You said: " + line + "\r\n"); if (line.equals("quit")) { Send("Goodbye!\r\n"); SetCloseAndDelete(); } } public static void main(String[] args) { StdLog log = new StdoutLog(); SocketHandler h = new SocketHandler(log); ListenSocket l = new ListenSocket(h, new MySocket(h)); if (l.Bind(8000) == 0) // listen on port 8000 { h.Add(l); } boolean quit = false; while (!quit) // forever { h.Select(1, 0); } } } Simple client public class MySocket extends TcpSocket { public MySocket(SocketHandler h) { super(h); SetLineProtocol(); } public void OnConnect() { Send("GET / HTTP/1.0\r\n" + "Host: www.alhem.net\r\n" + "\r\n"); } public void OnLine(String line) { System.out.println(line); } public static void main(String[] args) { StdLog log = new StdoutLog(); SocketHandler h = new SocketHandler(log); MySocket sock = new MySocket(h); sock.Open( "www.alhem.net", 80 ); h.Add( sock ); boolean quit = false; while (!quit) // forever { h.Select(1, 0); } } } Current implementation status
*) version when class was first implemented Differences compared to the C++ Sockets libraryListenSocket Due to Java generics not being the same as C++ templates,
the creation of a ListenSocket is different.
A ListenSocket must have knowledge of what kind of Socket object to create
for incoming connections.
In C++ this is accomplished using ListenSocket as a template class, as in
ListenSocket In Java, I didn't find a good way to do this. I finally decided to add a Create() method to the Socket class. Each Socket class implements its own Create() method, which should return a new object of its own kind. The constructor of the ListenSocket class then is used like this: { StdoutLog log = new StdoutLog(); SocketHandler h = new SocketHandler(log); ListenSocket l = new ListenSocket(h, new MySocket(h)); // ... } The ListenSocket object then owns an instance of whatever Socket class it is supposed to create, and can use that objects Create() method to get a new instance of the correct Socket type. Initialization order When creating a new Socket and adding it to the SocketHandler, a fixed sequence of operations must be obeyed. 1. Create the socket object 2. Use Open() or Bind() methods to create the underlying SelectableChannel java.nio object 3. Add() the object to the SocketHandler - an underlying SelectableChannel object is necessary for this to work |