 |
|  |
 |
| Implementation of the Ethernet 10BaseT Connection |
 |
 |
 |
 |
 |
 |
 |
| Implementation of ethernet connections by sockets |
In this example we will show how to use - the SaJe board as a server and the PC as a client - the PC as a server and the SaJe board as a client.
On the hardware side:
Connect the ethernet port on the SaJe board to the ethernet port on your PC: a) using a cross-over cable or b) connect both the SaJe board and the PC to the network on the school.
On the software side:
On the SaJe you shall use the socket classes from the aJile's CLDC library. These classes are in the javax.microedition.io library which is a part of the aJile software package, and on my PC the class files are placed in C:\aJile\Runtime_cldc\classes.jar. You will find no documentation for the classes, but you can download Java 2 Micro Edition CLDC from Sun: http://java.sun.com/products/cldc/ In this package you will find the javadoc documentation for the classes.
On the PC you shall use the socket classes from JDK 1.3.1.
Setting up the project in JEMBuilder:
In "New JVM step 4": Add Drivers to JVM, - here you shall add: Ethernet.
Setting up the IP addresses:
a) If you have a direct connection between the SaJe and your PC with a cross-over cable: - Specify the ethernet IP information in JEMBuilder as par example: IP address: 192.168.8.2 Subnet mask: 255.255.255.0 Gateway address: 192.168.8.1 - Specify in the same way the ethernet IP information on your PC manually, but use another IP address and Gateway adress, that is on Windows XP: Network Connections -> Local Area Connection -> Properties -> Internet protocol (TCP/IP) ->Properties: IP address: 192.168.8.3 Subnet mask: 255.255.255.0 Gateway address: 192.168.8.4
b) If you use the network on the school connecting the SaJe and your PC, you can do the following: - take another PC which is on the ethernet - find out the IP settings on this PC: IP address, subnet mask and gateway adress (you can par example use the command: ipconfig) - take the ethernet cable from this PC and put it in the net stick on the SaJe - write these IP settings in JEMBuilder - remember to let your PC obtain its IP address automatically, if you have changed it manually under point a).
Running the programs:
Nothing special: The CLDC-socket program is downloaded on SaJe. The ordinary socket program is on the PC. You can start it in a Command Prompt.
Implementation of the example programs:
On the client side I have implemented some socket services in a class SocketService. class SocketService
{
private InputStream istream = ...;
private OutputStream ostream = ...;
// Socket read/write methods with bytes:
public void readBytesFromSocket (byte[] inBuf) throws IOException
{
istream.read(inBuf, 0, 128);
}
public void writeBytesToSocket (String str) throws IOException
{
ostream.write(str.getBytes(), 0, str.length());
ostream.flush();
}
}
You can yourself implement more services in this class if you want to.
The next thing to implement is the client which uses the socket services. Here I have a method runClient: class SocketClient // on aJile or on PC
{
.
public void runClient()
{
int maxMessages = 1000; // Number of messages to be sent to host
try
{
for (int i = 0; i < maxMessages; i++)
{
.
ss.writeBytesToSocket (test);
ss.readBytesFromSocket (inBuf);
.
}
// Send the server the signoff message
ss.writeBytesToSocket ("BYE");
}
catch (Exception e) {...}
}
}
I have not shown the details for setting up the connection to the server. These details are hidden in the constructor. It is not so easy to initialize the CLDC client on the aJile. So have a look at the full implementation in the programs below.
On the server side each new connection to the server is handled by a new SocketHandler, which is a thread. This is implemented in the method runServer: class CLDCServerSocket // on the aJile
{
.
public void runServer()
{
String serverSocketURL = "serversocket://:" + portNr;
try
{
StreamConnectionNotifier scn = // I use the cldc connector API
(StreamConnectionNotifier) Connector.open(serverSocketURL, Connector.READ_WRITE, false);
// Start a new thread (SocketHandler) for each new socket connection
int i=0;
while (true)
{
// Wait for a client to request a connection, then create a socket
StreamConnection socket = scn.acceptAndOpen();
new SocketHandler(socket, i).start(); // new thread
i++;
}
}
catch (Exception ex) {...} }
}
}
There is not a great difference between opening a server socket on the aJile and on the PC, but have a look at the details in the programs below.
You can download the programs here:
CLDC-socket client on the aJile CLDC-socket server on the aJile |
 |
 |
 |
 |
 |
 |
 |
Compilation:
Because the package java.net is imported from jdk1.3.1, the following classpath must be included when we compile:
-classpath C:\jdk1.3.1_05\jre\lib\rt.jar
I use a bat file with the following content:
javac -bootclasspath C:\aJile\Runtime_cldc\Rts;C:\aJile\Runtime_cldc\classes.jar -classpath C:\jdk1.3.1_05\jre\lib\rt.jar *.java
pause |
 |
 |
 |
 |
 |
 |
 |
|
 |
|
|  |
|
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
 |
Implementation of the Ethernet 10BaseT Connection |
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|