Home  +  Forums  +  C++ and Sockets  +  C++ and SQL: MySQL, sqlite, ODBC  +  Miscellaneous Projects
Logo
~Sockets~
~New~
~Examples~
~Contact~
english

Java Sockets

Socket programming made easy

Java 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.

Download

All 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 caveat

There 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.

Documentation

javadoc

Doxygen

Code samples

Simple 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

Sockets
Class C++Javaversion*
SocketHandler YesYes0.1
Socket YesYes0.1
ListenSocket YesYes0.1
TcpSocket YesYes0.1
UdpSocket YesNo
CTcpSocket YesNo
NullCrypt YesNo
SSLSocket YesNo
HTTPSocket YesYes0.1
HttpGetSocket YesYes0.1
HttpPutSocket YesNo
HttpPostSocket YesNo
HttpDebugSocket YesNo
HttpsSocket YesNo
HttpsGetSocket YesNo
Thread YesNo
SocketThread YesNo
Base64 YesNo
CircularBuffer YesNo
Parse YesYes0.1
StdLog YesYes0.1
StdoutLog YesYes0.1
SocketsEx
Class C++Javaversion*
Uid YesNo
MinderSocket YesNo
MinionSocket YesNo
MinderHandler YesNo

*) version when class was first implemented

Differences compared to the C++ Sockets library

ListenSocket

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. When a new connection is accepted, the ListenSocket class will create a NewKindOfSocket object and add it to the SocketHandler.

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

Page, code, and content Copyright (C) 2021 by Anders Hedström