The Java program uses two main threads
for the client and the server. One reads data
from the keyboard and send it to the socket, and
the other reads from the socket and sends to the
output screen. The files are writethread1.java
(which takes from the keyboard and sends to the
network connection), writethread2.java (which
takes from the network connection and sends to
the output screen), client.java (which makes a
connection to a remote address, and a remote port)
and sever.java (which waits for a connection on
a given port). A sample run is:
To test the code, do the following:
1.
Run the server in one command window
with server 1001 (which
will listen on port 1001), then run the
client in another window with client
127.0.0.1 1001 (which will connect
to the local server). You should be able
to make a connection, and send and receive
messages in the two windows.
2.
Run the server in one command window
on one machine with server 1001
(which will listen on port 1001). Determine
the server IP address with IPCONFIG. Next
run the client on another computer with
client w.x.y.z 1001 (where
w.x.y.z is the IP address of the server).
You should be able to make a connection,
and send and receive messages in the two
computers.
The code has been designed using
threads, as a program without threads would have
problems with synchronisation, as the program
could be stuck waiting for keyboard input, and
not checking for incoming data. The structure
is:
The code for the client is:
//
Written by W.Buchanan, Aug 2003
// client.java
// Run the program with java client
127.0.0.1 1000 for port 1000 on
127.0.0.1
As the programming is written in Java, it can be
used on any type of devices which support Java.
For example the following is taken from a connection
between a Pocket PC and a notebook, over a wireless
connection:
In this case the Pocket PC runs the server, using
server 1000., and the client is
run on the notebook with client 192.168.0.3
1000. (where 192.168.0.3 is the IP address
of the Pocket PC). You can see that the client has
created port 3111 for its side of the connection.